about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2018-12-08 01:32:26 +0100
committerGitHub <noreply@github.com>2018-12-08 01:32:26 +0100
commit6b78e5b5ab813625152149b7e215d0568a595cdc (patch)
treee3bccf93cf3bfda06b35f1e4069cf7ef3a8454b1 /app
parent4638b1682247df52ab5b22d06cd2a3df3277b2f9 (diff)
Cache hashtag sample accounts, and exclude ineligible ones (#9465)
Diffstat (limited to 'app')
-rw-r--r--app/models/account.rb2
-rw-r--r--app/models/tag.rb7
-rw-r--r--app/views/directories/index.html.haml2
3 files changed, 8 insertions, 3 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index c22836afe..9767e3767 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -94,7 +94,7 @@ class Account < ApplicationRecord
   scope :discoverable, -> { where(silenced: false).where(discoverable: true).joins(:account_stat).where(AccountStat.arel_table[:followers_count].gteq(MIN_FOLLOWERS_DISCOVERY)) }
   scope :tagged_with, ->(tag) { joins(:accounts_tags).where(accounts_tags: { tag_id: tag }) }
   scope :popular, -> { order('account_stats.followers_count desc') }
-  scope :by_recent_status, -> { order('(case when account_stats.last_status_at is null then 1 else 0 end) asc, account_stats.last_status_at desc') }
+  scope :by_recent_status, -> { order(Arel.sql('(case when account_stats.last_status_at is null then 1 else 0 end) asc, account_stats.last_status_at desc')) }
 
   delegate :email,
            :unconfirmed_email,
diff --git a/app/models/tag.rb b/app/models/tag.rb
index 41e58e3ca..99830ae92 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -12,6 +12,7 @@
 class Tag < ApplicationRecord
   has_and_belongs_to_many :statuses
   has_and_belongs_to_many :accounts
+  has_and_belongs_to_many :sample_accounts, -> { searchable.discoverable.popular.limit(3) }, class_name: 'Account'
 
   has_one :account_tag_stat, dependent: :destroy
 
@@ -20,7 +21,7 @@ class Tag < ApplicationRecord
 
   validates :name, presence: true, uniqueness: true, format: { with: /\A#{HASHTAG_NAME_RE}\z/i }
 
-  scope :discoverable, -> { joins(:account_tag_stat).where(AccountTagStat.arel_table[:accounts_count].gt(0)).where(account_tag_stats: { hidden: false }).order('account_tag_stats.accounts_count desc') }
+  scope :discoverable, -> { joins(:account_tag_stat).where(AccountTagStat.arel_table[:accounts_count].gt(0)).where(account_tag_stats: { hidden: false }).order(Arel.sql('account_tag_stats.accounts_count desc')) }
   scope :hidden, -> { where(account_tag_stats: { hidden: true }) }
 
   delegate :accounts_count,
@@ -36,6 +37,10 @@ class Tag < ApplicationRecord
     super || build_account_tag_stat
   end
 
+  def cached_sample_accounts
+    Rails.cache.fetch("#{cache_key}/sample_accounts", expires_in: 12.hours) { sample_accounts }
+  end
+
   def to_param
     name
   end
diff --git a/app/views/directories/index.html.haml b/app/views/directories/index.html.haml
index 219950a51..f70eb964a 100644
--- a/app/views/directories/index.html.haml
+++ b/app/views/directories/index.html.haml
@@ -57,5 +57,5 @@
               %small= t('directories.people', count: tag.accounts_count)
 
             .avatar-stack
-              - tag.accounts.limit(3).each do |account|
+              - tag.cached_sample_accounts.each do |account|
                 = image_tag current_account&.user&.setting_auto_play_gif ? account.avatar_original_url : account.avatar_static_url, width: 48, height: 48, alt: '', class: 'account__avatar'