闭社主体 forked from https://github.com/tootsuite/mastodon
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.

185 lines
6.6 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 :account, inverse_of: :statuses
  8. belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account'
  9. belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies
  10. belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, touch: true
  11. has_many :favourites, inverse_of: :status, dependent: :destroy
  12. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
  13. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
  14. has_many :mentions, dependent: :destroy
  15. has_many :media_attachments, dependent: :destroy
  16. has_and_belongs_to_many :tags
  17. has_one :notification, as: :activity, dependent: :destroy
  18. validates :account, presence: true
  19. validates :uri, uniqueness: true, unless: 'local?'
  20. validates :text, presence: true, length: { maximum: 500 }, if: proc { |s| s.local? && !s.reblog? }
  21. validates :text, presence: true, if: proc { |s| !s.local? && !s.reblog? }
  22. validates :reblog, uniqueness: { scope: :account, message: 'of status already exists' }, if: 'reblog?'
  23. default_scope { order('id desc') }
  24. scope :remote, -> { where.not(uri: nil) }
  25. scope :local, -> { where(uri: nil) }
  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)) : other_account.nil? || !account.blocking?(other_account)
  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. def permitted_for(target_account, account)
  112. if account&.id == target_account.id || account&.following?(target_account)
  113. where('1 = 1')
  114. elsif !account.nil? && target_account.blocking?(account)
  115. where('1 = 0')
  116. else
  117. where.not(visibility: :private)
  118. end
  119. end
  120. private
  121. def filter_timeline(query, account)
  122. blocked = Block.where(account: account).pluck(:target_account_id)
  123. query = query.where('statuses.account_id NOT IN (?)', blocked) unless blocked.empty?
  124. query = query.where('accounts.silenced = TRUE') if account.silenced?
  125. query
  126. end
  127. def filter_timeline_default(query)
  128. query.where('accounts.silenced = FALSE')
  129. end
  130. end
  131. before_validation do
  132. text.strip!
  133. self.reblog = reblog.reblog if reblog? && reblog.reblog?
  134. self.in_reply_to_account_id = (thread.account_id == account_id && thread.reply? ? thread.in_reply_to_account_id : thread.account_id) if reply?
  135. self.visibility = (account.locked? ? :private : :public) if visibility.nil?
  136. end
  137. private
  138. def filter_from_context?(status, account)
  139. account&.blocking?(status.account) || !status.permitted?(account)
  140. end
  141. end