The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

238 lines
8.8 KiB

  1. # frozen_string_literal: true
  2. class Status < ApplicationRecord
  3. include Paginable
  4. include Streamable
  5. include Cacheable
  6. enum visibility: [:public, :unlisted, :private, :direct], _suffix: :visibility
  7. belongs_to :application, class_name: 'Doorkeeper::Application'
  8. belongs_to :account, inverse_of: :statuses, counter_cache: true, required: true
  9. belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account'
  10. belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies
  11. belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, counter_cache: :reblogs_count
  12. has_many :favourites, inverse_of: :status, dependent: :destroy
  13. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
  14. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
  15. has_many :mentions, dependent: :destroy
  16. has_many :media_attachments, dependent: :destroy
  17. has_and_belongs_to_many :tags
  18. has_one :notification, as: :activity, dependent: :destroy
  19. has_one :preview_card, dependent: :destroy
  20. validates :uri, uniqueness: true, unless: 'local?'
  21. validates :text, presence: true, unless: 'reblog?'
  22. validates_with StatusLengthValidator
  23. validates :reblog, uniqueness: { scope: :account }, if: 'reblog?'
  24. default_scope { order('id desc') }
  25. scope :remote, -> { where.not(uri: nil) }
  26. scope :local, -> { where(uri: nil) }
  27. scope :without_replies, -> { where('statuses.reply = FALSE OR statuses.in_reply_to_account_id = statuses.account_id') }
  28. scope :without_reblogs, -> { where('statuses.reblog_of_id IS NULL') }
  29. scope :with_public_visibility, -> { where(visibility: :public) }
  30. scope :tagged_with, ->(tag) { joins(:statuses_tags).where(statuses_tags: { tag_id: tag }) }
  31. scope :local_only, -> { left_outer_joins(:account).where(accounts: { domain: nil }) }
  32. scope :excluding_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced: false }) }
  33. scope :including_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced: true }) }
  34. scope :not_excluded_by_account, ->(account) { where.not(account_id: account.excluded_from_timeline_account_ids) }
  35. cache_associated :account, :application, :media_attachments, :tags, :stream_entry, mentions: :account, reblog: [:account, :application, :stream_entry, :tags, :media_attachments, mentions: :account], thread: :account
  36. def reply?
  37. !in_reply_to_id.nil? || super
  38. end
  39. def local?
  40. uri.nil?
  41. end
  42. def reblog?
  43. !reblog_of_id.nil?
  44. end
  45. def verb
  46. reblog? ? :share : :post
  47. end
  48. def object_type
  49. reply? ? :comment : :note
  50. end
  51. def proper
  52. reblog? ? reblog : self
  53. end
  54. def content
  55. proper.text
  56. end
  57. def target
  58. reblog
  59. end
  60. def title
  61. reblog? ? "#{account.acct} shared a status by #{reblog.account.acct}" : "New status by #{account.acct}"
  62. end
  63. def hidden?
  64. private_visibility? || direct_visibility?
  65. end
  66. def permitted?(other_account = nil)
  67. if direct_visibility?
  68. account.id == other_account&.id || mentions.where(account: other_account).exists?
  69. elsif private_visibility?
  70. account.id == other_account&.id || other_account&.following?(account) || mentions.where(account: other_account).exists?
  71. else
  72. other_account.nil? || !account.blocking?(other_account)
  73. end
  74. end
  75. def ancestors(account = nil)
  76. ids = Rails.cache.fetch("ancestors:#{id}") { (Status.find_by_sql(['WITH RECURSIVE search_tree(id, in_reply_to_id, path) AS (SELECT id, in_reply_to_id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id FROM search_tree JOIN statuses ON statuses.id = search_tree.in_reply_to_id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path DESC', id]) - [self]).pluck(:id) }
  77. statuses = Status.where(id: ids).group_by(&:id)
  78. results = ids.map { |id| statuses[id].first }
  79. results = results.reject { |status| filter_from_context?(status, account) }
  80. results
  81. end
  82. def descendants(account = nil)
  83. ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, path) AS (SELECT id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, path || statuses.id FROM search_tree JOIN statuses ON statuses.in_reply_to_id = search_tree.id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path', id]) - [self]).pluck(:id)
  84. statuses = Status.where(id: ids).group_by(&:id)
  85. results = ids.map { |id| statuses[id].first }
  86. results = results.reject { |status| filter_from_context?(status, account) }
  87. results
  88. end
  89. def non_sensitive_with_media?
  90. !sensitive? && media_attachments.any?
  91. end
  92. class << self
  93. def in_allowed_languages(account)
  94. where(language: account.allowed_languages)
  95. end
  96. def as_home_timeline(account)
  97. where(account: [account] + account.following)
  98. end
  99. def as_public_timeline(account = nil, local_only = false)
  100. query = timeline_scope(local_only).without_replies
  101. apply_timeline_filters(query, account)
  102. end
  103. def as_tag_timeline(tag, account = nil, local_only = false)
  104. query = timeline_scope(local_only).tagged_with(tag)
  105. apply_timeline_filters(query, account)
  106. end
  107. def as_outbox_timeline(account)
  108. where(account: account, visibility: :public)
  109. end
  110. def favourites_map(status_ids, account_id)
  111. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  112. end
  113. def reblogs_map(status_ids, account_id)
  114. select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h
  115. end
  116. def reload_stale_associations!(cached_items)
  117. account_ids = []
  118. cached_items.each do |item|
  119. account_ids << item.account_id
  120. account_ids << item.reblog.account_id if item.reblog?
  121. end
  122. accounts = Account.where(id: account_ids.uniq).map { |a| [a.id, a] }.to_h
  123. cached_items.each do |item|
  124. item.account = accounts[item.account_id]
  125. item.reblog.account = accounts[item.reblog.account_id] if item.reblog?
  126. end
  127. end
  128. def permitted_for(target_account, account)
  129. return where.not(visibility: [:private, :direct]) if account.nil?
  130. if target_account.blocking?(account) # get rid of blocked peeps
  131. none
  132. elsif account.id == target_account.id # author can see own stuff
  133. all
  134. elsif account.following?(target_account) # followers can see followers-only stuff, but also things they are mentioned in
  135. joins('LEFT OUTER JOIN mentions ON statuses.id = mentions.status_id AND mentions.account_id = ' + account.id.to_s)
  136. .where('statuses.visibility != ? OR mentions.id IS NOT NULL', Status.visibilities[:direct])
  137. else # non-followers can see everything that isn't private/direct, but can see stuff they are mentioned in
  138. joins('LEFT OUTER JOIN mentions ON statuses.id = mentions.status_id AND mentions.account_id = ' + account.id.to_s)
  139. .where('statuses.visibility NOT IN (?) OR mentions.id IS NOT NULL', [Status.visibilities[:direct], Status.visibilities[:private]])
  140. end
  141. end
  142. private
  143. def timeline_scope(local_only = false)
  144. starting_scope = local_only ? Status.local_only : Status
  145. starting_scope
  146. .with_public_visibility
  147. .without_reblogs
  148. end
  149. def apply_timeline_filters(query, account)
  150. if account.nil?
  151. filter_timeline_default(query)
  152. else
  153. filter_timeline_for_account(query, account)
  154. end
  155. end
  156. def filter_timeline_for_account(query, account)
  157. query = query.not_excluded_by_account(account)
  158. query = query.in_allowed_languages(account) if account.allowed_languages.present?
  159. query.merge(account_silencing_filter(account))
  160. end
  161. def filter_timeline_default(query)
  162. query.excluding_silenced_accounts
  163. end
  164. def account_silencing_filter(account)
  165. if account.silenced?
  166. including_silenced_accounts
  167. else
  168. excluding_silenced_accounts
  169. end
  170. end
  171. end
  172. before_validation do
  173. text&.strip!
  174. spoiler_text&.strip!
  175. self.reply = !(in_reply_to_id.nil? && thread.nil?) unless reply
  176. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  177. self.in_reply_to_account_id = (thread.account_id == account_id && thread.reply? ? thread.in_reply_to_account_id : thread.account_id) if reply? && !thread.nil?
  178. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  179. end
  180. private
  181. def filter_from_context?(status, account)
  182. account&.blocking?(status.account_id) || account&.muting?(status.account_id) || (status.account.silenced? && !account&.following?(status.account_id)) || !status.permitted?(account)
  183. end
  184. end