account_relationships_presenter.rb 3.4 KB

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