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.

187 lines
6.7 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. validates :account, presence: true
  20. validates :uri, uniqueness: true, unless: 'local?'
  21. validates :text, presence: true, length: { maximum: 500 }, if: proc { |s| s.local? && !s.reblog? }
  22. validates :text, presence: true, if: proc { |s| !s.local? && !s.reblog? }
  23. validates :reblog, uniqueness: { scope: :account, message: 'of status already exists' }, if: 'reblog?'
  24. default_scope { order('id desc') }
  25. scope :remote, -> { where.not(uri: nil) }
  26. scope :local, -> { where(uri: nil) }
  27. cache_associated :account, :application, :media_attachments, :tags, :stream_entry, mentions: :account, reblog: [:account, :application, :stream_entry, :tags, :media_attachments, mentions: :account], thread: :account
  28. def local?
  29. uri.nil?
  30. end
  31. def reblog?
  32. !reblog_of_id.nil?
  33. end
  34. def reply?
  35. !in_reply_to_id.nil?
  36. end
  37. def verb
  38. reblog? ? :share : :post
  39. end
  40. def object_type
  41. reply? ? :comment : :note
  42. end
  43. def content
  44. reblog? ? reblog.text : text
  45. end
  46. def target
  47. reblog
  48. end
  49. def title
  50. content
  51. end
  52. def hidden?
  53. private_visibility?
  54. end
  55. def permitted?(other_account = nil)
  56. private_visibility? ? (account.id == other_account&.id || other_account&.following?(account)) : other_account.nil? || !account.blocking?(other_account)
  57. end
  58. def ancestors(account = nil)
  59. 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)
  60. statuses = Status.where(id: ids).with_includes.group_by(&:id)
  61. results = ids.map { |id| statuses[id].first }
  62. results = results.reject { |status| filter_from_context?(status, account) }
  63. results
  64. end
  65. def descendants(account = nil)
  66. 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)
  67. statuses = Status.where(id: ids).with_includes.group_by(&:id)
  68. results = ids.map { |id| statuses[id].first }
  69. results = results.reject { |status| filter_from_context?(status, account) }
  70. results
  71. end
  72. class << self
  73. def as_home_timeline(account)
  74. where(account: [account] + account.following)
  75. end
  76. def as_mentions_timeline(account)
  77. where(id: Mention.where(account: account).select(:status_id))
  78. end
  79. def as_public_timeline(account = nil)
  80. query = joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  81. .where(visibility: :public)
  82. .where('(statuses.in_reply_to_id IS NULL OR statuses.in_reply_to_account_id = statuses.account_id)')
  83. .where('statuses.reblog_of_id IS NULL')
  84. account.nil? ? filter_timeline_default(query) : filter_timeline_default(filter_timeline(query, account))
  85. end
  86. def as_tag_timeline(tag, account = nil)
  87. query = tag.statuses
  88. .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  89. .where(visibility: :public)
  90. .where('(statuses.in_reply_to_id IS NULL OR statuses.in_reply_to_account_id = statuses.account_id)')
  91. .where('statuses.reblog_of_id IS NULL')
  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)
  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. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  135. self.in_reply_to_account_id = (thread.account_id == account_id && thread.reply? ? thread.in_reply_to_account_id : thread.account_id) if reply?
  136. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  137. end
  138. private
  139. def filter_from_context?(status, account)
  140. account&.blocking?(status.account) || !status.permitted?(account)
  141. end
  142. end