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.

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