account_interactions.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # frozen_string_literal: true
  2. module AccountInteractions
  3. extend ActiveSupport::Concern
  4. class_methods do
  5. def following_map(target_account_ids, account_id)
  6. Follow.where(target_account_id: target_account_ids, account_id: account_id).each_with_object({}) do |follow, mapping|
  7. mapping[follow.target_account_id] = {
  8. reblogs: follow.show_reblogs?,
  9. }
  10. end
  11. end
  12. def followed_by_map(target_account_ids, account_id)
  13. follow_mapping(Follow.where(account_id: target_account_ids, target_account_id: account_id), :account_id)
  14. end
  15. def blocking_map(target_account_ids, account_id)
  16. follow_mapping(Block.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id)
  17. end
  18. def blocked_by_map(target_account_ids, account_id)
  19. follow_mapping(Block.where(account_id: target_account_ids, target_account_id: account_id), :account_id)
  20. end
  21. def muting_map(target_account_ids, account_id)
  22. Mute.where(target_account_id: target_account_ids, account_id: account_id).each_with_object({}) do |mute, mapping|
  23. mapping[mute.target_account_id] = {
  24. notifications: mute.hide_notifications?,
  25. }
  26. end
  27. end
  28. def requested_map(target_account_ids, account_id)
  29. FollowRequest.where(target_account_id: target_account_ids, account_id: account_id).each_with_object({}) do |follow_request, mapping|
  30. mapping[follow_request.target_account_id] = {
  31. reblogs: follow_request.show_reblogs?,
  32. }
  33. end
  34. end
  35. def domain_blocking_map(target_account_ids, account_id)
  36. accounts_map = Account.where(id: target_account_ids).select('id, domain').map { |a| [a.id, a.domain] }.to_h
  37. blocked_domains = domain_blocking_map_by_domain(accounts_map.values.compact, account_id)
  38. accounts_map.map { |id, domain| [id, blocked_domains[domain]] }.to_h
  39. end
  40. def domain_blocking_map_by_domain(target_domains, account_id)
  41. follow_mapping(AccountDomainBlock.where(account_id: account_id, domain: target_domains), :domain)
  42. end
  43. private
  44. def follow_mapping(query, field)
  45. query.pluck(field).each_with_object({}) { |id, mapping| mapping[id] = true }
  46. end
  47. end
  48. included do
  49. # Follow relations
  50. has_many :follow_requests, dependent: :destroy
  51. has_many :active_relationships, class_name: 'Follow', foreign_key: 'account_id', dependent: :destroy
  52. has_many :passive_relationships, class_name: 'Follow', foreign_key: 'target_account_id', dependent: :destroy
  53. has_many :following, -> { order('follows.id desc') }, through: :active_relationships, source: :target_account
  54. has_many :followers, -> { order('follows.id desc') }, through: :passive_relationships, source: :account
  55. # Block relationships
  56. has_many :block_relationships, class_name: 'Block', foreign_key: 'account_id', dependent: :destroy
  57. has_many :blocking, -> { order('blocks.id desc') }, through: :block_relationships, source: :target_account
  58. has_many :blocked_by_relationships, class_name: 'Block', foreign_key: :target_account_id, dependent: :destroy
  59. has_many :blocked_by, -> { order('blocks.id desc') }, through: :blocked_by_relationships, source: :account
  60. # Mute relationships
  61. has_many :mute_relationships, class_name: 'Mute', foreign_key: 'account_id', dependent: :destroy
  62. has_many :muting, -> { order('mutes.id desc') }, through: :mute_relationships, source: :target_account
  63. has_many :muted_by_relationships, class_name: 'Mute', foreign_key: :target_account_id, dependent: :destroy
  64. has_many :muted_by, -> { order('mutes.id desc') }, through: :muted_by_relationships, source: :account
  65. has_many :conversation_mutes, dependent: :destroy
  66. has_many :domain_blocks, class_name: 'AccountDomainBlock', dependent: :destroy
  67. end
  68. def follow!(other_account, reblogs: nil)
  69. reblogs = true if reblogs.nil?
  70. rel = active_relationships.create_with(show_reblogs: reblogs).find_or_create_by!(target_account: other_account)
  71. rel.update!(show_reblogs: reblogs)
  72. rel
  73. end
  74. def block!(other_account)
  75. block_relationships.find_or_create_by!(target_account: other_account)
  76. end
  77. def mute!(other_account, notifications: nil)
  78. notifications = true if notifications.nil?
  79. mute = mute_relationships.create_with(hide_notifications: notifications).find_or_create_by!(target_account: other_account)
  80. # When toggling a mute between hiding and allowing notifications, the mute will already exist, so the find_or_create_by! call will return the existing Mute without updating the hide_notifications attribute. Therefore, we check that hide_notifications? is what we want and set it if it isn't.
  81. if mute.hide_notifications? != notifications
  82. mute.update!(hide_notifications: notifications)
  83. end
  84. mute
  85. end
  86. def mute_conversation!(conversation)
  87. conversation_mutes.find_or_create_by!(conversation: conversation)
  88. end
  89. def block_domain!(other_domain)
  90. domain_blocks.find_or_create_by!(domain: other_domain)
  91. end
  92. def unfollow!(other_account)
  93. follow = active_relationships.find_by(target_account: other_account)
  94. follow&.destroy
  95. end
  96. def unblock!(other_account)
  97. block = block_relationships.find_by(target_account: other_account)
  98. block&.destroy
  99. end
  100. def unmute!(other_account)
  101. mute = mute_relationships.find_by(target_account: other_account)
  102. mute&.destroy
  103. end
  104. def unmute_conversation!(conversation)
  105. mute = conversation_mutes.find_by(conversation: conversation)
  106. mute&.destroy!
  107. end
  108. def unblock_domain!(other_domain)
  109. block = domain_blocks.find_by(domain: other_domain)
  110. block&.destroy
  111. end
  112. def following?(other_account)
  113. active_relationships.where(target_account: other_account).exists?
  114. end
  115. def blocking?(other_account)
  116. block_relationships.where(target_account: other_account).exists?
  117. end
  118. def domain_blocking?(other_domain)
  119. domain_blocks.where(domain: other_domain).exists?
  120. end
  121. def muting?(other_account)
  122. mute_relationships.where(target_account: other_account).exists?
  123. end
  124. def muting_conversation?(conversation)
  125. conversation_mutes.where(conversation: conversation).exists?
  126. end
  127. def muting_notifications?(other_account)
  128. mute_relationships.where(target_account: other_account, hide_notifications: true).exists?
  129. end
  130. def muting_reblogs?(other_account)
  131. active_relationships.where(target_account: other_account, show_reblogs: false).exists?
  132. end
  133. def requested?(other_account)
  134. follow_requests.where(target_account: other_account).exists?
  135. end
  136. def favourited?(status)
  137. status.proper.favourites.where(account: self).exists?
  138. end
  139. def reblogged?(status)
  140. status.proper.reblogs.where(account: self).exists?
  141. end
  142. def pinned?(status)
  143. status_pins.where(status: status).exists?
  144. end
  145. end