闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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