Skip to content

Commit

Permalink
Fix cached posts including stale stats (#26409)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire committed Sep 19, 2023
1 parent 9c91379 commit 5aba8e7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
12 changes: 12 additions & 0 deletions app/models/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,25 @@ def reload_stale_associations!(cached_items)

account_ids.uniq!

status_ids = cached_items.map { |item| item.reblog? ? item.reblog_of_id : item.id }.uniq

return if account_ids.empty?

accounts = Account.where(id: account_ids).includes(:account_stat, :user).index_by(&:id)

status_stats = StatusStat.where(status_id: status_ids).index_by(&:status_id)

cached_items.each do |item|
item.account = accounts[item.account_id]
item.reblog.account = accounts[item.reblog.account_id] if item.reblog?

if item.reblog?
status_stat = status_stats[item.reblog.id]
item.reblog.status_stat = status_stat if status_stat.present?
else
status_stat = status_stats[item.id]
item.status_stat = status_stat if status_stat.present?
end
end
end

Expand Down
24 changes: 22 additions & 2 deletions spec/controllers/concerns/cache_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ def empty_array
def empty_relation
render plain: cache_collection(Status.none, Status).size
end

def account_statuses_favourites
render plain: cache_collection(Status.where(account_id: params[:id]), Status).map(&:favourites_count)
end
end

before do
routes.draw do
get 'empty_array' => 'anonymous#empty_array'
post 'empty_relation' => 'anonymous#empty_relation'
get 'empty_array' => 'anonymous#empty_array'
get 'empty_relation' => 'anonymous#empty_relation'
get 'account_statuses_favourites' => 'anonymous#account_statuses_favourites'
end
end

Expand All @@ -36,5 +41,20 @@ def empty_relation
expect(response.body).to eq '0'
end
end

context 'when given a collection of statuses' do
let!(:account) { Fabricate(:account) }
let!(:status) { Fabricate(:status, account: account) }

it 'correctly updates with new interactions' do
get :account_statuses_favourites, params: { id: account.id }
expect(response.body).to eq '[0]'

FavouriteService.new.call(account, status)

get :account_statuses_favourites, params: { id: account.id }
expect(response.body).to eq '[1]'
end
end
end
end

0 comments on commit 5aba8e7

Please sign in to comment.