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.

190 lines
6.8 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. class Status < ApplicationRecord
  3. include ActiveModel::Validations
  4. include Paginable
  5. include Streamable
  6. include Cacheable
  7. enum visibility: [:public, :unlisted, :private], _suffix: :visibility
  8. belongs_to :application, class_name: 'Doorkeeper::Application'
  9. belongs_to :account, inverse_of: :statuses
  10. belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account'
  11. belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies
  12. belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs
  13. has_many :favourites, inverse_of: :status, dependent: :destroy
  14. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
  15. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
  16. has_many :mentions, dependent: :destroy
  17. has_many :media_attachments, dependent: :destroy
  18. has_and_belongs_to_many :tags
  19. has_one :notification, as: :activity, dependent: :destroy
  20. has_one :preview_card, dependent: :destroy
  21. validates :account, presence: true
  22. validates :uri, uniqueness: true, unless: 'local?'
  23. validates :text, presence: true, unless: 'reblog?'
  24. validates_with StatusLengthValidator
  25. validates :reblog, uniqueness: { scope: :account, message: 'of status already exists' }, if: 'reblog?'
  26. default_scope { order('id desc') }
  27. scope :remote, -> { where.not(uri: nil) }
  28. scope :local, -> { where(uri: nil) }
  29. cache_associated :account, :application, :media_attachments, :tags, :stream_entry, mentions: :account, reblog: [:account, :application, :stream_entry, :tags, :media_attachments, mentions: :account], thread: :account
  30. def reply?
  31. super || !in_reply_to_id.nil?
  32. end
  33. def local?
  34. uri.nil?
  35. end
  36. def reblog?
  37. !reblog_of_id.nil?
  38. end
  39. def verb
  40. reblog? ? :share : :post
  41. end
  42. def object_type
  43. reply? ? :comment : :note
  44. end
  45. def content
  46. reblog? ? reblog.text : text
  47. end
  48. def target
  49. reblog
  50. end
  51. def title
  52. content
  53. end
  54. def hidden?
  55. private_visibility?
  56. end
  57. def permitted?(other_account = nil)
  58. private_visibility? ? (account.id == other_account&.id || other_account&.following?(account)) : other_account.nil? || !account.blocking?(other_account)
  59. end
  60. def ancestors(account = nil)
  61. ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, in_reply_to_id, path) AS (SELECT id, in_reply_to_id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id FROM search_tree JOIN statuses ON statuses.id = search_tree.in_reply_to_id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path DESC', id]) - [self]).pluck(:id)
  62. statuses = Status.where(id: ids).group_by(&:id)
  63. results = ids.map { |id| statuses[id].first }
  64. results = results.reject { |status| filter_from_context?(status, account) }
  65. results
  66. end
  67. def descendants(account = nil)
  68. ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, path) AS (SELECT id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, path || statuses.id FROM search_tree JOIN statuses ON statuses.in_reply_to_id = search_tree.id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path', id]) - [self]).pluck(:id)
  69. statuses = Status.where(id: ids).group_by(&:id)
  70. results = ids.map { |id| statuses[id].first }
  71. results = results.reject { |status| filter_from_context?(status, account) }
  72. results
  73. end
  74. class << self
  75. def as_home_timeline(account)
  76. where(account: [account] + account.following)
  77. end
  78. def as_public_timeline(account = nil, local_only = false)
  79. query = joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  80. .where(visibility: :public)
  81. .where('(statuses.reply = false OR statuses.in_reply_to_account_id = statuses.account_id)')
  82. .where('statuses.reblog_of_id IS NULL')
  83. query = query.where('accounts.domain IS NULL') if local_only
  84. account.nil? ? filter_timeline_default(query) : filter_timeline_default(filter_timeline(query, account))
  85. end
  86. def as_tag_timeline(tag, account = nil, local_only = false)
  87. query = tag.statuses
  88. .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  89. .where(visibility: :public)
  90. .where('statuses.reblog_of_id IS NULL')
  91. query = query.where('accounts.domain IS NULL') if local_only
  92. account.nil? ? filter_timeline_default(query) : filter_timeline_default(filter_timeline(query, account))
  93. end
  94. def favourites_map(status_ids, account_id)
  95. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  96. end
  97. def reblogs_map(status_ids, account_id)
  98. select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h
  99. end
  100. def reload_stale_associations!(cached_items)
  101. account_ids = []
  102. cached_items.each do |item|
  103. account_ids << item.account_id
  104. account_ids << item.reblog.account_id if item.reblog?
  105. end
  106. accounts = Account.where(id: account_ids.uniq).map { |a| [a.id, a] }.to_h
  107. cached_items.each do |item|
  108. item.account = accounts[item.account_id]
  109. item.reblog.account = accounts[item.reblog.account_id] if item.reblog?
  110. end
  111. end
  112. def permitted_for(target_account, account)
  113. if account&.id == target_account.id || account&.following?(target_account)
  114. where('1 = 1')
  115. elsif !account.nil? && target_account.blocking?(account)
  116. where('1 = 0')
  117. else
  118. where.not(visibility: :private)
  119. end
  120. end
  121. private
  122. def filter_timeline(query, account)
  123. blocked = Block.where(account: account).pluck(:target_account_id) + Block.where(target_account: account).pluck(:account_id)
  124. query = query.where('statuses.account_id NOT IN (?)', blocked) unless blocked.empty?
  125. query = query.where('accounts.silenced = TRUE') if account.silenced?
  126. query
  127. end
  128. def filter_timeline_default(query)
  129. query.where('accounts.silenced = FALSE')
  130. end
  131. end
  132. before_validation do
  133. text.strip!
  134. spoiler_text&.strip!
  135. self.reply = !(in_reply_to_id.nil? && thread.nil?) unless attributes[:reply]
  136. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  137. self.in_reply_to_account_id = (thread.account_id == account_id && thread.reply? ? thread.in_reply_to_account_id : thread.account_id) if reply? && !thread.nil?
  138. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  139. end
  140. private
  141. def filter_from_context?(status, account)
  142. account&.blocking?(status.account_id) || !status.permitted?(account)
  143. end
  144. end