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.

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