1
0

account_relationships_presenter.rb 3.6 KB

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