logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

account_relationships_presenter.rb (2607B)


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