The code powering m.abunchtell.com https://m.abunchtell.com
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

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