status.rb 9.7 KB

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