interactions.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # frozen_string_literal: true
  2. module Account::Interactions
  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_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. with_options class_name: 'Follow', dependent: :destroy do
  64. has_many :active_relationships, foreign_key: 'account_id', inverse_of: :account
  65. has_many :passive_relationships, foreign_key: 'target_account_id', inverse_of: :target_account
  66. end
  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. with_options class_name: 'Block', dependent: :destroy do
  73. has_many :block_relationships, foreign_key: 'account_id', inverse_of: :account
  74. has_many :blocked_by_relationships, foreign_key: :target_account_id, inverse_of: :target_account
  75. end
  76. has_many :blocking, -> { order('blocks.id desc') }, through: :block_relationships, source: :target_account
  77. has_many :blocked_by, -> { order('blocks.id desc') }, through: :blocked_by_relationships, source: :account
  78. # Mute relationships
  79. with_options class_name: 'Mute', dependent: :destroy do
  80. has_many :mute_relationships, foreign_key: 'account_id', inverse_of: :account
  81. has_many :muted_by_relationships, foreign_key: :target_account_id, inverse_of: :target_account
  82. end
  83. has_many :muting, -> { order('mutes.id desc') }, through: :mute_relationships, source: :target_account
  84. has_many :muted_by, -> { order('mutes.id desc') }, through: :muted_by_relationships, source: :account
  85. has_many :conversation_mutes, dependent: :destroy
  86. has_many :domain_blocks, class_name: 'AccountDomainBlock', dependent: :destroy
  87. has_many :announcement_mutes, dependent: :destroy
  88. end
  89. def follow!(other_account, reblogs: nil, notify: nil, languages: nil, uri: nil, rate_limit: false, bypass_limit: false)
  90. 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)
  91. .find_or_create_by!(target_account: other_account)
  92. rel.show_reblogs = reblogs unless reblogs.nil?
  93. rel.notify = notify unless notify.nil?
  94. rel.languages = languages unless languages.nil?
  95. rel.save! if rel.changed?
  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. rel
  106. end
  107. def block!(other_account, uri: nil)
  108. block_relationships.create_with(uri: uri)
  109. .find_or_create_by!(target_account: other_account)
  110. end
  111. def mute!(other_account, notifications: nil, duration: 0)
  112. notifications = true if notifications.nil?
  113. mute = mute_relationships.create_with(hide_notifications: notifications).find_or_initialize_by(target_account: other_account)
  114. mute.expires_in = duration.zero? ? nil : duration
  115. mute.save!
  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. mute.update!(hide_notifications: notifications) if mute.hide_notifications? != notifications
  118. mute
  119. end
  120. def mute_conversation!(conversation)
  121. conversation_mutes.find_or_create_by!(conversation: conversation)
  122. end
  123. def block_domain!(other_domain)
  124. domain_blocks.find_or_create_by!(domain: other_domain)
  125. end
  126. def unfollow!(other_account)
  127. follow = active_relationships.find_by(target_account: other_account)
  128. follow&.destroy
  129. end
  130. def unblock!(other_account)
  131. block = block_relationships.find_by(target_account: other_account)
  132. block&.destroy
  133. end
  134. def unmute!(other_account)
  135. mute = mute_relationships.find_by(target_account: other_account)
  136. mute&.destroy
  137. end
  138. def unmute_conversation!(conversation)
  139. mute = conversation_mutes.find_by(conversation: conversation)
  140. mute&.destroy!
  141. end
  142. def unblock_domain!(other_domain)
  143. block = domain_blocks.find_by(domain: other_domain)
  144. block&.destroy
  145. end
  146. def following?(other_account)
  147. active_relationships.where(target_account: other_account).exists?
  148. end
  149. def following_anyone?
  150. active_relationships.exists?
  151. end
  152. def not_following_anyone?
  153. !following_anyone?
  154. end
  155. def followed_by?(other_account)
  156. other_account.following?(self)
  157. end
  158. def blocking?(other_account)
  159. block_relationships.where(target_account: other_account).exists?
  160. end
  161. def domain_blocking?(other_domain)
  162. domain_blocks.where(domain: other_domain).exists?
  163. end
  164. def muting?(other_account)
  165. mute_relationships.where(target_account: other_account).exists?
  166. end
  167. def muting_conversation?(conversation)
  168. conversation_mutes.where(conversation: conversation).exists?
  169. end
  170. def muting_notifications?(other_account)
  171. mute_relationships.where(target_account: other_account, hide_notifications: true).exists?
  172. end
  173. def muting_reblogs?(other_account)
  174. active_relationships.where(target_account: other_account, show_reblogs: false).exists?
  175. end
  176. def requested?(other_account)
  177. follow_requests.where(target_account: other_account).exists?
  178. end
  179. def favourited?(status)
  180. status.proper.favourites.where(account: self).exists?
  181. end
  182. def bookmarked?(status)
  183. status.proper.bookmarks.where(account: self).exists?
  184. end
  185. def reblogged?(status)
  186. status.proper.reblogs.where(account: self).exists?
  187. end
  188. def pinned?(status)
  189. status_pins.where(status: status).exists?
  190. end
  191. def endorsed?(account)
  192. account_pins.where(target_account: account).exists?
  193. end
  194. def status_matches_filters(status)
  195. active_filters = CustomFilter.cached_filters_for(id)
  196. CustomFilter.apply_cached_filters(active_filters, status)
  197. end
  198. def followers_for_local_distribution
  199. followers.local
  200. .joins(:user)
  201. .where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago)
  202. end
  203. def lists_for_local_distribution
  204. scope = lists.joins(account: :user)
  205. scope.where.not(list_accounts: { follow_id: nil }).or(scope.where(account_id: id))
  206. .where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago)
  207. end
  208. def remote_followers_hash(url)
  209. url_prefix = url[Account::URL_PREFIX_RE]
  210. return if url_prefix.blank?
  211. Rails.cache.fetch("followers_hash:#{id}:#{url_prefix}/") do
  212. digest = "\x00" * 32
  213. 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|
  214. Xorcist.xor!(digest, Digest::SHA256.digest(uri))
  215. end
  216. digest.unpack1('H*')
  217. end
  218. end
  219. def local_followers_hash
  220. Rails.cache.fetch("followers_hash:#{id}:local") do
  221. digest = "\x00" * 32
  222. followers.where(domain: nil).pluck_each(:username) do |username|
  223. Xorcist.xor!(digest, Digest::SHA256.digest(ActivityPub::TagManager.instance.uri_for_username(username)))
  224. end
  225. digest.unpack1('H*')
  226. end
  227. end
  228. def relations_map(account_ids, domains = nil, **options)
  229. relations = {
  230. blocked_by: Account.blocked_by_map(account_ids, id),
  231. following: Account.following_map(account_ids, id),
  232. }
  233. return relations if options[:skip_blocking_and_muting]
  234. relations.merge!({
  235. blocking: Account.blocking_map(account_ids, id),
  236. muting: Account.muting_map(account_ids, id),
  237. domain_blocking_by_domain: Account.domain_blocking_map_by_domain(domains, id),
  238. })
  239. end
  240. end