account_interactions.rb 11 KB

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