闭社主体 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.

503 lines
16 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: statuses
  5. #
  6. # id :bigint(8) not null, primary key
  7. # uri :string
  8. # text :text default(""), not null
  9. # created_at :datetime not null
  10. # updated_at :datetime not null
  11. # in_reply_to_id :bigint(8)
  12. # reblog_of_id :bigint(8)
  13. # url :string
  14. # sensitive :boolean default(FALSE), not null
  15. # visibility :integer default("public"), not null
  16. # spoiler_text :text default(""), not null
  17. # reply :boolean default(FALSE), not null
  18. # language :string
  19. # conversation_id :bigint(8)
  20. # local :boolean
  21. # account_id :bigint(8) not null
  22. # application_id :bigint(8)
  23. # in_reply_to_account_id :bigint(8)
  24. #
  25. class Status < ApplicationRecord
  26. before_destroy :unlink_from_conversations
  27. include Paginable
  28. include Streamable
  29. include Cacheable
  30. include StatusThreadingConcern
  31. # If `override_timestamps` is set at creation time, Snowflake ID creation
  32. # will be based on current time instead of `created_at`
  33. attr_accessor :override_timestamps
  34. update_index('statuses#status', :proper) if Chewy.enabled?
  35. enum visibility: [:public, :unlisted, :private, :direct, :limited], _suffix: :visibility
  36. belongs_to :application, class_name: 'Doorkeeper::Application', optional: true
  37. belongs_to :account, inverse_of: :statuses
  38. belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account', optional: true
  39. belongs_to :conversation, optional: true
  40. belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies, optional: true
  41. belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, optional: true
  42. has_many :favourites, inverse_of: :status, dependent: :destroy
  43. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
  44. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
  45. has_many :mentions, dependent: :destroy, inverse_of: :status
  46. has_many :active_mentions, -> { active }, class_name: 'Mention', inverse_of: :status
  47. has_many :media_attachments, dependent: :nullify
  48. has_and_belongs_to_many :tags
  49. has_and_belongs_to_many :preview_cards
  50. has_one :notification, as: :activity, dependent: :destroy
  51. has_one :stream_entry, as: :activity, inverse_of: :status
  52. has_one :status_stat, inverse_of: :status
  53. validates :uri, uniqueness: true, presence: true, unless: :local?
  54. validates :text, presence: true, unless: -> { with_media? || reblog? }
  55. validates_with StatusLengthValidator
  56. validates_with DisallowedHashtagsValidator
  57. validates :reblog, uniqueness: { scope: :account }, if: :reblog?
  58. default_scope { recent }
  59. scope :recent, -> { reorder(id: :desc) }
  60. scope :remote, -> { where(local: false).or(where.not(uri: nil)) }
  61. scope :local, -> { where(local: true).or(where(uri: nil)) }
  62. scope :without_replies, -> { where('statuses.reply = FALSE OR statuses.in_reply_to_account_id = statuses.account_id') }
  63. scope :without_reblogs, -> { where('statuses.reblog_of_id IS NULL') }
  64. scope :with_public_visibility, -> { where(visibility: :public) }
  65. scope :tagged_with, ->(tag) { joins(:statuses_tags).where(statuses_tags: { tag_id: tag }) }
  66. scope :excluding_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced: false }) }
  67. scope :including_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced: true }) }
  68. scope :not_excluded_by_account, ->(account) { where.not(account_id: account.excluded_from_timeline_account_ids) }
  69. scope :not_domain_blocked_by_account, ->(account) { account.excluded_from_timeline_domains.blank? ? left_outer_joins(:account) : left_outer_joins(:account).where('accounts.domain IS NULL OR accounts.domain NOT IN (?)', account.excluded_from_timeline_domains) }
  70. scope :tagged_with_all, ->(tags) {
  71. Array(tags).map(&:id).map(&:to_i).reduce(self) do |result, id|
  72. result.joins("INNER JOIN statuses_tags t#{id} ON t#{id}.status_id = statuses.id AND t#{id}.tag_id = #{id}")
  73. end
  74. }
  75. scope :tagged_with_none, ->(tags) {
  76. Array(tags).map(&:id).map(&:to_i).reduce(self) do |result, id|
  77. result.joins("LEFT OUTER JOIN statuses_tags t#{id} ON t#{id}.status_id = statuses.id AND t#{id}.tag_id = #{id}")
  78. .where("t#{id}.tag_id IS NULL")
  79. end
  80. }
  81. cache_associated :application,
  82. :media_attachments,
  83. :conversation,
  84. :status_stat,
  85. :tags,
  86. :preview_cards,
  87. :stream_entry,
  88. account: :account_stat,
  89. active_mentions: { account: :account_stat },
  90. reblog: [
  91. :application,
  92. :stream_entry,
  93. :tags,
  94. :preview_cards,
  95. :media_attachments,
  96. :conversation,
  97. :status_stat,
  98. account: :account_stat,
  99. active_mentions: { account: :account_stat },
  100. ],
  101. thread: { account: :account_stat }
  102. delegate :domain, to: :account, prefix: true
  103. REAL_TIME_WINDOW = 6.hours
  104. def searchable_by(preloaded = nil)
  105. ids = [account_id]
  106. if preloaded.nil?
  107. ids += mentions.pluck(:account_id)
  108. ids += favourites.pluck(:account_id)
  109. ids += reblogs.pluck(:account_id)
  110. else
  111. ids += preloaded.mentions[id] || []
  112. ids += preloaded.favourites[id] || []
  113. ids += preloaded.reblogs[id] || []
  114. end
  115. ids.uniq
  116. end
  117. def reply?
  118. !in_reply_to_id.nil? || attributes['reply']
  119. end
  120. def local?
  121. attributes['local'] || uri.nil?
  122. end
  123. def reblog?
  124. !reblog_of_id.nil?
  125. end
  126. def within_realtime_window?
  127. created_at >= REAL_TIME_WINDOW.ago
  128. end
  129. def verb
  130. if destroyed?
  131. :delete
  132. else
  133. reblog? ? :share : :post
  134. end
  135. end
  136. def object_type
  137. reply? ? :comment : :note
  138. end
  139. def proper
  140. reblog? ? reblog : self
  141. end
  142. def content
  143. proper.text
  144. end
  145. def target
  146. reblog
  147. end
  148. def preview_card
  149. preview_cards.first
  150. end
  151. def title
  152. if destroyed?
  153. "#{account.acct} deleted status"
  154. else
  155. reblog? ? "#{account.acct} shared a status by #{reblog.account.acct}" : "New status by #{account.acct}"
  156. end
  157. end
  158. def hidden?
  159. private_visibility? || direct_visibility? || limited_visibility?
  160. end
  161. def distributable?
  162. public_visibility? || unlisted_visibility?
  163. end
  164. def with_media?
  165. media_attachments.any?
  166. end
  167. def non_sensitive_with_media?
  168. !sensitive? && with_media?
  169. end
  170. def emojis
  171. @emojis ||= CustomEmoji.from_text([spoiler_text, text].join(' '), account.domain)
  172. end
  173. def mark_for_mass_destruction!
  174. @marked_for_mass_destruction = true
  175. end
  176. def marked_for_mass_destruction?
  177. @marked_for_mass_destruction
  178. end
  179. def replies_count
  180. status_stat&.replies_count || 0
  181. end
  182. def reblogs_count
  183. status_stat&.reblogs_count || 0
  184. end
  185. def favourites_count
  186. status_stat&.favourites_count || 0
  187. end
  188. def increment_count!(key)
  189. update_status_stat!(key => public_send(key) + 1)
  190. end
  191. def decrement_count!(key)
  192. update_status_stat!(key => [public_send(key) - 1, 0].max)
  193. end
  194. after_create_commit :increment_counter_caches
  195. after_destroy_commit :decrement_counter_caches
  196. after_create_commit :store_uri, if: :local?
  197. after_create_commit :update_statistics, if: :local?
  198. around_create Mastodon::Snowflake::Callbacks
  199. before_validation :prepare_contents, if: :local?
  200. before_validation :set_reblog
  201. before_validation :set_visibility
  202. before_validation :set_conversation
  203. before_validation :set_local
  204. class << self
  205. def selectable_visibilities
  206. visibilities.keys - %w(direct limited)
  207. end
  208. def in_chosen_languages(account)
  209. where(language: nil).or where(language: account.chosen_languages)
  210. end
  211. def as_home_timeline(account)
  212. where(account: [account] + account.following).where(visibility: [:public, :unlisted, :private])
  213. end
  214. def as_direct_timeline(account, limit = 20, max_id = nil, since_id = nil, cache_ids = false)
  215. # direct timeline is mix of direct message from_me and to_me.
  216. # 2 queries are executed with pagination.
  217. # constant expression using arel_table is required for partial index
  218. # _from_me part does not require any timeline filters
  219. query_from_me = where(account_id: account.id)
  220. .where(Status.arel_table[:visibility].eq(3))
  221. .limit(limit)
  222. .order('statuses.id DESC')
  223. # _to_me part requires mute and block filter.
  224. # FIXME: may we check mutes.hide_notifications?
  225. query_to_me = Status
  226. .joins(:mentions)
  227. .merge(Mention.where(account_id: account.id))
  228. .where(Status.arel_table[:visibility].eq(3))
  229. .limit(limit)
  230. .order('mentions.status_id DESC')
  231. .not_excluded_by_account(account)
  232. if max_id.present?
  233. query_from_me = query_from_me.where('statuses.id < ?', max_id)
  234. query_to_me = query_to_me.where('mentions.status_id < ?', max_id)
  235. end
  236. if since_id.present?
  237. query_from_me = query_from_me.where('statuses.id > ?', since_id)
  238. query_to_me = query_to_me.where('mentions.status_id > ?', since_id)
  239. end
  240. if cache_ids
  241. # returns array of cache_ids object that have id and updated_at
  242. (query_from_me.cache_ids.to_a + query_to_me.cache_ids.to_a).uniq(&:id).sort_by(&:id).reverse.take(limit)
  243. else
  244. # returns ActiveRecord.Relation
  245. items = (query_from_me.select(:id).to_a + query_to_me.select(:id).to_a).uniq(&:id).sort_by(&:id).reverse.take(limit)
  246. Status.where(id: items.map(&:id))
  247. end
  248. end
  249. def as_public_timeline(account = nil, local_only = false)
  250. query = timeline_scope(local_only).without_replies
  251. apply_timeline_filters(query, account, local_only)
  252. end
  253. def as_tag_timeline(tag, account = nil, local_only = false)
  254. query = timeline_scope(local_only).tagged_with(tag)
  255. apply_timeline_filters(query, account, local_only)
  256. end
  257. def as_outbox_timeline(account)
  258. where(account: account, visibility: :public)
  259. end
  260. def favourites_map(status_ids, account_id)
  261. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |f, h| h[f.status_id] = true }
  262. end
  263. def reblogs_map(status_ids, account_id)
  264. select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).reorder(nil).each_with_object({}) { |s, h| h[s.reblog_of_id] = true }
  265. end
  266. def mutes_map(conversation_ids, account_id)
  267. ConversationMute.select('conversation_id').where(conversation_id: conversation_ids).where(account_id: account_id).each_with_object({}) { |m, h| h[m.conversation_id] = true }
  268. end
  269. def pins_map(status_ids, account_id)
  270. StatusPin.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |p, h| h[p.status_id] = true }
  271. end
  272. def reload_stale_associations!(cached_items)
  273. account_ids = []
  274. cached_items.each do |item|
  275. account_ids << item.account_id
  276. account_ids << item.reblog.account_id if item.reblog?
  277. end
  278. account_ids.uniq!
  279. return if account_ids.empty?
  280. accounts = Account.where(id: account_ids).includes(:account_stat).each_with_object({}) { |a, h| h[a.id] = a }
  281. cached_items.each do |item|
  282. item.account = accounts[item.account_id]
  283. item.reblog.account = accounts[item.reblog.account_id] if item.reblog?
  284. end
  285. end
  286. def permitted_for(target_account, account)
  287. visibility = [:public, :unlisted]
  288. if account.nil?
  289. where(visibility: visibility)
  290. elsif target_account.blocking?(account) # get rid of blocked peeps
  291. none
  292. elsif account.id == target_account.id # author can see own stuff
  293. all
  294. else
  295. # followers can see followers-only stuff, but also things they are mentioned in.
  296. # non-followers can see everything that isn't private/direct, but can see stuff they are mentioned in.
  297. visibility.push(:private) if account.following?(target_account)
  298. scope = left_outer_joins(:reblog)
  299. scope.where(visibility: visibility)
  300. .or(scope.where(id: account.mentions.select(:status_id)))
  301. .merge(scope.where(reblog_of_id: nil).or(scope.where.not(reblogs_statuses: { account_id: account.excluded_from_timeline_account_ids })))
  302. end
  303. end
  304. private
  305. def timeline_scope(local_only = false)
  306. starting_scope = local_only ? Status.local : Status
  307. starting_scope
  308. .with_public_visibility
  309. .without_reblogs
  310. end
  311. def apply_timeline_filters(query, account, local_only)
  312. if account.nil?
  313. filter_timeline_default(query)
  314. else
  315. filter_timeline_for_account(query, account, local_only)
  316. end
  317. end
  318. def filter_timeline_for_account(query, account, local_only)
  319. query = query.not_excluded_by_account(account)
  320. query = query.not_domain_blocked_by_account(account) unless local_only
  321. query = query.in_chosen_languages(account) if account.chosen_languages.present?
  322. query.merge(account_silencing_filter(account))
  323. end
  324. def filter_timeline_default(query)
  325. query.excluding_silenced_accounts
  326. end
  327. def account_silencing_filter(account)
  328. if account.silenced?
  329. including_myself = left_outer_joins(:account).where(account_id: account.id).references(:accounts)
  330. excluding_silenced_accounts.or(including_myself)
  331. else
  332. excluding_silenced_accounts
  333. end
  334. end
  335. end
  336. private
  337. def update_status_stat!(attrs)
  338. return if marked_for_destruction? || destroyed?
  339. record = status_stat || build_status_stat
  340. record.update(attrs)
  341. end
  342. def store_uri
  343. update_column(:uri, ActivityPub::TagManager.instance.uri_for(self)) if uri.nil?
  344. end
  345. def prepare_contents
  346. text&.strip!
  347. spoiler_text&.strip!
  348. end
  349. def set_reblog
  350. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  351. end
  352. def set_visibility
  353. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  354. self.visibility = reblog.visibility if reblog?
  355. self.sensitive = false if sensitive.nil?
  356. end
  357. def set_conversation
  358. self.thread = thread.reblog if thread&.reblog?
  359. self.reply = !(in_reply_to_id.nil? && thread.nil?) unless reply
  360. if reply? && !thread.nil?
  361. self.in_reply_to_account_id = carried_over_reply_to_account_id
  362. self.conversation_id = thread.conversation_id if conversation_id.nil?
  363. elsif conversation_id.nil?
  364. self.conversation = Conversation.new
  365. end
  366. end
  367. def carried_over_reply_to_account_id
  368. if thread.account_id == account_id && thread.reply?
  369. thread.in_reply_to_account_id
  370. else
  371. thread.account_id
  372. end
  373. end
  374. def set_local
  375. self.local = account.local?
  376. end
  377. def update_statistics
  378. return unless public_visibility? || unlisted_visibility?
  379. ActivityTracker.increment('activity:statuses:local')
  380. end
  381. def increment_counter_caches
  382. return if direct_visibility?
  383. account&.increment_count!(:statuses_count)
  384. reblog&.increment_count!(:reblogs_count) if reblog?
  385. thread&.increment_count!(:replies_count) if in_reply_to_id.present? && (public_visibility? || unlisted_visibility?)
  386. end
  387. def decrement_counter_caches
  388. return if direct_visibility? || marked_for_mass_destruction?
  389. account&.decrement_count!(:statuses_count)
  390. reblog&.decrement_count!(:reblogs_count) if reblog?
  391. thread&.decrement_count!(:replies_count) if in_reply_to_id.present? && (public_visibility? || unlisted_visibility?)
  392. end
  393. def unlink_from_conversations
  394. return unless direct_visibility?
  395. mentioned_accounts = mentions.includes(:account).map(&:account)
  396. inbox_owners = mentioned_accounts.select(&:local?) + (account.local? ? [account] : [])
  397. inbox_owners.each do |inbox_owner|
  398. AccountConversation.remove_status(inbox_owner, self)
  399. end
  400. end
  401. end