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.

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