1
0

account_relationships_presenter.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # frozen_string_literal: true
  2. class AccountRelationshipsPresenter
  3. attr_reader :following, :followed_by, :blocking, :blocked_by,
  4. :muting, :requested, :requested_by, :domain_blocking,
  5. :endorsed, :account_note
  6. def initialize(accounts, current_account_id, **options)
  7. @accounts = accounts.to_a
  8. @account_ids = @accounts.pluck(:id)
  9. @current_account_id = current_account_id
  10. @following = cached[:following].merge(Account.following_map(@uncached_account_ids, @current_account_id))
  11. @followed_by = cached[:followed_by].merge(Account.followed_by_map(@uncached_account_ids, @current_account_id))
  12. @blocking = cached[:blocking].merge(Account.blocking_map(@uncached_account_ids, @current_account_id))
  13. @blocked_by = cached[:blocked_by].merge(Account.blocked_by_map(@uncached_account_ids, @current_account_id))
  14. @muting = cached[:muting].merge(Account.muting_map(@uncached_account_ids, @current_account_id))
  15. @requested = cached[:requested].merge(Account.requested_map(@uncached_account_ids, @current_account_id))
  16. @requested_by = cached[:requested_by].merge(Account.requested_by_map(@uncached_account_ids, @current_account_id))
  17. @endorsed = cached[:endorsed].merge(Account.endorsed_map(@uncached_account_ids, @current_account_id))
  18. @account_note = cached[:account_note].merge(Account.account_note_map(@uncached_account_ids, @current_account_id))
  19. @domain_blocking = domain_blocking_map
  20. cache_uncached!
  21. @following.merge!(options[:following_map] || {})
  22. @followed_by.merge!(options[:followed_by_map] || {})
  23. @blocking.merge!(options[:blocking_map] || {})
  24. @blocked_by.merge!(options[:blocked_by_map] || {})
  25. @muting.merge!(options[:muting_map] || {})
  26. @requested.merge!(options[:requested_map] || {})
  27. @requested_by.merge!(options[:requested_by_map] || {})
  28. @domain_blocking.merge!(options[:domain_blocking_map] || {})
  29. @endorsed.merge!(options[:endorsed_map] || {})
  30. @account_note.merge!(options[:account_note_map] || {})
  31. end
  32. private
  33. def domain_blocking_map
  34. target_domains = @accounts.pluck(:domain).compact.uniq
  35. blocks_by_domain = {}
  36. # Fetch from cache
  37. cache_keys = target_domains.map { |domain| domain_cache_key(domain) }
  38. Rails.cache.read_multi(*cache_keys).each do |key, blocking|
  39. blocks_by_domain[key.last] = blocking
  40. end
  41. uncached_domains = target_domains - blocks_by_domain.keys
  42. # Read uncached values from database
  43. AccountDomainBlock.where(account_id: @current_account_id, domain: uncached_domains).pluck(:domain).each do |domain|
  44. blocks_by_domain[domain] = true
  45. end
  46. # Write database reads to cache
  47. to_cache = uncached_domains.to_h { |domain| [domain_cache_key(domain), blocks_by_domain[domain]] }
  48. Rails.cache.write_multi(to_cache, expires_in: 1.day)
  49. # Return formatted value
  50. @accounts.each_with_object({}) { |account, h| h[account.id] = blocks_by_domain[account.domain] }
  51. end
  52. def cached
  53. return @cached if defined?(@cached)
  54. @cached = {
  55. following: {},
  56. followed_by: {},
  57. blocking: {},
  58. blocked_by: {},
  59. muting: {},
  60. requested: {},
  61. requested_by: {},
  62. endorsed: {},
  63. account_note: {},
  64. }
  65. @uncached_account_ids = @account_ids.uniq
  66. cache_ids = @account_ids.map { |account_id| relationship_cache_key(account_id) }
  67. Rails.cache.read_multi(*cache_ids).each do |key, maps_for_account|
  68. @cached.deep_merge!(maps_for_account)
  69. @uncached_account_ids.delete(key.last)
  70. end
  71. @cached
  72. end
  73. def cache_uncached!
  74. to_cache = @uncached_account_ids.to_h do |account_id|
  75. maps_for_account = {
  76. following: { account_id => following[account_id] },
  77. followed_by: { account_id => followed_by[account_id] },
  78. blocking: { account_id => blocking[account_id] },
  79. blocked_by: { account_id => blocked_by[account_id] },
  80. muting: { account_id => muting[account_id] },
  81. requested: { account_id => requested[account_id] },
  82. requested_by: { account_id => requested_by[account_id] },
  83. endorsed: { account_id => endorsed[account_id] },
  84. account_note: { account_id => account_note[account_id] },
  85. }
  86. [relationship_cache_key(account_id), maps_for_account]
  87. end
  88. Rails.cache.write_multi(to_cache, expires_in: 1.day)
  89. end
  90. def domain_cache_key(domain)
  91. ['exclude_domains', @current_account_id, domain]
  92. end
  93. def relationship_cache_key(account_id)
  94. ['relationship', @current_account_id, account_id]
  95. end
  96. end