From 25843710c39d03558add11cd86d211839ad14d27 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 30 Apr 2022 06:22:01 +0200 Subject: [PATCH] Fix error when response contains reblogs of soft-deleted statuses in REST API --- app/models/status.rb | 18 +++++++----------- .../status_relationships_presenter.rb | 2 +- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/app/models/status.rb b/app/models/status.rb index 288d374fd..182bbf9fd 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -336,22 +336,18 @@ class Status < ApplicationRecord end def reload_stale_associations!(cached_items) - account_ids = [] - - cached_items.each do |item| - account_ids << item.account_id - account_ids << item.reblog.account_id if item.reblog? - end - - account_ids.uniq! + account_ids = cached_items.each_with_object([]) do |item, arr| + arr << item.account_id + arr << item.reblog&.account_id if item.reblog? + end.compact.uniq return if account_ids.empty? - accounts = Account.where(id: account_ids).includes(:account_stat, :user).index_by(&:id) + accounts_map = Account.where(id: account_ids).includes(:account_stat, :user).index_by(&:id) cached_items.each do |item| - item.account = accounts[item.account_id] - item.reblog.account = accounts[item.reblog.account_id] if item.reblog? + item.account = accounts_map[item.account_id] + item.reblog.account = accounts_map[item.reblog.account_id] if item.reblog? && item.reblog end end diff --git a/app/presenters/status_relationships_presenter.rb b/app/presenters/status_relationships_presenter.rb index 4163bb098..3c29b7e7e 100644 --- a/app/presenters/status_relationships_presenter.rb +++ b/app/presenters/status_relationships_presenter.rb @@ -15,7 +15,7 @@ class StatusRelationshipsPresenter statuses = statuses.compact status_ids = statuses.flat_map { |s| [s.id, s.reblog_of_id] }.uniq.compact conversation_ids = statuses.filter_map(&:conversation_id).uniq - pinnable_status_ids = statuses.map(&:proper).filter_map { |s| s.id if s.account_id == current_account_id && %w(public unlisted private).include?(s.visibility) } + pinnable_status_ids = statuses.filter_map(&:proper).filter_map { |s| s.id if s.account_id == current_account_id && %w(public unlisted private).include?(s.visibility) } @reblogs_map = Status.reblogs_map(status_ids, current_account_id).merge(options[:reblogs_map] || {}) @favourites_map = Status.favourites_map(status_ids, current_account_id).merge(options[:favourites_map] || {})