logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

account_interactions.rb (6525B)


  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 muting_map(target_account_ids, account_id)
  19. Mute.where(target_account_id: target_account_ids, account_id: account_id).each_with_object({}) do |mute, mapping|
  20. mapping[mute.target_account_id] = {
  21. notifications: mute.hide_notifications?,
  22. }
  23. end
  24. end
  25. def requested_map(target_account_ids, account_id)
  26. FollowRequest.where(target_account_id: target_account_ids, account_id: account_id).each_with_object({}) do |follow_request, mapping|
  27. mapping[follow_request.target_account_id] = {
  28. reblogs: follow_request.show_reblogs?,
  29. }
  30. end
  31. end
  32. def domain_blocking_map(target_account_ids, account_id)
  33. accounts_map = Account.where(id: target_account_ids).select('id, domain').map { |a| [a.id, a.domain] }.to_h
  34. blocked_domains = AccountDomainBlock.where(account_id: account_id, domain: accounts_map.values).pluck(:domain)
  35. accounts_map.map { |id, domain| [id, blocked_domains.include?(domain)] }.to_h
  36. end
  37. private
  38. def follow_mapping(query, field)
  39. query.pluck(field).each_with_object({}) { |id, mapping| mapping[id] = true }
  40. end
  41. end
  42. included do
  43. # Follow relations
  44. has_many :follow_requests, dependent: :destroy
  45. has_many :active_relationships, class_name: 'Follow', foreign_key: 'account_id', dependent: :destroy
  46. has_many :passive_relationships, class_name: 'Follow', foreign_key: 'target_account_id', dependent: :destroy
  47. has_many :following, -> { order('follows.id desc') }, through: :active_relationships, source: :target_account
  48. has_many :followers, -> { order('follows.id desc') }, through: :passive_relationships, source: :account
  49. # Block relationships
  50. has_many :block_relationships, class_name: 'Block', foreign_key: 'account_id', dependent: :destroy
  51. has_many :blocking, -> { order('blocks.id desc') }, through: :block_relationships, source: :target_account
  52. has_many :blocked_by_relationships, class_name: 'Block', foreign_key: :target_account_id, dependent: :destroy
  53. has_many :blocked_by, -> { order('blocks.id desc') }, through: :blocked_by_relationships, source: :account
  54. # Mute relationships
  55. has_many :mute_relationships, class_name: 'Mute', foreign_key: 'account_id', dependent: :destroy
  56. has_many :muting, -> { order('mutes.id desc') }, through: :mute_relationships, source: :target_account
  57. has_many :muted_by_relationships, class_name: 'Mute', foreign_key: :target_account_id, dependent: :destroy
  58. has_many :muted_by, -> { order('mutes.id desc') }, through: :muted_by_relationships, source: :account
  59. has_many :conversation_mutes, dependent: :destroy
  60. has_many :domain_blocks, class_name: 'AccountDomainBlock', dependent: :destroy
  61. end
  62. def follow!(other_account, reblogs: nil)
  63. reblogs = true if reblogs.nil?
  64. rel = active_relationships.create_with(show_reblogs: reblogs).find_or_create_by!(target_account: other_account)
  65. rel.update!(show_reblogs: reblogs)
  66. rel
  67. end
  68. def block!(other_account)
  69. block_relationships.find_or_create_by!(target_account: other_account)
  70. end
  71. def mute!(other_account, notifications: nil)
  72. notifications = true if notifications.nil?
  73. mute = mute_relationships.create_with(hide_notifications: notifications).find_or_create_by!(target_account: other_account)
  74. # 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.
  75. if mute.hide_notifications? != notifications
  76. mute.update!(hide_notifications: notifications)
  77. end
  78. end
  79. def mute_conversation!(conversation)
  80. conversation_mutes.find_or_create_by!(conversation: conversation)
  81. end
  82. def block_domain!(other_domain)
  83. domain_blocks.find_or_create_by!(domain: other_domain)
  84. end
  85. def unfollow!(other_account)
  86. follow = active_relationships.find_by(target_account: other_account)
  87. follow&.destroy
  88. end
  89. def unblock!(other_account)
  90. block = block_relationships.find_by(target_account: other_account)
  91. block&.destroy
  92. end
  93. def unmute!(other_account)
  94. mute = mute_relationships.find_by(target_account: other_account)
  95. mute&.destroy
  96. end
  97. def unmute_conversation!(conversation)
  98. mute = conversation_mutes.find_by(conversation: conversation)
  99. mute&.destroy!
  100. end
  101. def unblock_domain!(other_domain)
  102. block = domain_blocks.find_by(domain: other_domain)
  103. block&.destroy
  104. end
  105. def following?(other_account)
  106. active_relationships.where(target_account: other_account).exists?
  107. end
  108. def blocking?(other_account)
  109. block_relationships.where(target_account: other_account).exists?
  110. end
  111. def domain_blocking?(other_domain)
  112. domain_blocks.where(domain: other_domain).exists?
  113. end
  114. def muting?(other_account)
  115. mute_relationships.where(target_account: other_account).exists?
  116. end
  117. def muting_conversation?(conversation)
  118. conversation_mutes.where(conversation: conversation).exists?
  119. end
  120. def muting_notifications?(other_account)
  121. mute_relationships.where(target_account: other_account, hide_notifications: true).exists?
  122. end
  123. def muting_reblogs?(other_account)
  124. active_relationships.where(target_account: other_account, show_reblogs: false).exists?
  125. end
  126. def requested?(other_account)
  127. follow_requests.where(target_account: other_account).exists?
  128. end
  129. def favourited?(status)
  130. status.proper.favourites.where(account: self).exists?
  131. end
  132. def reblogged?(status)
  133. status.proper.reblogs.where(account: self).exists?
  134. end
  135. def pinned?(status)
  136. status_pins.where(status: status).exists?
  137. end
  138. end