Hydrate user-specific feature approval policy (#39194)
This commit is contained in:
parent
554b6cf35e
commit
faa5944d46
@ -5,42 +5,45 @@ class StatusCacheHydrator
|
||||
@status = status
|
||||
end
|
||||
|
||||
def hydrate(account_id, nested: false)
|
||||
def hydrate(account_or_id, nested: false)
|
||||
account = account_or_id.is_a?(Account) ? account_or_id : Account.find(account_or_id)
|
||||
|
||||
# The cache of the serialized hash is generated by the fan-out-on-write service
|
||||
payload = Rails.cache.fetch("fan-out/#{@status.id}") { InlineRenderer.render(@status, nil, :status) }
|
||||
|
||||
# If we're delivering to the author who disabled the display of the application used to create the
|
||||
# status, we need to hydrate the application, since it was not rendered for the basic payload
|
||||
payload[:application] = payload_application if payload[:application].nil? && @status.account_id == account_id
|
||||
payload[:application] = payload_application if payload[:application].nil? && @status.account_id == account.id
|
||||
|
||||
# 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, nested:)
|
||||
hydrate_reblog_payload(payload, account, nested:)
|
||||
else
|
||||
hydrate_non_reblog_payload(payload, account_id, nested:)
|
||||
hydrate_non_reblog_payload(payload, account, nested:)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def hydrate_non_reblog_payload(empty_payload, account_id, nested: false)
|
||||
def hydrate_non_reblog_payload(empty_payload, account, nested: false)
|
||||
empty_payload.tap do |payload|
|
||||
fill_status_payload(payload, @status, account_id, fresh: !nested, nested:)
|
||||
fill_status_payload(payload, @status, account, fresh: !nested, nested:)
|
||||
end
|
||||
end
|
||||
|
||||
def hydrate_reblog_payload(empty_payload, account_id, nested: false)
|
||||
def hydrate_reblog_payload(empty_payload, account, nested: false)
|
||||
empty_payload.tap do |payload|
|
||||
payload[:muted] = false
|
||||
payload[:bookmarked] = false
|
||||
payload[:pinned] = false if @status.account_id == account_id
|
||||
payload[:pinned] = false if @status.account_id == account.id
|
||||
|
||||
# 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
|
||||
payload[:reblog][:application] = payload_reblog_application if payload[:reblog][:application].nil? && @status.reblog.account_id == account_id
|
||||
payload[:reblog][:application] = payload_reblog_application if payload[:reblog][:application].nil? && @status.reblog.account_id == account.id
|
||||
|
||||
fill_status_payload(payload[:reblog], @status.reblog, account_id, fresh: false, nested:)
|
||||
hydrate_account(payload[:account], account)
|
||||
fill_status_payload(payload[:reblog], @status.reblog, account, fresh: false, nested:)
|
||||
|
||||
payload[:filtered] = payload[:reblog][:filtered]
|
||||
payload[:favourited] = payload[:reblog][:favourited]
|
||||
@ -49,36 +52,37 @@ class StatusCacheHydrator
|
||||
end
|
||||
end
|
||||
|
||||
def fill_status_payload(payload, status, account_id, nested: false, fresh: true)
|
||||
payload[:favourited] = Favourite.exists?(account_id: account_id, status_id: status.id)
|
||||
payload[:reblogged] = Status.exists?(account_id: account_id, reblog_of_id: status.id)
|
||||
payload[:muted] = ConversationMute.exists?(account_id: account_id, conversation_id: status.conversation_id)
|
||||
payload[:bookmarked] = Bookmark.exists?(account_id: account_id, status_id: status.id)
|
||||
payload[:pinned] = StatusPin.exists?(account_id: account_id, status_id: status.id) if status.account_id == account_id
|
||||
payload[:filtered] = mapped_applied_custom_filter(account_id, status)
|
||||
# TODO: performance optimization by not loading `Account` twice
|
||||
payload[:quote_approval][:current_user] = status.quote_policy_for_account(Account.find_by(id: account_id)) if payload[:quote_approval]
|
||||
payload[:quote] = hydrate_quote_payload(payload[:quote], status.quote, account_id, nested:) if payload[:quote]
|
||||
def fill_status_payload(payload, status, account, nested: false, fresh: true)
|
||||
payload[:favourited] = Favourite.exists?(account_id: account.id, status_id: status.id)
|
||||
payload[:reblogged] = Status.exists?(account_id: account.id, reblog_of_id: status.id)
|
||||
payload[:muted] = ConversationMute.exists?(account_id: account.id, conversation_id: status.conversation_id)
|
||||
payload[:bookmarked] = Bookmark.exists?(account_id: account.id, status_id: status.id)
|
||||
payload[:pinned] = StatusPin.exists?(account_id: account.id, status_id: status.id) if status.account_id == account.id
|
||||
payload[:filtered] = mapped_applied_custom_filter(account, status)
|
||||
payload[:quote_approval][:current_user] = status.quote_policy_for_account(account) if payload[:quote_approval]
|
||||
payload[:quote] = hydrate_quote_payload(payload[:quote], status.quote, account, nested:) if payload[:quote]
|
||||
|
||||
if payload[:poll]
|
||||
if fresh
|
||||
# If the status is brand new, we don't need to look up votes in database
|
||||
payload[:poll][:voted] = status.account_id == account_id
|
||||
payload[:poll][:voted] = status.account_id == account.id
|
||||
payload[:poll][:own_votes] = []
|
||||
elsif status.account_id == account_id
|
||||
elsif status.account_id == account.id
|
||||
payload[:poll][:voted] = true
|
||||
payload[:poll][:own_votes] = []
|
||||
else
|
||||
own_votes = PollVote.where(poll_id: status.poll_id, account_id: account_id).pluck(:choice)
|
||||
own_votes = PollVote.where(poll_id: status.poll_id, account_id: account.id).pluck(:choice)
|
||||
payload[:poll][:voted] = !own_votes.empty?
|
||||
payload[:poll][:own_votes] = own_votes
|
||||
end
|
||||
end
|
||||
|
||||
payload[:card][:missing_attribution] = status.preview_card.unverified_author_account_id == account_id if payload[:card]
|
||||
payload[:card][:missing_attribution] = status.preview_card.unverified_author_account_id == account.id if payload[:card]
|
||||
|
||||
# Nested statuses are more likely to have a stale cache
|
||||
fill_status_stats(payload, status) if nested
|
||||
|
||||
hydrate_account(payload[:account], account)
|
||||
end
|
||||
|
||||
def fill_status_stats(payload, status)
|
||||
@ -88,7 +92,7 @@ class StatusCacheHydrator
|
||||
payload[:quotes_count] = status.quotes_count
|
||||
end
|
||||
|
||||
def hydrate_quote_payload(empty_payload, quote, account_id, nested: false)
|
||||
def hydrate_quote_payload(empty_payload, quote, account, nested: false)
|
||||
return unless quote&.acceptable?
|
||||
|
||||
empty_payload.tap do |payload|
|
||||
@ -100,14 +104,14 @@ class StatusCacheHydrator
|
||||
payload[nested ? :quoted_status_id : :quoted_status] = nil
|
||||
payload[:state] = 'deleted'
|
||||
else
|
||||
filter_state = StatusFilter.new(quote.quoted_status, Account.find_by(id: account_id)).filter_state_for_quote
|
||||
filter_state = StatusFilter.new(quote.quoted_status, account).filter_state_for_quote
|
||||
payload[:state] = filter_state || 'accepted'
|
||||
if filter_state == 'unauthorized'
|
||||
payload[nested ? :quoted_status_id : :quoted_status] = nil
|
||||
elsif nested
|
||||
payload[:quoted_status_id] = quote.quoted_status_id&.to_s
|
||||
else
|
||||
payload[:quoted_status] = StatusCacheHydrator.new(quote.quoted_status).hydrate(account_id, nested: true)
|
||||
payload[:quoted_status] = StatusCacheHydrator.new(quote.quoted_status).hydrate(account, nested: true)
|
||||
end
|
||||
end
|
||||
else
|
||||
@ -116,9 +120,19 @@ class StatusCacheHydrator
|
||||
end
|
||||
end
|
||||
|
||||
def mapped_applied_custom_filter(account_id, status)
|
||||
def hydrate_account(payload, account)
|
||||
return unless Mastodon::Feature.collections_enabled?
|
||||
return unless payload[:id]
|
||||
|
||||
stale_account = Account.find_by(id: payload[:id])
|
||||
return if stale_account.nil?
|
||||
|
||||
payload[:feature_approval][:current_user] = stale_account.feature_policy_for_account(account)
|
||||
end
|
||||
|
||||
def mapped_applied_custom_filter(account, status)
|
||||
CustomFilter
|
||||
.apply_cached_filters(CustomFilter.cached_filters_for(account_id), status)
|
||||
.apply_cached_filters(CustomFilter.cached_filters_for(account), status)
|
||||
.map { |filter| serialized_filter(filter) }
|
||||
end
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ RSpec.describe StatusCacheHydrator do
|
||||
let(:status) { Fabricate(:status) }
|
||||
let(:account) { Fabricate(:account) }
|
||||
|
||||
describe '#hydrate' do
|
||||
describe '#hydrate', feature: :collections do
|
||||
let(:compare_to_hash) { InlineRenderer.render(status, account, :status) }
|
||||
|
||||
shared_examples 'shared behavior' do
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user