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.
 
 
 
 

490 lines
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. #
  25. class Status < ApplicationRecord
  26. before_destroy :unlink_from_conversations
  27. include Paginable
  28. include Streamable
  29. include Cacheable
  30. include StatusThreadingConcern
  31. # If `override_timestamps` is set at creation time, Snowflake ID creation
  32. # will be based on current time instead of `created_at`
  33. attr_accessor :override_timestamps
  34. update_index('statuses#status', :proper) if Chewy.enabled?
  35. enum visibility: [:public, :unlisted, :private, :direct], _suffix: :visibility
  36. belongs_to :application, class_name: 'Doorkeeper::Application', optional: true
  37. belongs_to :account, inverse_of: :statuses
  38. belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account', optional: true
  39. belongs_to :conversation, optional: true
  40. belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies, optional: true
  41. belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, optional: true
  42. has_many :favourites, inverse_of: :status, dependent: :destroy
  43. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
  44. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
  45. has_many :mentions, dependent: :destroy
  46. has_many :media_attachments, dependent: :nullify
  47. has_and_belongs_to_many :tags
  48. has_and_belongs_to_many :preview_cards
  49. has_one :notification, as: :activity, dependent: :destroy
  50. has_one :stream_entry, as: :activity, inverse_of: :status
  51. has_one :status_stat, 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,
  70. :application,
  71. :media_attachments,
  72. :conversation,
  73. :status_stat,
  74. :tags,
  75. :stream_entry,
  76. mentions: :account,
  77. reblog: [
  78. :account,
  79. :application,
  80. :stream_entry,
  81. :tags,
  82. :media_attachments,
  83. :conversation,
  84. :status_stat,
  85. mentions: :account,
  86. ],
  87. thread: :account
  88. delegate :domain, to: :account, prefix: true
  89. REAL_TIME_WINDOW = 6.hours
  90. def searchable_by(preloaded = nil)
  91. ids = [account_id]
  92. if preloaded.nil?
  93. ids += mentions.pluck(:account_id)
  94. ids += favourites.pluck(:account_id)
  95. ids += reblogs.pluck(:account_id)
  96. else
  97. ids += preloaded.mentions[id] || []
  98. ids += preloaded.favourites[id] || []
  99. ids += preloaded.reblogs[id] || []
  100. end
  101. ids.uniq
  102. end
  103. def reply?
  104. !in_reply_to_id.nil? || attributes['reply']
  105. end
  106. def local?
  107. attributes['local'] || uri.nil?
  108. end
  109. def reblog?
  110. !reblog_of_id.nil?
  111. end
  112. def within_realtime_window?
  113. created_at >= REAL_TIME_WINDOW.ago
  114. end
  115. def verb
  116. if destroyed?
  117. :delete
  118. else
  119. reblog? ? :share : :post
  120. end
  121. end
  122. def object_type
  123. reply? ? :comment : :note
  124. end
  125. def proper
  126. reblog? ? reblog : self
  127. end
  128. def content
  129. proper.text
  130. end
  131. def target
  132. reblog
  133. end
  134. def title
  135. if destroyed?
  136. "#{account.acct} deleted status"
  137. else
  138. reblog? ? "#{account.acct} shared a status by #{reblog.account.acct}" : "New status by #{account.acct}"
  139. end
  140. end
  141. def hidden?
  142. private_visibility? || direct_visibility?
  143. end
  144. def with_media?
  145. media_attachments.any?
  146. end
  147. def non_sensitive_with_media?
  148. !sensitive? && with_media?
  149. end
  150. def emojis
  151. @emojis ||= CustomEmoji.from_text([spoiler_text, text].join(' '), account.domain)
  152. end
  153. def mark_for_mass_destruction!
  154. @marked_for_mass_destruction = true
  155. end
  156. def marked_for_mass_destruction?
  157. @marked_for_mass_destruction
  158. end
  159. def replies_count
  160. status_stat&.replies_count || 0
  161. end
  162. def reblogs_count
  163. status_stat&.reblogs_count || 0
  164. end
  165. def favourites_count
  166. status_stat&.favourites_count || 0
  167. end
  168. def increment_count!(key)
  169. update_status_stat!(key => public_send(key) + 1)
  170. end
  171. def decrement_count!(key)
  172. update_status_stat!(key => [public_send(key) - 1, 0].max)
  173. end
  174. after_create :increment_counter_caches
  175. after_destroy :decrement_counter_caches
  176. after_create_commit :store_uri, if: :local?
  177. after_create_commit :update_statistics, if: :local?
  178. around_create Mastodon::Snowflake::Callbacks
  179. before_validation :prepare_contents, if: :local?
  180. before_validation :set_reblog
  181. before_validation :set_visibility
  182. before_validation :set_conversation
  183. before_validation :set_local
  184. class << self
  185. def cache_ids
  186. left_outer_joins(:status_stat).select('statuses.id, greatest(statuses.updated_at, status_stats.updated_at) AS updated_at')
  187. end
  188. def in_chosen_languages(account)
  189. where(language: nil).or where(language: account.chosen_languages)
  190. end
  191. def as_home_timeline(account)
  192. where(account: [account] + account.following).where(visibility: [:public, :unlisted, :private])
  193. end
  194. def as_direct_timeline(account, limit = 20, max_id = nil, since_id = nil, cache_ids = false)
  195. # direct timeline is mix of direct message from_me and to_me.
  196. # 2 queries are executed with pagination.
  197. # constant expression using arel_table is required for partial index
  198. # _from_me part does not require any timeline filters
  199. query_from_me = where(account_id: account.id)
  200. .where(Status.arel_table[:visibility].eq(3))
  201. .limit(limit)
  202. .order('statuses.id DESC')
  203. # _to_me part requires mute and block filter.
  204. # FIXME: may we check mutes.hide_notifications?
  205. query_to_me = Status
  206. .joins(:mentions)
  207. .merge(Mention.where(account_id: account.id))
  208. .where(Status.arel_table[:visibility].eq(3))
  209. .limit(limit)
  210. .order('mentions.status_id DESC')
  211. .not_excluded_by_account(account)
  212. if max_id.present?
  213. query_from_me = query_from_me.where('statuses.id < ?', max_id)
  214. query_to_me = query_to_me.where('mentions.status_id < ?', max_id)
  215. end
  216. if since_id.present?
  217. query_from_me = query_from_me.where('statuses.id > ?', since_id)
  218. query_to_me = query_to_me.where('mentions.status_id > ?', since_id)
  219. end
  220. if cache_ids
  221. # returns array of cache_ids object that have id and updated_at
  222. (query_from_me.cache_ids.to_a + query_to_me.cache_ids.to_a).uniq(&:id).sort_by(&:id).reverse.take(limit)
  223. else
  224. # returns ActiveRecord.Relation
  225. items = (query_from_me.select(:id).to_a + query_to_me.select(:id).to_a).uniq(&:id).sort_by(&:id).reverse.take(limit)
  226. Status.where(id: items.map(&:id))
  227. end
  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).map { |f| [f.status_id, true] }.to_h
  242. end
  243. def reblogs_map(status_ids, account_id)
  244. 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
  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).map { |m| [m.conversation_id, true] }.to_h
  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).map { |p| [p.status_id, true] }.to_h
  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).map { |a| [a.id, a] }.to_h
  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. private
  317. def update_status_stat!(attrs)
  318. return if marked_for_destruction? || destroyed?
  319. record = status_stat || build_status_stat
  320. record.update(attrs)
  321. end
  322. def store_uri
  323. update_attribute(:uri, ActivityPub::TagManager.instance.uri_for(self)) if uri.nil?
  324. end
  325. def prepare_contents
  326. text&.strip!
  327. spoiler_text&.strip!
  328. end
  329. def set_reblog
  330. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  331. end
  332. def set_visibility
  333. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  334. self.visibility = reblog.visibility if reblog?
  335. self.sensitive = false if sensitive.nil?
  336. end
  337. def set_conversation
  338. self.reply = !(in_reply_to_id.nil? && thread.nil?) unless reply
  339. if reply? && !thread.nil?
  340. self.in_reply_to_account_id = carried_over_reply_to_account_id
  341. self.conversation_id = thread.conversation_id if conversation_id.nil?
  342. elsif conversation_id.nil?
  343. self.conversation = Conversation.new
  344. end
  345. end
  346. def carried_over_reply_to_account_id
  347. if thread.account_id == account_id && thread.reply?
  348. thread.in_reply_to_account_id
  349. else
  350. thread.account_id
  351. end
  352. end
  353. def set_local
  354. self.local = account.local?
  355. end
  356. def update_statistics
  357. return unless public_visibility? || unlisted_visibility?
  358. ActivityTracker.increment('activity:statuses:local')
  359. end
  360. def increment_counter_caches
  361. return if direct_visibility?
  362. if association(:account).loaded?
  363. account.update_attribute(:statuses_count, account.statuses_count + 1)
  364. else
  365. Account.where(id: account_id).update_all('statuses_count = COALESCE(statuses_count, 0) + 1')
  366. end
  367. reblog&.increment_count!(:reblogs_count) if reblog?
  368. thread&.increment_count!(:replies_count) if in_reply_to_id.present? && (public_visibility? || unlisted_visibility?)
  369. end
  370. def decrement_counter_caches
  371. return if direct_visibility? || marked_for_mass_destruction?
  372. if association(:account).loaded?
  373. account.update_attribute(:statuses_count, [account.statuses_count - 1, 0].max)
  374. else
  375. Account.where(id: account_id).update_all('statuses_count = GREATEST(COALESCE(statuses_count, 0) - 1, 0)')
  376. end
  377. reblog&.decrement_count!(:reblogs_count) if reblog?
  378. thread&.decrement_count!(:replies_count) if in_reply_to_id.present? && (public_visibility? || unlisted_visibility?)
  379. end
  380. def unlink_from_conversations
  381. return unless direct_visibility?
  382. mentioned_accounts = mentions.includes(:account).map(&:account)
  383. inbox_owners = mentioned_accounts.select(&:local?) + (account.local? ? [account] : [])
  384. inbox_owners.each do |inbox_owner|
  385. AccountConversation.remove_status(inbox_owner, self)
  386. end
  387. end
  388. end