status_relationships_presenter.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # frozen_string_literal: true
  2. class StatusRelationshipsPresenter
  3. attr_reader :reblogs_map, :favourites_map, :mutes_map, :pins_map,
  4. :bookmarks_map, :filters_map
  5. def initialize(statuses, current_account_id = nil, **options)
  6. if current_account_id.nil?
  7. @reblogs_map = {}
  8. @favourites_map = {}
  9. @bookmarks_map = {}
  10. @mutes_map = {}
  11. @pins_map = {}
  12. @filters_map = {}
  13. else
  14. statuses = statuses.compact
  15. status_ids = statuses.flat_map { |s| [s.id, s.reblog_of_id] }.uniq.compact
  16. conversation_ids = statuses.filter_map(&:conversation_id).uniq
  17. 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) }
  18. @filters_map = build_filters_map(statuses, current_account_id).merge(options[:filters_map] || {})
  19. @reblogs_map = Status.reblogs_map(status_ids, current_account_id).merge(options[:reblogs_map] || {})
  20. @favourites_map = Status.favourites_map(status_ids, current_account_id).merge(options[:favourites_map] || {})
  21. @bookmarks_map = Status.bookmarks_map(status_ids, current_account_id).merge(options[:bookmarks_map] || {})
  22. @mutes_map = Status.mutes_map(conversation_ids, current_account_id).merge(options[:mutes_map] || {})
  23. @pins_map = Status.pins_map(pinnable_status_ids, current_account_id).merge(options[:pins_map] || {})
  24. end
  25. end
  26. private
  27. def build_filters_map(statuses, current_account_id)
  28. active_filters = CustomFilter.cached_filters_for(current_account_id)
  29. @filters_map = statuses.each_with_object({}) do |status, h|
  30. filter_matches = CustomFilter.apply_cached_filters(active_filters, status)
  31. unless filter_matches.empty?
  32. h[status.id] = filter_matches
  33. h[status.reblog_of_id] = filter_matches if status.reblog?
  34. end
  35. end
  36. end
  37. end