闭社主体 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.

129 lines
5.0 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. class Status < ApplicationRecord
  2. include Paginable
  3. include Streamable
  4. belongs_to :account, -> { with_counters }, inverse_of: :statuses
  5. belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies
  6. belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, touch: true
  7. has_many :favourites, inverse_of: :status, dependent: :destroy
  8. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
  9. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
  10. has_many :mentions, dependent: :destroy
  11. has_many :media_attachments, dependent: :destroy
  12. has_and_belongs_to_many :tags
  13. validates :account, presence: true
  14. validates :uri, uniqueness: true, unless: 'local?'
  15. validates :text, presence: true, length: { maximum: 500 }, if: proc { |s| s.local? && !s.reblog? }
  16. validates :reblog, uniqueness: { scope: :account, message: 'of status already exists' }, if: 'reblog?'
  17. default_scope { order('id desc') }
  18. scope :with_counters, -> { select('statuses.*, (select count(r.id) from statuses as r where r.reblog_of_id = statuses.id) as reblogs_count, (select count(f.id) from favourites as f where f.status_id = statuses.id) as favourites_count') }
  19. scope :with_includes, -> { includes(:account, :media_attachments, :tags, :stream_entry, mentions: :account, reblog: [:account, mentions: :account], thread: :account) }
  20. def local?
  21. uri.nil?
  22. end
  23. def reblog?
  24. !reblog_of_id.nil?
  25. end
  26. def reply?
  27. !in_reply_to_id.nil?
  28. end
  29. def verb
  30. reblog? ? :share : :post
  31. end
  32. def object_type
  33. reply? ? :comment : :note
  34. end
  35. def content
  36. reblog? ? reblog.text : text
  37. end
  38. def target
  39. reblog
  40. end
  41. def title
  42. content
  43. end
  44. def reblogs_count
  45. attributes['reblogs_count'] || reblogs.count
  46. end
  47. def favourites_count
  48. attributes['favourites_count'] || favourites.count
  49. end
  50. def ancestors
  51. 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)
  52. statuses = Status.where(id: ids).with_counters.with_includes.group_by(&:id)
  53. ids.map { |id| statuses[id].first }
  54. end
  55. def descendants
  56. 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)
  57. statuses = Status.where(id: ids).with_counters.with_includes.group_by(&:id)
  58. ids.map { |id| statuses[id].first }
  59. end
  60. def reblogged_by(limit)
  61. Account.where(id: reblogs.limit(limit).pluck(:account_id)).with_counters
  62. end
  63. def favourited_by(limit)
  64. Account.where(id: favourites.limit(limit).pluck(:account_id)).with_counters
  65. end
  66. class << self
  67. def as_home_timeline(account)
  68. where(account: [account] + account.following).with_includes.with_counters
  69. end
  70. def as_mentions_timeline(account)
  71. where(id: Mention.where(account: account).pluck(:status_id)).with_includes.with_counters
  72. end
  73. def as_public_timeline(account)
  74. joins('LEFT OUTER JOIN statuses AS reblogs ON statuses.reblog_of_id = reblogs.id')
  75. .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  76. .where('accounts.silenced = FALSE')
  77. .where('(reblogs.account_id IS NULL OR reblogs.account_id NOT IN (SELECT target_account_id FROM blocks WHERE account_id = ?)) AND statuses.account_id NOT IN (SELECT target_account_id FROM blocks WHERE account_id = ?)', account.id, account.id)
  78. .with_includes
  79. .with_counters
  80. end
  81. def as_tag_timeline(tag, account)
  82. tag.statuses
  83. .joins('LEFT OUTER JOIN statuses AS reblogs ON statuses.reblog_of_id = reblogs.id')
  84. .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  85. .where('accounts.silenced = FALSE')
  86. .where('(reblogs.account_id IS NULL OR reblogs.account_id NOT IN (SELECT target_account_id FROM blocks WHERE account_id = ?)) AND statuses.account_id NOT IN (SELECT target_account_id FROM blocks WHERE account_id = ?)', account.id, account.id)
  87. .with_includes
  88. .with_counters
  89. end
  90. def favourites_map(status_ids, account_id)
  91. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  92. end
  93. def reblogs_map(status_ids, account_id)
  94. select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h
  95. end
  96. end
  97. before_validation do
  98. text.strip!
  99. end
  100. end