闭社主体 forked from https://github.com/tootsuite/mastodon
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.

260 lines
9.8 KiB

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