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.

307 lines
10 KiB

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