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.

384 lines
12 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 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, counter_cache: true
  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, counter_cache: :reblogs_count, 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. after_create_commit :store_uri, if: :local?
  140. after_create_commit :update_statistics, if: :local?
  141. around_create Mastodon::Snowflake::Callbacks
  142. before_create :set_locality
  143. before_validation :prepare_contents, if: :local?
  144. before_validation :set_reblog
  145. before_validation :set_visibility
  146. before_validation :set_conversation
  147. before_validation :set_sensitivity
  148. before_validation :set_local
  149. class << self
  150. def not_in_filtered_languages(account)
  151. where(language: nil).or where.not(language: account.filtered_languages)
  152. end
  153. def as_home_timeline(account)
  154. where(account: [account] + account.following).where(visibility: [:public, :unlisted, :private])
  155. end
  156. def as_direct_timeline(account)
  157. query = joins("LEFT OUTER JOIN mentions ON statuses.id = mentions.status_id AND mentions.account_id = #{account.id}")
  158. .where("mentions.account_id = #{account.id} OR statuses.account_id = #{account.id}")
  159. .where(visibility: [:direct])
  160. apply_timeline_filters(query, account, false)
  161. end
  162. def as_public_timeline(account = nil, local_only = false)
  163. query = timeline_scope(local_only).without_replies
  164. apply_timeline_filters(query, account, local_only)
  165. end
  166. def as_tag_timeline(tag, account = nil, local_only = false)
  167. query = timeline_scope(local_only).tagged_with(tag)
  168. apply_timeline_filters(query, account, local_only)
  169. end
  170. def as_outbox_timeline(account)
  171. where(account: account, visibility: :public)
  172. end
  173. def favourites_map(status_ids, account_id)
  174. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  175. end
  176. def bookmarks_map(status_ids, account_id)
  177. Bookmark.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  178. end
  179. def reblogs_map(status_ids, account_id)
  180. 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
  181. end
  182. def mutes_map(conversation_ids, account_id)
  183. ConversationMute.select('conversation_id').where(conversation_id: conversation_ids).where(account_id: account_id).map { |m| [m.conversation_id, true] }.to_h
  184. end
  185. def pins_map(status_ids, account_id)
  186. StatusPin.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |p| [p.status_id, true] }.to_h
  187. end
  188. def reload_stale_associations!(cached_items)
  189. account_ids = []
  190. cached_items.each do |item|
  191. account_ids << item.account_id
  192. account_ids << item.reblog.account_id if item.reblog?
  193. end
  194. account_ids.uniq!
  195. return if account_ids.empty?
  196. accounts = Account.where(id: account_ids).map { |a| [a.id, a] }.to_h
  197. cached_items.each do |item|
  198. item.account = accounts[item.account_id]
  199. item.reblog.account = accounts[item.reblog.account_id] if item.reblog?
  200. end
  201. end
  202. def permitted_for(target_account, account)
  203. visibility = [:public, :unlisted]
  204. if account.nil?
  205. where(visibility: visibility).not_local_only
  206. elsif target_account.blocking?(account) # get rid of blocked peeps
  207. none
  208. elsif account.id == target_account.id # author can see own stuff
  209. all
  210. else
  211. # followers can see followers-only stuff, but also things they are mentioned in.
  212. # non-followers can see everything that isn't private/direct, but can see stuff they are mentioned in.
  213. visibility.push(:private) if account.following?(target_account)
  214. where(visibility: visibility).or(where(id: account.mentions.select(:status_id)))
  215. end
  216. end
  217. private
  218. def timeline_scope(local_only = false)
  219. starting_scope = local_only ? Status.local : Status
  220. starting_scope
  221. .with_public_visibility
  222. .without_reblogs
  223. end
  224. def apply_timeline_filters(query, account, local_only)
  225. if account.nil?
  226. filter_timeline_default(query)
  227. else
  228. filter_timeline_for_account(query, account, local_only)
  229. end
  230. end
  231. def filter_timeline_for_account(query, account, local_only)
  232. query = query.not_excluded_by_account(account)
  233. query = query.not_domain_blocked_by_account(account) unless local_only
  234. query = query.not_in_filtered_languages(account) if account.filtered_languages.present?
  235. query.merge(account_silencing_filter(account))
  236. end
  237. def filter_timeline_default(query)
  238. query.not_local_only.excluding_silenced_accounts
  239. end
  240. def account_silencing_filter(account)
  241. if account.silenced?
  242. including_silenced_accounts
  243. else
  244. excluding_silenced_accounts
  245. end
  246. end
  247. end
  248. def marked_local_only?
  249. # match both with and without U+FE0F (the emoji variation selector)
  250. /#{local_only_emoji}\ufe0f?\z/.match?(content)
  251. end
  252. def local_only_emoji
  253. '👁'
  254. end
  255. private
  256. def store_uri
  257. update_attribute(:uri, ActivityPub::TagManager.instance.uri_for(self)) if uri.nil?
  258. end
  259. def prepare_contents
  260. text&.strip!
  261. spoiler_text&.strip!
  262. end
  263. def set_reblog
  264. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  265. end
  266. def set_visibility
  267. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  268. self.visibility = reblog.visibility if reblog?
  269. self.sensitive = false if sensitive.nil?
  270. end
  271. def set_sensitivity
  272. self.sensitive = sensitive || spoiler_text.present?
  273. end
  274. def set_locality
  275. if account.domain.nil? && !attribute_changed?(:local_only)
  276. self.local_only = marked_local_only?
  277. end
  278. end
  279. def set_conversation
  280. self.reply = !(in_reply_to_id.nil? && thread.nil?) unless reply
  281. if reply? && !thread.nil?
  282. self.in_reply_to_account_id = carried_over_reply_to_account_id
  283. self.conversation_id = thread.conversation_id if conversation_id.nil?
  284. elsif conversation_id.nil?
  285. self.conversation = Conversation.new
  286. end
  287. end
  288. def carried_over_reply_to_account_id
  289. if thread.account_id == account_id && thread.reply?
  290. thread.in_reply_to_account_id
  291. else
  292. thread.account_id
  293. end
  294. end
  295. def set_local
  296. self.local = account.local?
  297. end
  298. def update_statistics
  299. return unless public_visibility? || unlisted_visibility?
  300. ActivityTracker.increment('activity:statuses:local')
  301. end
  302. end