The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

494 lines
15 KiB

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