status.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. update_index('statuses#status', :proper) if Chewy.enabled?
  33. enum visibility: [:public, :unlisted, :private, :direct], _suffix: :visibility
  34. belongs_to :application, class_name: 'Doorkeeper::Application', optional: true
  35. belongs_to :account, inverse_of: :statuses, counter_cache: true
  36. belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account', optional: true
  37. belongs_to :conversation, optional: true
  38. belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies, optional: true
  39. belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, counter_cache: :reblogs_count, optional: true
  40. has_many :favourites, inverse_of: :status, dependent: :destroy
  41. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
  42. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
  43. has_many :mentions, dependent: :destroy
  44. has_many :media_attachments, dependent: :destroy
  45. has_and_belongs_to_many :tags
  46. has_and_belongs_to_many :preview_cards
  47. has_one :notification, as: :activity, dependent: :destroy
  48. has_one :stream_entry, as: :activity, inverse_of: :status
  49. validates :uri, uniqueness: true, presence: true, unless: :local?
  50. validates :text, presence: true, unless: -> { with_media? || reblog? }
  51. validates_with StatusLengthValidator
  52. validates_with DisallowedHashtagsValidator
  53. validates :reblog, uniqueness: { scope: :account }, if: :reblog?
  54. default_scope { recent }
  55. scope :recent, -> { reorder(id: :desc) }
  56. scope :remote, -> { where(local: false).or(where.not(uri: nil)) }
  57. scope :local, -> { where(local: true).or(where(uri: nil)) }
  58. scope :without_replies, -> { where('statuses.reply = FALSE OR statuses.in_reply_to_account_id = statuses.account_id') }
  59. scope :without_reblogs, -> { where('statuses.reblog_of_id IS NULL') }
  60. scope :with_public_visibility, -> { where(visibility: :public) }
  61. scope :tagged_with, ->(tag) { joins(:statuses_tags).where(statuses_tags: { tag_id: tag }) }
  62. scope :excluding_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced: false }) }
  63. scope :including_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced: true }) }
  64. scope :not_excluded_by_account, ->(account) { where.not(account_id: account.excluded_from_timeline_account_ids) }
  65. 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) }
  66. 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
  67. delegate :domain, to: :account, prefix: true
  68. REAL_TIME_WINDOW = 6.hours
  69. def searchable_by(preloaded = nil)
  70. ids = [account_id]
  71. if preloaded.nil?
  72. ids += mentions.pluck(:account_id)
  73. ids += favourites.pluck(:account_id)
  74. ids += reblogs.pluck(:account_id)
  75. else
  76. ids += preloaded.mentions[id] || []
  77. ids += preloaded.favourites[id] || []
  78. ids += preloaded.reblogs[id] || []
  79. end
  80. ids.uniq
  81. end
  82. def reply?
  83. !in_reply_to_id.nil? || attributes['reply']
  84. end
  85. def local?
  86. attributes['local'] || uri.nil?
  87. end
  88. def reblog?
  89. !reblog_of_id.nil?
  90. end
  91. def within_realtime_window?
  92. created_at >= REAL_TIME_WINDOW.ago
  93. end
  94. def verb
  95. if destroyed?
  96. :delete
  97. else
  98. reblog? ? :share : :post
  99. end
  100. end
  101. def object_type
  102. reply? ? :comment : :note
  103. end
  104. def proper
  105. reblog? ? reblog : self
  106. end
  107. def content
  108. proper.text
  109. end
  110. def target
  111. reblog
  112. end
  113. def title
  114. if destroyed?
  115. "#{account.acct} deleted status"
  116. else
  117. reblog? ? "#{account.acct} shared a status by #{reblog.account.acct}" : "New status by #{account.acct}"
  118. end
  119. end
  120. def hidden?
  121. private_visibility? || direct_visibility?
  122. end
  123. def with_media?
  124. media_attachments.any?
  125. end
  126. def non_sensitive_with_media?
  127. !sensitive? && with_media?
  128. end
  129. def emojis
  130. @emojis ||= CustomEmoji.from_text([spoiler_text, text].join(' '), account.domain)
  131. end
  132. after_create_commit :store_uri, if: :local?
  133. after_create_commit :update_statistics, if: :local?
  134. around_create Mastodon::Snowflake::Callbacks
  135. before_validation :prepare_contents, if: :local?
  136. before_validation :set_reblog
  137. before_validation :set_visibility
  138. before_validation :set_conversation
  139. before_validation :set_sensitivity
  140. before_validation :set_local
  141. class << self
  142. def not_in_filtered_languages(account)
  143. where(language: nil).or where.not(language: account.filtered_languages)
  144. end
  145. def as_home_timeline(account)
  146. where(account: [account] + account.following).where(visibility: [:public, :unlisted, :private])
  147. end
  148. def as_direct_timeline(account)
  149. query = joins("LEFT OUTER JOIN mentions ON statuses.id = mentions.status_id AND mentions.account_id = #{account.id}")
  150. .where("mentions.account_id = #{account.id} OR statuses.account_id = #{account.id}")
  151. .where(visibility: [:direct])
  152. apply_timeline_filters(query, account, false)
  153. end
  154. def as_public_timeline(account = nil, local_only = false)
  155. query = timeline_scope(local_only).without_replies
  156. apply_timeline_filters(query, account, local_only)
  157. end
  158. def as_tag_timeline(tag, account = nil, local_only = false)
  159. query = timeline_scope(local_only).tagged_with(tag)
  160. apply_timeline_filters(query, account, local_only)
  161. end
  162. def as_outbox_timeline(account)
  163. where(account: account, visibility: :public)
  164. end
  165. def favourites_map(status_ids, account_id)
  166. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  167. end
  168. def reblogs_map(status_ids, account_id)
  169. 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
  170. end
  171. def mutes_map(conversation_ids, account_id)
  172. ConversationMute.select('conversation_id').where(conversation_id: conversation_ids).where(account_id: account_id).map { |m| [m.conversation_id, true] }.to_h
  173. end
  174. def pins_map(status_ids, account_id)
  175. StatusPin.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |p| [p.status_id, true] }.to_h
  176. end
  177. def reload_stale_associations!(cached_items)
  178. account_ids = []
  179. cached_items.each do |item|
  180. account_ids << item.account_id
  181. account_ids << item.reblog.account_id if item.reblog?
  182. end
  183. account_ids.uniq!
  184. return if account_ids.empty?
  185. accounts = Account.where(id: account_ids).map { |a| [a.id, a] }.to_h
  186. cached_items.each do |item|
  187. item.account = accounts[item.account_id]
  188. item.reblog.account = accounts[item.reblog.account_id] if item.reblog?
  189. end
  190. end
  191. def permitted_for(target_account, account)
  192. visibility = [:public, :unlisted]
  193. if account.nil?
  194. where(visibility: visibility)
  195. elsif target_account.blocking?(account) # get rid of blocked peeps
  196. none
  197. elsif account.id == target_account.id # author can see own stuff
  198. all
  199. else
  200. # followers can see followers-only stuff, but also things they are mentioned in.
  201. # non-followers can see everything that isn't private/direct, but can see stuff they are mentioned in.
  202. visibility.push(:private) if account.following?(target_account)
  203. where(visibility: visibility).or(where(id: account.mentions.select(:status_id)))
  204. end
  205. end
  206. private
  207. def timeline_scope(local_only = false)
  208. starting_scope = local_only ? Status.local : Status
  209. starting_scope
  210. .with_public_visibility
  211. .without_reblogs
  212. end
  213. def apply_timeline_filters(query, account, local_only)
  214. if account.nil?
  215. filter_timeline_default(query)
  216. else
  217. filter_timeline_for_account(query, account, local_only)
  218. end
  219. end
  220. def filter_timeline_for_account(query, account, local_only)
  221. query = query.not_excluded_by_account(account)
  222. query = query.not_domain_blocked_by_account(account) unless local_only
  223. query = query.not_in_filtered_languages(account) if account.filtered_languages.present?
  224. query.merge(account_silencing_filter(account))
  225. end
  226. def filter_timeline_default(query)
  227. query.excluding_silenced_accounts
  228. end
  229. def account_silencing_filter(account)
  230. if account.silenced?
  231. including_silenced_accounts
  232. else
  233. excluding_silenced_accounts
  234. end
  235. end
  236. end
  237. private
  238. def store_uri
  239. update_attribute(:uri, ActivityPub::TagManager.instance.uri_for(self)) if uri.nil?
  240. end
  241. def prepare_contents
  242. text&.strip!
  243. spoiler_text&.strip!
  244. end
  245. def set_reblog
  246. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  247. end
  248. def set_visibility
  249. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  250. self.visibility = reblog.visibility if reblog?
  251. self.sensitive = false if sensitive.nil?
  252. end
  253. def set_sensitivity
  254. self.sensitive = sensitive || spoiler_text.present?
  255. end
  256. def set_conversation
  257. self.reply = !(in_reply_to_id.nil? && thread.nil?) unless reply
  258. if reply? && !thread.nil?
  259. self.in_reply_to_account_id = carried_over_reply_to_account_id
  260. self.conversation_id = thread.conversation_id if conversation_id.nil?
  261. elsif conversation_id.nil?
  262. self.conversation = Conversation.new
  263. end
  264. end
  265. def carried_over_reply_to_account_id
  266. if thread.account_id == account_id && thread.reply?
  267. thread.in_reply_to_account_id
  268. else
  269. thread.account_id
  270. end
  271. end
  272. def set_local
  273. self.local = account.local?
  274. end
  275. def update_statistics
  276. return unless public_visibility? || unlisted_visibility?
  277. ActivityTracker.increment('activity:statuses:local')
  278. end
  279. end