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.

216 lines
8.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
  1. # frozen_string_literal: true
  2. class Status < ApplicationRecord
  3. include ActiveModel::Validations
  4. include Paginable
  5. include Streamable
  6. include Cacheable
  7. enum visibility: [:public, :unlisted, :private, :direct], _suffix: :visibility
  8. belongs_to :application, class_name: 'Doorkeeper::Application'
  9. belongs_to :account, inverse_of: :statuses, counter_cache: true, required: true
  10. belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account'
  11. belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies
  12. belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, counter_cache: :reblogs_count
  13. has_many :favourites, inverse_of: :status, dependent: :destroy
  14. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
  15. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
  16. has_many :mentions, dependent: :destroy
  17. has_many :media_attachments, dependent: :destroy
  18. has_and_belongs_to_many :tags
  19. has_one :notification, as: :activity, dependent: :destroy
  20. has_one :preview_card, dependent: :destroy
  21. validates :uri, uniqueness: true, unless: 'local?'
  22. validates :text, presence: true, unless: 'reblog?'
  23. validates_with StatusLengthValidator
  24. validates :reblog, uniqueness: { scope: :account }, if: 'reblog?'
  25. default_scope { order('id desc') }
  26. scope :remote, -> { where.not(uri: nil) }
  27. scope :local, -> { where(uri: nil) }
  28. scope :without_replies, -> { where('statuses.reply = FALSE OR statuses.in_reply_to_account_id = statuses.account_id') }
  29. scope :without_reblogs, -> { where('statuses.reblog_of_id IS NULL') }
  30. cache_associated :account, :application, :media_attachments, :tags, :stream_entry, mentions: :account, reblog: [:account, :application, :stream_entry, :tags, :media_attachments, mentions: :account], thread: :account
  31. def reply?
  32. !in_reply_to_id.nil? || super
  33. end
  34. def local?
  35. uri.nil?
  36. end
  37. def reblog?
  38. !reblog_of_id.nil?
  39. end
  40. def verb
  41. reblog? ? :share : :post
  42. end
  43. def object_type
  44. reply? ? :comment : :note
  45. end
  46. def proper
  47. reblog? ? reblog : self
  48. end
  49. def content
  50. proper.text
  51. end
  52. def target
  53. reblog
  54. end
  55. def title
  56. reblog? ? "#{account.acct} shared a status by #{reblog.account.acct}" : "New status by #{account.acct}"
  57. end
  58. def hidden?
  59. private_visibility? || direct_visibility?
  60. end
  61. def permitted?(other_account = nil)
  62. if direct_visibility?
  63. account.id == other_account&.id || mentions.where(account: other_account).exists?
  64. elsif private_visibility?
  65. account.id == other_account&.id || other_account&.following?(account) || mentions.where(account: other_account).exists?
  66. else
  67. other_account.nil? || !account.blocking?(other_account)
  68. end
  69. end
  70. def ancestors(account = nil)
  71. ids = (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)
  72. statuses = Status.where(id: ids).group_by(&:id)
  73. results = ids.map { |id| statuses[id].first }
  74. results = results.reject { |status| filter_from_context?(status, account) }
  75. results
  76. end
  77. def descendants(account = nil)
  78. 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)
  79. statuses = Status.where(id: ids).group_by(&:id)
  80. results = ids.map { |id| statuses[id].first }
  81. results = results.reject { |status| filter_from_context?(status, account) }
  82. results
  83. end
  84. def non_sensitive_with_media?
  85. !sensitive? && media_attachments.any?
  86. end
  87. class << self
  88. def as_home_timeline(account)
  89. where(account: [account] + account.following)
  90. end
  91. def as_public_timeline(account = nil, local_only = false)
  92. query = joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  93. .where(visibility: :public)
  94. .without_replies
  95. .without_reblogs
  96. query = query.where('accounts.domain IS NULL') if local_only
  97. account.nil? ? filter_timeline_default(query) : filter_timeline_default(filter_timeline(query, account))
  98. end
  99. def as_tag_timeline(tag, account = nil, local_only = false)
  100. query = tag.statuses
  101. .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  102. .where(visibility: :public)
  103. .without_reblogs
  104. query = query.where('accounts.domain IS NULL') if local_only
  105. account.nil? ? filter_timeline_default(query) : filter_timeline_default(filter_timeline(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 filter_timeline(query, account)
  144. blocked = Rails.cache.fetch("exclude_account_ids_for:#{account.id}") { Block.where(account: account).pluck(:target_account_id) + Block.where(target_account: account).pluck(:account_id) + Mute.where(account: account).pluck(:target_account_id) }
  145. query = query.where('statuses.account_id NOT IN (?)', blocked) unless blocked.empty? # Only give us statuses from people we haven't blocked, or muted, or that have blocked us
  146. query = query.where('accounts.silenced = TRUE') if account.silenced? # and if we're hellbanned, only people who are also hellbanned
  147. query
  148. end
  149. def filter_timeline_default(query)
  150. query.where('accounts.silenced = FALSE')
  151. end
  152. end
  153. before_validation do
  154. text&.strip!
  155. spoiler_text&.strip!
  156. self.reply = !(in_reply_to_id.nil? && thread.nil?) unless reply
  157. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  158. 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?
  159. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  160. end
  161. private
  162. def filter_from_context?(status, account)
  163. account&.blocking?(status.account_id) || account&.muting?(status.account_id) || (status.account.silenced? && !account&.following?(status.account_id)) || !status.permitted?(account)
  164. end
  165. end