The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

77 lines
2.8 KiB

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