A fork of Glitch Social, a fork of 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.

499 lines
15KB

  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 :bookmarks, inverse_of: :status, dependent: :destroy
  48. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
  49. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
  50. has_many :mentions, dependent: :destroy, inverse_of: :status
  51. has_many :active_mentions, -> { active }, class_name: 'Mention', inverse_of: :status
  52. has_many :media_attachments, dependent: :nullify
  53. has_and_belongs_to_many :tags
  54. has_and_belongs_to_many :preview_cards
  55. has_one :notification, as: :activity, dependent: :destroy
  56. has_one :status_stat, inverse_of: :status
  57. has_one :poll, inverse_of: :status, dependent: :destroy
  58. validates :uri, uniqueness: true, presence: true, unless: :local?
  59. validates :text, presence: true, unless: -> { with_media? || reblog? }
  60. validates_with StatusLengthValidator
  61. validates_with DisallowedHashtagsValidator
  62. validates :reblog, uniqueness: { scope: :account }, if: :reblog?
  63. validates :visibility, exclusion: { in: %w(direct limited) }, if: :reblog?
  64. accepts_nested_attributes_for :poll
  65. default_scope { recent.kept }
  66. scope :recent, -> { reorder(id: :desc) }
  67. scope :remote, -> { where(local: false).where.not(uri: nil) }
  68. scope :local, -> { where(local: true).or(where(uri: nil)) }
  69. scope :without_replies, -> { where('statuses.reply = FALSE OR statuses.in_reply_to_account_id = statuses.account_id') }
  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. elsif reblog?
  163. preview = sensitive ? '<sensitive>' : text.slice(0, 10).split("\n")[0]
  164. "#{account.acct} shared #{reblog.account.acct}'s: #{preview}"
  165. else
  166. preview = sensitive ? '<sensitive>' : text.slice(0, 20).split("\n")[0]
  167. "#{account.acct}: #{preview}"
  168. end
  169. end
  170. def hidden?
  171. !distributable?
  172. end
  173. def distributable?
  174. public_visibility? || unlisted_visibility?
  175. end
  176. alias sign? distributable?
  177. def with_media?
  178. media_attachments.any?
  179. end
  180. def non_sensitive_with_media?
  181. !sensitive? && with_media?
  182. end
  183. def reported?
  184. @reported ||= Report.where(target_account: account).unresolved.where('? = ANY(status_ids)', id).exists?
  185. end
  186. def emojis
  187. return @emojis if defined?(@emojis)
  188. fields = [spoiler_text, text]
  189. fields += preloadable_poll.options unless preloadable_poll.nil?
  190. @emojis = CustomEmoji.from_text(fields.join(' '), account.domain)
  191. end
  192. def mark_for_mass_destruction!
  193. @marked_for_mass_destruction = true
  194. end
  195. def marked_for_mass_destruction?
  196. @marked_for_mass_destruction
  197. end
  198. def replies_count
  199. status_stat&.replies_count || 0
  200. end
  201. def reblogs_count
  202. status_stat&.reblogs_count || 0
  203. end
  204. def favourites_count
  205. status_stat&.favourites_count || 0
  206. end
  207. def increment_count!(key)
  208. update_status_stat!(key => public_send(key) + 1)
  209. end
  210. def decrement_count!(key)
  211. update_status_stat!(key => [public_send(key) - 1, 0].max)
  212. end
  213. after_create_commit :increment_counter_caches
  214. after_destroy_commit :decrement_counter_caches
  215. after_create_commit :store_uri, if: :local?
  216. after_create_commit :update_statistics, if: :local?
  217. around_create Mastodon::Snowflake::Callbacks
  218. before_validation :prepare_contents, if: :local?
  219. before_validation :set_reblog
  220. before_validation :set_visibility
  221. before_validation :set_conversation
  222. before_validation :set_local
  223. after_create :set_poll_id
  224. class << self
  225. def selectable_visibilities
  226. visibilities.keys - %w(direct limited)
  227. end
  228. def in_chosen_languages(account)
  229. where(language: nil).or where(language: account.chosen_languages)
  230. end
  231. def as_public_timeline(account = nil, local_only = false)
  232. query = timeline_scope(local_only).without_replies
  233. apply_timeline_filters(query, account, local_only)
  234. end
  235. def as_tag_timeline(tag, account = nil, local_only = false)
  236. query = timeline_scope(local_only).tagged_with(tag)
  237. apply_timeline_filters(query, account, local_only)
  238. end
  239. def as_outbox_timeline(account)
  240. where(account: account, visibility: :public)
  241. end
  242. def favourites_map(status_ids, account_id)
  243. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |f, h| h[f.status_id] = true }
  244. end
  245. def bookmarks_map(status_ids, account_id)
  246. Bookmark.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  247. end
  248. def reblogs_map(status_ids, account_id)
  249. 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 }
  250. end
  251. def mutes_map(conversation_ids, account_id)
  252. ConversationMute.select('conversation_id').where(conversation_id: conversation_ids).where(account_id: account_id).each_with_object({}) { |m, h| h[m.conversation_id] = true }
  253. end
  254. def pins_map(status_ids, account_id)
  255. StatusPin.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |p, h| h[p.status_id] = true }
  256. end
  257. def reload_stale_associations!(cached_items)
  258. account_ids = []
  259. cached_items.each do |item|
  260. account_ids << item.account_id
  261. account_ids << item.reblog.account_id if item.reblog?
  262. end
  263. account_ids.uniq!
  264. return if account_ids.empty?
  265. accounts = Account.where(id: account_ids).includes(:account_stat).each_with_object({}) { |a, h| h[a.id] = a }
  266. cached_items.each do |item|
  267. item.account = accounts[item.account_id]
  268. item.reblog.account = accounts[item.reblog.account_id] if item.reblog?
  269. end
  270. end
  271. def permitted_for(target_account, account)
  272. visibility = [:public, :unlisted]
  273. if account.nil?
  274. where(visibility: visibility)
  275. elsif target_account.blocking?(account) # get rid of blocked peeps
  276. none
  277. elsif account.id == target_account.id # author can see own stuff
  278. all
  279. else
  280. # followers can see followers-only stuff, but also things they are mentioned in.
  281. # non-followers can see everything that isn't private/direct, but can see stuff they are mentioned in.
  282. visibility.push(:private) if account.following?(target_account)
  283. scope = left_outer_joins(:reblog)
  284. scope.where(visibility: visibility)
  285. .or(scope.where(id: account.mentions.select(:status_id)))
  286. .merge(scope.where(reblog_of_id: nil).or(scope.where.not(reblogs_statuses: { account_id: account.excluded_from_timeline_account_ids })))
  287. end
  288. end
  289. private
  290. def timeline_scope(local_only = false)
  291. starting_scope = local_only ? Status.local : Status
  292. starting_scope
  293. .with_public_visibility
  294. .without_reblogs
  295. end
  296. def apply_timeline_filters(query, account, local_only)
  297. if account.nil?
  298. filter_timeline_default(query)
  299. else
  300. filter_timeline_for_account(query, account, local_only)
  301. end
  302. end
  303. def filter_timeline_for_account(query, account, local_only)
  304. query = query.not_excluded_by_account(account)
  305. query = query.not_domain_blocked_by_account(account) unless local_only
  306. query = query.in_chosen_languages(account) if account.chosen_languages.present?
  307. query.merge(account_silencing_filter(account))
  308. end
  309. def filter_timeline_default(query)
  310. query.excluding_silenced_accounts
  311. end
  312. def account_silencing_filter(account)
  313. if account.silenced?
  314. including_myself = left_outer_joins(:account).where(account_id: account.id).references(:accounts)
  315. excluding_silenced_accounts.or(including_myself)
  316. else
  317. excluding_silenced_accounts
  318. end
  319. end
  320. end
  321. def status_stat
  322. super || build_status_stat
  323. end
  324. private
  325. def update_status_stat!(attrs)
  326. return if marked_for_destruction? || destroyed?
  327. status_stat.update(attrs)
  328. end
  329. def store_uri
  330. update_column(:uri, ActivityPub::TagManager.instance.uri_for(self)) if uri.nil?
  331. end
  332. def prepare_contents
  333. text&.strip!
  334. spoiler_text&.strip!
  335. end
  336. def set_reblog
  337. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  338. end
  339. def set_poll_id
  340. update_column(:poll_id, poll.id) unless poll.nil?
  341. end
  342. def set_visibility
  343. self.visibility = reblog.visibility if reblog? && visibility.nil?
  344. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  345. self.sensitive = false if sensitive.nil?
  346. end
  347. def set_conversation
  348. self.thread = thread.reblog if thread&.reblog?
  349. self.reply = !(in_reply_to_id.nil? && thread.nil?) unless reply
  350. if reply? && !thread.nil?
  351. self.in_reply_to_account_id = carried_over_reply_to_account_id
  352. self.conversation_id = thread.conversation_id if conversation_id.nil?
  353. elsif conversation_id.nil?
  354. self.conversation = Conversation.new
  355. end
  356. end
  357. def carried_over_reply_to_account_id
  358. if thread.account_id == account_id && thread.reply?
  359. thread.in_reply_to_account_id
  360. else
  361. thread.account_id
  362. end
  363. end
  364. def set_local
  365. self.local = account.local?
  366. end
  367. def update_statistics
  368. return unless distributable?
  369. ActivityTracker.increment('activity:statuses:local')
  370. end
  371. def increment_counter_caches
  372. return if direct_visibility?
  373. account&.increment_count!(:statuses_count)
  374. reblog&.increment_count!(:reblogs_count) if reblog?
  375. thread&.increment_count!(:replies_count) if in_reply_to_id.present? && distributable?
  376. end
  377. def decrement_counter_caches
  378. return if direct_visibility? || marked_for_mass_destruction?
  379. account&.decrement_count!(:statuses_count)
  380. reblog&.decrement_count!(:reblogs_count) if reblog?
  381. thread&.decrement_count!(:replies_count) if in_reply_to_id.present? && distributable?
  382. end
  383. def unlink_from_conversations
  384. return unless direct_visibility?
  385. mentioned_accounts = mentions.includes(:account).map(&:account)
  386. inbox_owners = mentioned_accounts.select(&:local?) + (account.local? ? [account] : [])
  387. inbox_owners.each do |inbox_owner|
  388. AccountConversation.remove_status(inbox_owner, self)
  389. end
  390. end
  391. end