status_relationships_presenter.rb 2.0 KB

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