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.

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