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.

490 lines
15 KiB

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