Hydrate user-specific feature approval policy (#39194)
This commit is contained in:
parent
554b6cf35e
commit
faa5944d46
@ -5,42 +5,45 @@ class StatusCacheHydrator
|
|||||||
@status = status
|
@status = status
|
||||||
end
|
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
|
# 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) }
|
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
|
# 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
|
# 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
|
# 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
|
# the reblog that wraps it, so we can assume that some values are always false
|
||||||
if payload[:reblog]
|
if payload[:reblog]
|
||||||
hydrate_reblog_payload(payload, account_id, nested:)
|
hydrate_reblog_payload(payload, account, nested:)
|
||||||
else
|
else
|
||||||
hydrate_non_reblog_payload(payload, account_id, nested:)
|
hydrate_non_reblog_payload(payload, account, nested:)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
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|
|
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
|
||||||
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|
|
empty_payload.tap do |payload|
|
||||||
payload[:muted] = false
|
payload[:muted] = false
|
||||||
payload[:bookmarked] = 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
|
# 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
|
# 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[:filtered] = payload[:reblog][:filtered]
|
||||||
payload[:favourited] = payload[:reblog][:favourited]
|
payload[:favourited] = payload[:reblog][:favourited]
|
||||||
@ -49,36 +52,37 @@ class StatusCacheHydrator
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def fill_status_payload(payload, status, account_id, nested: false, fresh: true)
|
def fill_status_payload(payload, status, account, nested: false, fresh: true)
|
||||||
payload[:favourited] = Favourite.exists?(account_id: account_id, status_id: status.id)
|
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[: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[: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[: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[: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)
|
payload[:filtered] = mapped_applied_custom_filter(account, status)
|
||||||
# TODO: performance optimization by not loading `Account` twice
|
payload[:quote_approval][:current_user] = status.quote_policy_for_account(account) if payload[:quote_approval]
|
||||||
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, nested:) if payload[:quote]
|
||||||
payload[:quote] = hydrate_quote_payload(payload[:quote], status.quote, account_id, nested:) if payload[:quote]
|
|
||||||
|
|
||||||
if payload[:poll]
|
if payload[:poll]
|
||||||
if fresh
|
if fresh
|
||||||
# If the status is brand new, we don't need to look up votes in database
|
# 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] = []
|
payload[:poll][:own_votes] = []
|
||||||
elsif status.account_id == account_id
|
elsif status.account_id == account.id
|
||||||
payload[:poll][:voted] = true
|
payload[:poll][:voted] = true
|
||||||
payload[:poll][:own_votes] = []
|
payload[:poll][:own_votes] = []
|
||||||
else
|
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][:voted] = !own_votes.empty?
|
||||||
payload[:poll][:own_votes] = own_votes
|
payload[:poll][:own_votes] = own_votes
|
||||||
end
|
end
|
||||||
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
|
# Nested statuses are more likely to have a stale cache
|
||||||
fill_status_stats(payload, status) if nested
|
fill_status_stats(payload, status) if nested
|
||||||
|
|
||||||
|
hydrate_account(payload[:account], account)
|
||||||
end
|
end
|
||||||
|
|
||||||
def fill_status_stats(payload, status)
|
def fill_status_stats(payload, status)
|
||||||
@ -88,7 +92,7 @@ class StatusCacheHydrator
|
|||||||
payload[:quotes_count] = status.quotes_count
|
payload[:quotes_count] = status.quotes_count
|
||||||
end
|
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?
|
return unless quote&.acceptable?
|
||||||
|
|
||||||
empty_payload.tap do |payload|
|
empty_payload.tap do |payload|
|
||||||
@ -100,14 +104,14 @@ class StatusCacheHydrator
|
|||||||
payload[nested ? :quoted_status_id : :quoted_status] = nil
|
payload[nested ? :quoted_status_id : :quoted_status] = nil
|
||||||
payload[:state] = 'deleted'
|
payload[:state] = 'deleted'
|
||||||
else
|
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'
|
payload[:state] = filter_state || 'accepted'
|
||||||
if filter_state == 'unauthorized'
|
if filter_state == 'unauthorized'
|
||||||
payload[nested ? :quoted_status_id : :quoted_status] = nil
|
payload[nested ? :quoted_status_id : :quoted_status] = nil
|
||||||
elsif nested
|
elsif nested
|
||||||
payload[:quoted_status_id] = quote.quoted_status_id&.to_s
|
payload[:quoted_status_id] = quote.quoted_status_id&.to_s
|
||||||
else
|
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
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@ -116,9 +120,19 @@ class StatusCacheHydrator
|
|||||||
end
|
end
|
||||||
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
|
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) }
|
.map { |filter| serialized_filter(filter) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ RSpec.describe StatusCacheHydrator do
|
|||||||
let(:status) { Fabricate(:status) }
|
let(:status) { Fabricate(:status) }
|
||||||
let(:account) { Fabricate(:account) }
|
let(:account) { Fabricate(:account) }
|
||||||
|
|
||||||
describe '#hydrate' do
|
describe '#hydrate', feature: :collections do
|
||||||
let(:compare_to_hash) { InlineRenderer.render(status, account, :status) }
|
let(:compare_to_hash) { InlineRenderer.render(status, account, :status) }
|
||||||
|
|
||||||
shared_examples 'shared behavior' do
|
shared_examples 'shared behavior' do
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user