Browse Source

Cache hashtag sample accounts, and exclude ineligible ones (#9465)

Eugen Rochko 5 years ago
parent
commit
6b78e5b5ab
3 changed files with 8 additions and 3 deletions
  1. 1 1
      app/models/account.rb
  2. 6 1
      app/models/tag.rb
  3. 1 1
      app/views/directories/index.html.haml

+ 1 - 1
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,

+ 6 - 1
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

+ 1 - 1
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'