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.

200 lines
7.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
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], _suffix: :visibility
  8. belongs_to :application, class_name: 'Doorkeeper::Application'
  9. belongs_to :account, inverse_of: :statuses
  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
  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 :account, presence: true
  22. validates :uri, uniqueness: true, unless: 'local?'
  23. validates :text, presence: true, unless: 'reblog?'
  24. validates_with StatusLengthValidator
  25. validates :reblog, uniqueness: { scope: :account, message: 'of status already exists' }, if: 'reblog?'
  26. default_scope { order('id desc') }
  27. scope :remote, -> { where.not(uri: nil) }
  28. scope :local, -> { where(uri: nil) }
  29. scope :without_replies, -> { where('statuses.reply = FALSE OR statuses.in_reply_to_account_id = statuses.account_id') }
  30. scope :without_reblogs, -> { where('statuses.reblog_of_id IS NULL') }
  31. cache_associated :account, :application, :media_attachments, :tags, :stream_entry, mentions: :account, reblog: [:account, :application, :stream_entry, :tags, :media_attachments, mentions: :account], thread: :account
  32. def reply?
  33. super || !in_reply_to_id.nil?
  34. end
  35. def local?
  36. uri.nil?
  37. end
  38. def reblog?
  39. !reblog_of_id.nil?
  40. end
  41. def verb
  42. reblog? ? :share : :post
  43. end
  44. def object_type
  45. reply? ? :comment : :note
  46. end
  47. def content
  48. reblog? ? reblog.text : text
  49. end
  50. def target
  51. reblog
  52. end
  53. def title
  54. content
  55. end
  56. def hidden?
  57. private_visibility?
  58. end
  59. def permitted?(other_account = nil)
  60. if private_visibility?
  61. (account.id == other_account&.id || other_account&.following?(account) || mentions.where(account: other_account).exists?)
  62. else
  63. other_account.nil? || !account.blocking?(other_account)
  64. end
  65. end
  66. def ancestors(account = nil)
  67. 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)
  68. statuses = Status.where(id: ids).group_by(&:id)
  69. results = ids.map { |id| statuses[id].first }
  70. results = results.reject { |status| filter_from_context?(status, account) }
  71. results
  72. end
  73. def descendants(account = nil)
  74. 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)
  75. statuses = Status.where(id: ids).group_by(&:id)
  76. results = ids.map { |id| statuses[id].first }
  77. results = results.reject { |status| filter_from_context?(status, account) }
  78. results
  79. end
  80. class << self
  81. def as_home_timeline(account)
  82. where(account: [account] + account.following)
  83. end
  84. def as_public_timeline(account = nil, local_only = false)
  85. query = joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  86. .where(visibility: :public)
  87. .without_replies
  88. .without_reblogs
  89. query = query.where('accounts.domain IS NULL') if local_only
  90. account.nil? ? filter_timeline_default(query) : filter_timeline_default(filter_timeline(query, account))
  91. end
  92. def as_tag_timeline(tag, account = nil, local_only = false)
  93. query = tag.statuses
  94. .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  95. .where(visibility: :public)
  96. .without_reblogs
  97. query = query.where('accounts.domain IS NULL') if local_only
  98. account.nil? ? filter_timeline_default(query) : filter_timeline_default(filter_timeline(query, account))
  99. end
  100. def favourites_map(status_ids, account_id)
  101. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  102. end
  103. def reblogs_map(status_ids, account_id)
  104. select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h
  105. end
  106. def reload_stale_associations!(cached_items)
  107. account_ids = []
  108. cached_items.each do |item|
  109. account_ids << item.account_id
  110. account_ids << item.reblog.account_id if item.reblog?
  111. end
  112. accounts = Account.where(id: account_ids.uniq).map { |a| [a.id, a] }.to_h
  113. cached_items.each do |item|
  114. item.account = accounts[item.account_id]
  115. item.reblog.account = accounts[item.reblog.account_id] if item.reblog?
  116. end
  117. end
  118. def permitted_for(target_account, account)
  119. if account&.id == target_account.id || account&.following?(target_account)
  120. where('1 = 1')
  121. elsif !account.nil? && target_account.blocking?(account)
  122. where('1 = 0')
  123. elsif !account.nil?
  124. joins('LEFT OUTER JOIN mentions ON statuses.id = mentions.status_id AND mentions.account_id = ' + account.id.to_s)
  125. .where('statuses.visibility != ? OR mentions.id IS NOT NULL', Status.visibilities[:private])
  126. else
  127. where.not(visibility: :private)
  128. end
  129. end
  130. private
  131. def filter_timeline(query, account)
  132. blocked = Block.where(account: account).pluck(:target_account_id) + Block.where(target_account: account).pluck(:account_id) + Mute.where(account: account).pluck(:target_account_id)
  133. 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
  134. query = query.where('accounts.silenced = TRUE') if account.silenced? # and if we're hellbanned, only people who are also hellbanned
  135. query
  136. end
  137. def filter_timeline_default(query)
  138. query.where('accounts.silenced = FALSE')
  139. end
  140. end
  141. before_validation do
  142. text.strip!
  143. spoiler_text&.strip!
  144. self.reply = !(in_reply_to_id.nil? && thread.nil?) unless reply
  145. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  146. 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?
  147. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  148. end
  149. private
  150. def filter_from_context?(status, account)
  151. account&.blocking?(status.account_id) || account&.muting?(status.account_id) || (status.account.silenced? && !account&.following?(status.account_id)) || !status.permitted?(account)
  152. end
  153. end