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.

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