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

135 lines
4.9 KiB

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