Merge branch 'kb_patch' into kb_development
This commit is contained in:
commit
e0da14515f
16 changed files with 100 additions and 22 deletions
|
@ -9,7 +9,9 @@ class Api::V1::Accounts::StatusesController < Api::BaseController
|
|||
def index
|
||||
cache_if_unauthenticated!
|
||||
@statuses = load_statuses
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -7,7 +7,9 @@ class Api::V1::BookmarksController < Api::BaseController
|
|||
|
||||
def index
|
||||
@statuses = load_statuses
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -7,7 +7,9 @@ class Api::V1::EmojiReactionsController < Api::BaseController
|
|||
|
||||
def index
|
||||
@statuses = load_statuses
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -7,7 +7,9 @@ class Api::V1::FavouritesController < Api::BaseController
|
|||
|
||||
def index
|
||||
@statuses = load_statuses
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -9,7 +9,9 @@ class Api::V1::Statuses::ReferredByStatusesController < Api::BaseController
|
|||
|
||||
def index
|
||||
@statuses = load_statuses
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -9,11 +9,13 @@ class Api::V1::Timelines::HomeController < Api::BaseController
|
|||
with_read_replica do
|
||||
@statuses = load_statuses
|
||||
@relationships = StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
@emoji_reactions = EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
render json: @statuses,
|
||||
each_serializer: REST::StatusSerializer,
|
||||
relationships: @relationships,
|
||||
emoji_reaction_permitted_account_ids: @emoji_reactions,
|
||||
status: account_home_feed.regenerating? ? 206 : 200
|
||||
end
|
||||
|
||||
|
|
|
@ -7,7 +7,9 @@ class Api::V1::Timelines::PublicController < Api::BaseController
|
|||
def show
|
||||
cache_if_unauthenticated!
|
||||
@statuses = load_statuses
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -8,7 +8,9 @@ class Api::V1::Timelines::TagController < Api::BaseController
|
|||
def show
|
||||
cache_if_unauthenticated!
|
||||
@statuses = load_statuses
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||
render json: @statuses, each_serializer: REST::StatusSerializer,
|
||||
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
|
||||
emoji_reaction_permitted_account_ids: EmojiReactionAccountsPresenter.new(@statuses, current_user&.account_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -357,21 +357,30 @@ class Status < ApplicationRecord
|
|||
return [] if account.present? && !self.account.show_emoji_reaction?(account)
|
||||
return [] if account.nil? && !options[:force] && self.account.emoji_reaction_policy != :allow
|
||||
|
||||
permitted_account_ids = options[:permitted_account_ids]
|
||||
|
||||
(Oj.load(status_stat&.emoji_reactions || '', mode: :strict) || []).tap do |emoji_reactions|
|
||||
if account.present?
|
||||
remove_emoji_reactions = []
|
||||
public_emoji_reactions = []
|
||||
|
||||
emoji_reactions.each do |emoji_reaction|
|
||||
emoji_reaction['me'] = emoji_reaction['account_ids'].include?(account.id.to_s)
|
||||
emoji_reaction['account_ids'] -= account.excluded_from_timeline_account_ids.map(&:to_s)
|
||||
|
||||
accounts = Account.where(id: emoji_reaction['account_ids'], silenced_at: nil, suspended_at: nil)
|
||||
accounts = accounts.where('domain IS NULL OR domain NOT IN (?)', account.excluded_from_timeline_domains) if account.excluded_from_timeline_domains.size.positive?
|
||||
emoji_reaction['account_ids'] = accounts.pluck(:id).map(&:to_s)
|
||||
accounts = []
|
||||
if permitted_account_ids
|
||||
emoji_reaction['account_ids'] = emoji_reaction['account_ids'] & permitted_account_ids.map(&:to_s)
|
||||
else
|
||||
accounts = Account.where(id: emoji_reaction['account_ids'], silenced_at: nil, suspended_at: nil)
|
||||
accounts = accounts.where('domain IS NULL OR domain NOT IN (?)', account.excluded_from_timeline_domains) if account.excluded_from_timeline_domains.size.positive?
|
||||
emoji_reaction['account_ids'] = accounts.pluck(:id).map(&:to_s)
|
||||
end
|
||||
|
||||
emoji_reaction['count'] = emoji_reaction['account_ids'].size
|
||||
remove_emoji_reactions << emoji_reaction if emoji_reaction['count'] <= 0
|
||||
public_emoji_reactions << emoji_reaction if (emoji_reaction['count']).positive?
|
||||
end
|
||||
emoji_reactions - remove_emoji_reactions
|
||||
|
||||
public_emoji_reactions
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -463,6 +472,11 @@ class Status < ApplicationRecord
|
|||
EmojiReaction.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |e, h| h[e.status_id] = true }
|
||||
end
|
||||
|
||||
def emoji_reaction_allows_map(status_ids, account_id)
|
||||
my_account = Account.find_by(id: account_id)
|
||||
Status.where(id: status_ids).pluck(:account_id).uniq.index_with { |a| Account.find_by(id: a).show_emoji_reaction?(my_account) }
|
||||
end
|
||||
|
||||
def reload_stale_associations!(cached_items)
|
||||
account_ids = []
|
||||
|
||||
|
|
|
@ -18,8 +18,16 @@ class StatusReference < ApplicationRecord
|
|||
has_one :notification, as: :activity, dependent: :destroy
|
||||
|
||||
validate :validate_status_visibilities
|
||||
after_commit :reset_parent_cache
|
||||
|
||||
private
|
||||
|
||||
def validate_status_visibilities
|
||||
raise Mastodon::ValidationError, I18n.t('status_references.errors.invalid_status_visibilities') if [:public, :public_unlisted, :unlisted, :login].exclude?(target_status.visibility.to_sym)
|
||||
end
|
||||
|
||||
def reset_parent_cache
|
||||
Rails.cache.delete("statuses/#{status_id}")
|
||||
Rails.cache.delete("statuses/#{target_status_id}")
|
||||
end
|
||||
end
|
||||
|
|
22
app/presenters/emoji_reaction_accounts_presenter.rb
Normal file
22
app/presenters/emoji_reaction_accounts_presenter.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class EmojiReactionAccountsPresenter
|
||||
attr_reader :permitted_account_ids
|
||||
|
||||
def initialize(statuses, current_account_id = nil, **_options)
|
||||
@current_account_id = current_account_id
|
||||
|
||||
statuses = statuses.compact
|
||||
status_ids = statuses.flat_map { |s| [s.id, s.reblog_of_id] }.uniq.compact
|
||||
emoji_reactions = EmojiReaction.where(status_id: status_ids)
|
||||
account_ids = emoji_reactions.pluck(:account_id).uniq
|
||||
|
||||
permitted_accounts = Account.where(id: account_ids, silenced_at: nil, suspended_at: nil)
|
||||
if current_account_id.present?
|
||||
account = Account.find(current_account_id)
|
||||
permitted_accounts = permitted_accounts.where('domain IS NULL OR domain NOT IN (?)', account.excluded_from_timeline_domains) if account.present? && account.excluded_from_timeline_domains.size.positive?
|
||||
end
|
||||
|
||||
@permitted_account_ids = permitted_accounts.pluck(:id)
|
||||
end
|
||||
end
|
|
@ -4,7 +4,7 @@ class StatusRelationshipsPresenter
|
|||
PINNABLE_VISIBILITIES = %w(public public_unlisted unlisted login private).freeze
|
||||
|
||||
attr_reader :reblogs_map, :favourites_map, :mutes_map, :pins_map,
|
||||
:bookmarks_map, :filters_map, :emoji_reactions_map, :attributes_map
|
||||
:bookmarks_map, :filters_map, :emoji_reactions_map, :attributes_map, :emoji_reaction_allows_map
|
||||
|
||||
def initialize(statuses, current_account_id = nil, **options)
|
||||
@current_account_id = current_account_id
|
||||
|
@ -17,6 +17,7 @@ class StatusRelationshipsPresenter
|
|||
@pins_map = {}
|
||||
@filters_map = {}
|
||||
@emoji_reactions_map = {}
|
||||
@emoji_reaction_allows_map = nil
|
||||
else
|
||||
statuses = statuses.compact
|
||||
status_ids = statuses.flat_map { |s| [s.id, s.reblog_of_id] }.uniq.compact
|
||||
|
@ -30,6 +31,7 @@ class StatusRelationshipsPresenter
|
|||
@mutes_map = Status.mutes_map(conversation_ids, current_account_id).merge(options[:mutes_map] || {})
|
||||
@pins_map = Status.pins_map(pinnable_status_ids, current_account_id).merge(options[:pins_map] || {})
|
||||
@emoji_reactions_map = Status.emoji_reactions_map(status_ids, current_account_id).merge(options[:emoji_reactions_map] || {})
|
||||
@emoji_reaction_allows_map = Status.emoji_reaction_allows_map(status_ids, current_account_id).merge(options[:emoji_reaction_allows_map] || {})
|
||||
@attributes_map = options[:attributes_map] || {}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -123,7 +123,7 @@ class REST::StatusSerializer < ActiveModel::Serializer
|
|||
end
|
||||
|
||||
def emoji_reactions
|
||||
object.emoji_reactions_grouped_by_name(current_user&.account)
|
||||
show_emoji_reaction? ? object.emoji_reactions_grouped_by_name(current_user&.account, permitted_account_ids: emoji_reaction_permitted_account_ids) : []
|
||||
end
|
||||
|
||||
def emoji_reactions_count
|
||||
|
@ -132,7 +132,17 @@ class REST::StatusSerializer < ActiveModel::Serializer
|
|||
|
||||
object.account.emoji_reaction_policy == :allow ? object.emoji_reactions_count : 0
|
||||
else
|
||||
object.account.show_emoji_reaction?(current_user.account) ? object.emoji_reactions_count : 0
|
||||
show_emoji_reaction? ? object.emoji_reactions_count : 0
|
||||
end
|
||||
end
|
||||
|
||||
def show_emoji_reaction?
|
||||
if relationships
|
||||
return true if relationships.emoji_reaction_allows_map.nil?
|
||||
|
||||
relationships.emoji_reaction_allows_map[object.account_id] || false
|
||||
else
|
||||
object.account.show_emoji_reaction?(current_user.account)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -220,6 +230,10 @@ class REST::StatusSerializer < ActiveModel::Serializer
|
|||
instance_options && instance_options[:relationships]
|
||||
end
|
||||
|
||||
def emoji_reaction_permitted_account_ids
|
||||
current_user.present? && instance_options && instance_options[:emoji_reaction_permitted_account_ids]&.permitted_account_ids
|
||||
end
|
||||
|
||||
class ApplicationSerializer < ActiveModel::Serializer
|
||||
attributes :name, :website
|
||||
|
||||
|
|
|
@ -5,13 +5,14 @@ class ProcessReferencesService < BaseService
|
|||
|
||||
DOMAIN = ENV['WEB_DOMAIN'] || ENV.fetch('LOCAL_DOMAIN', nil)
|
||||
REFURL_EXP = /(RT|QT|BT|RN|RE)((:|;)?\s+|:|;)(#{URI::DEFAULT_PARSER.make_regexp(%w(http https))})/
|
||||
MAX_REFERENCES = 5
|
||||
|
||||
def call(status, reference_parameters, urls: nil)
|
||||
@status = status
|
||||
@reference_parameters = reference_parameters || []
|
||||
@urls = urls || []
|
||||
|
||||
old_references
|
||||
@references_count = old_references.size
|
||||
|
||||
return unless added_references.size.positive? || removed_references.size.positive?
|
||||
|
||||
|
@ -63,6 +64,9 @@ class ProcessReferencesService < BaseService
|
|||
statuses.each do |status|
|
||||
@added_objects << @status.reference_objects.new(target_status: status)
|
||||
status.increment_count!(:status_referred_by_count)
|
||||
@references_count += 1
|
||||
|
||||
break if @references_count >= MAX_REFERENCES
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -85,6 +89,7 @@ class ProcessReferencesService < BaseService
|
|||
@status.reference_objects.where(target_status: statuses).destroy_all
|
||||
statuses.each do |status|
|
||||
status.decrement_count!(:status_referred_by_count)
|
||||
@references_count -= 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,7 +9,6 @@ class ActivityPub::FetchInstanceInfoWorker
|
|||
sidekiq_options queue: 'push', retry: 2
|
||||
|
||||
class Error < StandardError; end
|
||||
class GoneError < Error; end
|
||||
class RequestError < Error; end
|
||||
class DeadError < Error; end
|
||||
|
||||
|
@ -68,9 +67,7 @@ class ActivityPub::FetchInstanceInfoWorker
|
|||
raise Mastodon::UnexpectedResponseError, response unless response_successful?(response) || response_error_unsalvageable?(response)
|
||||
|
||||
body_to_json(response.body_with_limit)
|
||||
elsif response.code == 410
|
||||
raise ActivityPub::FetchInstanceInfoWorker::GoneError, "#{@instance.domain} is gone from the server"
|
||||
elsif response.code == 404
|
||||
elsif [400, 401, 403, 404, 410].include?(response.code)
|
||||
raise ActivityPub::FetchInstanceInfoWorker::DeadError, "Request for #{@instance.domain} returned HTTP #{response.code}"
|
||||
else
|
||||
raise ActivityPub::FetchInstanceInfoWorker::RequestError, "Request for #{@instance.domain} returned HTTP #{response.code}"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue