account_interactions.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. notify: follow.notify?,
  10. languages: follow.languages,
  11. }
  12. end
  13. end
  14. def followed_by_map(target_account_ids, account_id)
  15. follow_mapping(Follow.where(account_id: target_account_ids, target_account_id: account_id), :account_id)
  16. end
  17. def blocking_map(target_account_ids, account_id)
  18. follow_mapping(Block.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id)
  19. end
  20. def blocked_by_map(target_account_ids, account_id)
  21. follow_mapping(Block.where(account_id: target_account_ids, target_account_id: account_id), :account_id)
  22. end
  23. def muting_map(target_account_ids, account_id)
  24. Mute.where(target_account_id: target_account_ids, account_id: account_id).each_with_object({}) do |mute, mapping|
  25. mapping[mute.target_account_id] = {
  26. notifications: mute.hide_notifications?,
  27. }
  28. end
  29. end
  30. def requested_map(target_account_ids, account_id)
  31. FollowRequest.where(target_account_id: target_account_ids, account_id: account_id).each_with_object({}) do |follow_request, mapping|
  32. mapping[follow_request.target_account_id] = {
  33. reblogs: follow_request.show_reblogs?,
  34. notify: follow_request.notify?,
  35. languages: follow_request.languages,
  36. }
  37. end
  38. end
  39. def endorsed_map(target_account_ids, account_id)
  40. follow_mapping(AccountPin.where(account_id: account_id, target_account_id: target_account_ids), :target_account_id)
  41. end
  42. def account_note_map(target_account_ids, account_id)
  43. AccountNote.where(target_account_id: target_account_ids, account_id: account_id).each_with_object({}) do |note, mapping|
  44. mapping[note.target_account_id] = {
  45. comment: note.comment,
  46. }
  47. end
  48. end
  49. def domain_blocking_map(target_account_ids, account_id)
  50. accounts_map = Account.where(id: target_account_ids).select('id, domain').each_with_object({}) { |a, h| h[a.id] = a.domain }
  51. blocked_domains = domain_blocking_map_by_domain(accounts_map.values.compact, account_id)
  52. accounts_map.reduce({}) { |h, (id, domain)| h.merge(id => blocked_domains[domain]) }
  53. end
  54. def domain_blocking_map_by_domain(target_domains, account_id)
  55. follow_mapping(AccountDomainBlock.where(account_id: account_id, domain: target_domains), :domain)
  56. end
  57. private
  58. def follow_mapping(query, field)
  59. query.pluck(field).index_with(true)
  60. end
  61. end
  62. included do
  63. # Follow relations
  64. has_many :follow_requests, dependent: :destroy
  65. has_many :active_relationships, class_name: 'Follow', foreign_key: 'account_id', dependent: :destroy
  66. has_many :passive_relationships, class_name: 'Follow', foreign_key: 'target_account_id', dependent: :destroy
  67. has_many :following, -> { order('follows.id desc') }, through: :active_relationships, source: :target_account
  68. has_many :followers, -> { order('follows.id desc') }, through: :passive_relationships, source: :account
  69. # Account notes
  70. has_many :account_notes, dependent: :destroy
  71. # Block relationships
  72. has_many :block_relationships, class_name: 'Block', foreign_key: 'account_id', dependent: :destroy
  73. has_many :blocking, -> { order('blocks.id desc') }, through: :block_relationships, source: :target_account
  74. has_many :blocked_by_relationships, class_name: 'Block', foreign_key: :target_account_id, dependent: :destroy
  75. has_many :blocked_by, -> { order('blocks.id desc') }, through: :blocked_by_relationships, source: :account
  76. # Mute relationships
  77. has_many :mute_relationships, class_name: 'Mute', foreign_key: 'account_id', dependent: :destroy
  78. has_many :muting, -> { order('mutes.id desc') }, through: :mute_relationships, source: :target_account
  79. has_many :muted_by_relationships, class_name: 'Mute', foreign_key: :target_account_id, dependent: :destroy
  80. has_many :muted_by, -> { order('mutes.id desc') }, through: :muted_by_relationships, source: :account
  81. has_many :conversation_mutes, dependent: :destroy
  82. has_many :domain_blocks, class_name: 'AccountDomainBlock', dependent: :destroy
  83. has_many :announcement_mutes, dependent: :destroy
  84. end
  85. def follow!(other_account, reblogs: nil, notify: nil, languages: nil, uri: nil, rate_limit: false, bypass_limit: false)
  86. rel = active_relationships.create_with(show_reblogs: reblogs.nil? ? true : reblogs, notify: notify.nil? ? false : notify, languages: languages, uri: uri, rate_limit: rate_limit, bypass_follow_limit: bypass_limit)
  87. .find_or_create_by!(target_account: other_account)
  88. rel.show_reblogs = reblogs unless reblogs.nil?
  89. rel.notify = notify unless notify.nil?
  90. rel.languages = languages unless languages.nil?
  91. rel.save! if rel.changed?
  92. remove_potential_friendship(other_account)
  93. rel
  94. end
  95. def request_follow!(other_account, reblogs: nil, notify: nil, languages: nil, uri: nil, rate_limit: false, bypass_limit: false)
  96. rel = follow_requests.create_with(show_reblogs: reblogs.nil? ? true : reblogs, notify: notify.nil? ? false : notify, uri: uri, languages: languages, rate_limit: rate_limit, bypass_follow_limit: bypass_limit)
  97. .find_or_create_by!(target_account: other_account)
  98. rel.show_reblogs = reblogs unless reblogs.nil?
  99. rel.notify = notify unless notify.nil?
  100. rel.languages = languages unless languages.nil?
  101. rel.save! if rel.changed?
  102. remove_potential_friendship(other_account)
  103. rel
  104. end
  105. def block!(other_account, uri: nil)
  106. remove_potential_friendship(other_account)
  107. block_relationships.create_with(uri: uri)
  108. .find_or_create_by!(target_account: other_account)
  109. end
  110. def mute!(other_account, notifications: nil, duration: 0)
  111. notifications = true if notifications.nil?
  112. mute = mute_relationships.create_with(hide_notifications: notifications).find_or_initialize_by(target_account: other_account)
  113. mute.expires_in = duration.zero? ? nil : duration
  114. mute.save!
  115. remove_potential_friendship(other_account)
  116. # 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.
  117. if mute.hide_notifications? != notifications
  118. mute.update!(hide_notifications: notifications)
  119. end
  120. mute
  121. end
  122. def mute_conversation!(conversation)
  123. conversation_mutes.find_or_create_by!(conversation: conversation)
  124. end
  125. def block_domain!(other_domain)
  126. domain_blocks.find_or_create_by!(domain: other_domain)
  127. end
  128. def unfollow!(other_account)
  129. follow = active_relationships.find_by(target_account: other_account)
  130. follow&.destroy
  131. end
  132. def unblock!(other_account)
  133. block = block_relationships.find_by(target_account: other_account)
  134. block&.destroy
  135. end
  136. def unmute!(other_account)
  137. mute = mute_relationships.find_by(target_account: other_account)
  138. mute&.destroy
  139. end
  140. def unmute_conversation!(conversation)
  141. mute = conversation_mutes.find_by(conversation: conversation)
  142. mute&.destroy!
  143. end
  144. def unblock_domain!(other_domain)
  145. block = domain_blocks.find_by(domain: other_domain)
  146. block&.destroy
  147. end
  148. def following?(other_account)
  149. active_relationships.where(target_account: other_account).exists?
  150. end
  151. def following_anyone?
  152. active_relationships.exists?
  153. end
  154. def not_following_anyone?
  155. !following_anyone?
  156. end
  157. def followed_by?(other_account)
  158. other_account.following?(self)
  159. end
  160. def blocking?(other_account)
  161. block_relationships.where(target_account: other_account).exists?
  162. end
  163. def domain_blocking?(other_domain)
  164. domain_blocks.where(domain: other_domain).exists?
  165. end
  166. def muting?(other_account)
  167. mute_relationships.where(target_account: other_account).exists?
  168. end
  169. def muting_conversation?(conversation)
  170. conversation_mutes.where(conversation: conversation).exists?
  171. end
  172. def muting_notifications?(other_account)
  173. mute_relationships.where(target_account: other_account, hide_notifications: true).exists?
  174. end
  175. def muting_reblogs?(other_account)
  176. active_relationships.where(target_account: other_account, show_reblogs: false).exists?
  177. end
  178. def requested?(other_account)
  179. follow_requests.where(target_account: other_account).exists?
  180. end
  181. def favourited?(status)
  182. status.proper.favourites.where(account: self).exists?
  183. end
  184. def bookmarked?(status)
  185. status.proper.bookmarks.where(account: self).exists?
  186. end
  187. def reblogged?(status)
  188. status.proper.reblogs.where(account: self).exists?
  189. end
  190. def pinned?(status)
  191. status_pins.where(status: status).exists?
  192. end
  193. def endorsed?(account)
  194. account_pins.where(target_account: account).exists?
  195. end
  196. def status_matches_filters(status)
  197. active_filters = CustomFilter.cached_filters_for(id)
  198. CustomFilter.apply_cached_filters(active_filters, status)
  199. end
  200. def followers_for_local_distribution
  201. followers.local
  202. .joins(:user)
  203. .where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago)
  204. end
  205. def lists_for_local_distribution
  206. lists.joins(account: :user)
  207. .where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago)
  208. end
  209. def remote_followers_hash(url)
  210. url_prefix = url[Account::URL_PREFIX_RE]
  211. return if url_prefix.blank?
  212. Rails.cache.fetch("followers_hash:#{id}:#{url_prefix}/") do
  213. digest = "\x00" * 32
  214. followers.where(Account.arel_table[:uri].matches("#{Account.sanitize_sql_like(url_prefix)}/%", false, true)).or(followers.where(uri: url_prefix)).pluck_each(:uri) do |uri|
  215. Xorcist.xor!(digest, Digest::SHA256.digest(uri))
  216. end
  217. digest.unpack('H*')[0]
  218. end
  219. end
  220. def local_followers_hash
  221. Rails.cache.fetch("followers_hash:#{id}:local") do
  222. digest = "\x00" * 32
  223. followers.where(domain: nil).pluck_each(:username) do |username|
  224. Xorcist.xor!(digest, Digest::SHA256.digest(ActivityPub::TagManager.instance.uri_for_username(username)))
  225. end
  226. digest.unpack('H*')[0]
  227. end
  228. end
  229. private
  230. def remove_potential_friendship(other_account)
  231. PotentialFriendshipTracker.remove(id, other_account.id)
  232. end
  233. end