2017-07-07 02:02:06 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class StatusRelationshipsPresenter
|
2019-11-28 03:08:00 +00:00
|
|
|
attr_reader :reblogs_map, :favourites_map, :mutes_map, :pins_map,
|
2021-05-19 05:58:27 +00:00
|
|
|
:bookmarks_map, :emoji_reactions_map
|
2017-07-07 02:02:06 +00:00
|
|
|
|
2017-12-06 10:41:57 +00:00
|
|
|
def initialize(statuses, current_account_id = nil, **options)
|
2017-07-07 02:02:06 +00:00
|
|
|
if current_account_id.nil?
|
2021-05-19 05:58:27 +00:00
|
|
|
@reblogs_map = {}
|
|
|
|
@favourites_map = {}
|
|
|
|
@bookmarks_map = {}
|
|
|
|
@emoji_reactions_map = {}
|
|
|
|
@mutes_map = {}
|
|
|
|
@pins_map = {}
|
2017-07-07 02:02:06 +00:00
|
|
|
else
|
2021-07-16 06:38:50 +00:00
|
|
|
statuses = Status.where(id: statuses) if statuses.first.is_a?(Integer)
|
2017-08-24 23:41:18 +00:00
|
|
|
statuses = statuses.compact
|
2017-09-24 09:18:32 +00:00
|
|
|
status_ids = statuses.flat_map { |s| [s.id, s.reblog_of_id] }.uniq.compact
|
2021-01-09 23:32:01 +00:00
|
|
|
conversation_ids = statuses.filter_map(&:conversation_id).uniq
|
2022-01-16 23:49:55 +00:00
|
|
|
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) }
|
2017-08-24 23:41:18 +00:00
|
|
|
|
2021-07-03 09:48:27 +00:00
|
|
|
result = ActiveRecord::Base.connection.select_all(ActiveRecord::Base.sanitize_sql_array([<<-SQL.squish, account_id: current_account_id, status_ids: status_ids, conversation_ids: conversation_ids, pinnable_status_ids: pinnable_status_ids])).to_a.first
|
|
|
|
select
|
|
|
|
(select string_agg(reblog_of_id::text, ',') from statuses where account_id = :account_id and reblog_of_id in (:status_ids)) as reblogs,
|
|
|
|
(select string_agg(status_id::text, ',') from favourites where account_id = :account_id and status_id IN (:status_ids)) as favourites,
|
|
|
|
(select string_agg(status_id::text, ',') from bookmarks where account_id = :account_id and status_id in (:status_ids)) as bookmarks,
|
|
|
|
(select string_agg(status_id::text, ',') from emoji_reactions where account_id = :account_id and status_id in (:status_ids)) as emoji_reactions,
|
|
|
|
(select string_agg(conversation_id::text, ',') from conversation_mutes where account_id = :account_id and conversation_id in (:conversation_ids)) as mutes,
|
|
|
|
(select string_agg(status_id::text, ',') from status_pins where account_id = :account_id and status_id in (:pinnable_status_ids)) as pins
|
|
|
|
SQL
|
|
|
|
|
|
|
|
@reblogs_map = mapping(result['reblogs'], options[:reblogs_map])
|
|
|
|
@favourites_map = mapping(result['favourites'], options[:favourites_map])
|
|
|
|
@bookmarks_map = mapping(result['bookmarks'], options[:bookmarks_map])
|
|
|
|
@emoji_reactions_map = mapping(result['emoji_reactions'], options[:emoji_reactions_map])
|
|
|
|
@mutes_map = mapping(result['mutes'], options[:mutes_map])
|
|
|
|
@pins_map = mapping(result['pins'], options[:pins_map])
|
2017-07-07 02:02:06 +00:00
|
|
|
end
|
|
|
|
end
|
2021-07-03 09:48:27 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def mapping(result, additional)
|
|
|
|
(result&.split(',')&.map(&:to_i)&.index_with(true) || {}).merge(additional || {})
|
|
|
|
end
|
2017-07-07 02:02:06 +00:00
|
|
|
end
|