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