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.

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