From ce1bd81c854c3fc3fa19aa41d9c56520c25325b6 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 9 Nov 2023 04:53:44 -0500 Subject: [PATCH] Reduce complexity in `StatusCacheHydrator` (#27783) --- app/lib/status_cache_hydrator.rb | 52 ++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/app/lib/status_cache_hydrator.rb b/app/lib/status_cache_hydrator.rb index c1231311b1..45b50cb379 100644 --- a/app/lib/status_cache_hydrator.rb +++ b/app/lib/status_cache_hydrator.rb @@ -16,12 +16,36 @@ class StatusCacheHydrator # We take advantage of the fact that some relationships can only occur with an original status, not # the reblog that wraps it, so we can assume that some values are always false if payload[:reblog] + hydrate_reblog_payload(payload, account_id) + else + hydrate_non_reblog_payload(payload, account_id) + end + end + + private + + def hydrate_non_reblog_payload(empty_payload, account_id) + empty_payload.tap do |payload| + payload[:favourited] = Favourite.where(account_id: account_id, status_id: @status.id).exists? + payload[:reblogged] = Status.where(account_id: account_id, reblog_of_id: @status.id).exists? + payload[:muted] = ConversationMute.where(account_id: account_id, conversation_id: @status.conversation_id).exists? + payload[:bookmarked] = Bookmark.where(account_id: account_id, status_id: @status.id).exists? + payload[:pinned] = StatusPin.where(account_id: account_id, status_id: @status.id).exists? if @status.account_id == account_id + payload[:filtered] = mapped_applied_custom_filter(account_id, @status) + + if payload[:poll] + payload[:poll][:voted] = @status.account_id == account_id + payload[:poll][:own_votes] = [] + end + end + end + + def hydrate_reblog_payload(empty_payload, account_id) + empty_payload.tap do |payload| payload[:muted] = false payload[:bookmarked] = false payload[:pinned] = false if @status.account_id == account_id - payload[:filtered] = CustomFilter - .apply_cached_filters(CustomFilter.cached_filters_for(account_id), @status.reblog) - .map { |filter| serialized_filter(filter) } + payload[:filtered] = mapped_applied_custom_filter(account_id, @status.reblog) # If the reblogged status is being delivered to the author who disabled the display of the application # used to create the status, we need to hydrate it here too @@ -47,26 +71,14 @@ class StatusCacheHydrator payload[:favourited] = payload[:reblog][:favourited] payload[:reblogged] = payload[:reblog][:reblogged] - else - payload[:favourited] = Favourite.where(account_id: account_id, status_id: @status.id).exists? - payload[:reblogged] = Status.where(account_id: account_id, reblog_of_id: @status.id).exists? - payload[:muted] = ConversationMute.where(account_id: account_id, conversation_id: @status.conversation_id).exists? - payload[:bookmarked] = Bookmark.where(account_id: account_id, status_id: @status.id).exists? - payload[:pinned] = StatusPin.where(account_id: account_id, status_id: @status.id).exists? if @status.account_id == account_id - payload[:filtered] = CustomFilter - .apply_cached_filters(CustomFilter.cached_filters_for(account_id), @status) - .map { |filter| serialized_filter(filter) } - - if payload[:poll] - payload[:poll][:voted] = @status.account_id == account_id - payload[:poll][:own_votes] = [] - end end - - payload end - private + def mapped_applied_custom_filter(account_id, status) + CustomFilter + .apply_cached_filters(CustomFilter.cached_filters_for(account_id), status) + .map { |filter| serialized_filter(filter) } + end def serialized_filter(filter) ActiveModelSerializers::SerializableResource.new(