闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
3.1 KiB

  1. # frozen_string_literal: true
  2. class AccountRelationshipsPresenter
  3. attr_reader :following, :followed_by, :blocking, :blocked_by,
  4. :muting, :requested, :domain_blocking,
  5. :endorsed
  6. def initialize(account_ids, current_account_id, **options)
  7. @account_ids = account_ids.map { |a| a.is_a?(Account) ? a.id : a.to_i }
  8. @current_account_id = current_account_id
  9. @following = cached[:following].merge(Account.following_map(@uncached_account_ids, @current_account_id))
  10. @followed_by = cached[:followed_by].merge(Account.followed_by_map(@uncached_account_ids, @current_account_id))
  11. @blocking = cached[:blocking].merge(Account.blocking_map(@uncached_account_ids, @current_account_id))
  12. @blocked_by = cached[:blocked_by].merge(Account.blocked_by_map(@uncached_account_ids, @current_account_id))
  13. @muting = cached[:muting].merge(Account.muting_map(@uncached_account_ids, @current_account_id))
  14. @requested = cached[:requested].merge(Account.requested_map(@uncached_account_ids, @current_account_id))
  15. @domain_blocking = cached[:domain_blocking].merge(Account.domain_blocking_map(@uncached_account_ids, @current_account_id))
  16. @endorsed = cached[:endorsed].merge(Account.endorsed_map(@uncached_account_ids, @current_account_id))
  17. cache_uncached!
  18. @following.merge!(options[:following_map] || {})
  19. @followed_by.merge!(options[:followed_by_map] || {})
  20. @blocking.merge!(options[:blocking_map] || {})
  21. @blocked_by.merge!(options[:blocked_by_map] || {})
  22. @muting.merge!(options[:muting_map] || {})
  23. @requested.merge!(options[:requested_map] || {})
  24. @domain_blocking.merge!(options[:domain_blocking_map] || {})
  25. @endorsed.merge!(options[:endorsed_map] || {})
  26. end
  27. private
  28. def cached
  29. return @cached if defined?(@cached)
  30. @cached = {
  31. following: {},
  32. followed_by: {},
  33. blocking: {},
  34. blocked_by: {},
  35. muting: {},
  36. requested: {},
  37. domain_blocking: {},
  38. endorsed: {},
  39. }
  40. @uncached_account_ids = []
  41. @account_ids.each do |account_id|
  42. maps_for_account = Rails.cache.read("relationship:#{@current_account_id}:#{account_id}")
  43. if maps_for_account.is_a?(Hash)
  44. @cached.deep_merge!(maps_for_account)
  45. else
  46. @uncached_account_ids << account_id
  47. end
  48. end
  49. @cached
  50. end
  51. def cache_uncached!
  52. @uncached_account_ids.each do |account_id|
  53. maps_for_account = {
  54. following: { account_id => following[account_id] },
  55. followed_by: { account_id => followed_by[account_id] },
  56. blocking: { account_id => blocking[account_id] },
  57. blocked_by: { account_id => blocked_by[account_id] },
  58. muting: { account_id => muting[account_id] },
  59. requested: { account_id => requested[account_id] },
  60. domain_blocking: { account_id => domain_blocking[account_id] },
  61. endorsed: { account_id => endorsed[account_id] },
  62. }
  63. Rails.cache.write("relationship:#{@current_account_id}:#{account_id}", maps_for_account, expires_in: 1.day)
  64. end
  65. end
  66. end