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.

174 lines
6.3 KiB

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