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.

137 lines
4.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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. class << self
  61. def as_home_timeline(account)
  62. where(account: [account] + account.following).with_includes.with_counters
  63. end
  64. def as_mentions_timeline(account)
  65. where(id: Mention.where(account: account).pluck(:status_id)).with_includes.with_counters
  66. end
  67. def as_public_timeline(account = nil)
  68. query = joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id').where('accounts.silenced = FALSE')
  69. unless account.nil?
  70. query = filter_timeline(query, account)
  71. end
  72. query.with_includes.with_counters
  73. end
  74. def as_tag_timeline(tag, account = nil)
  75. query = tag.statuses
  76. .joins('LEFT OUTER JOIN accounts ON statuses.account_id = accounts.id')
  77. .where('accounts.silenced = FALSE')
  78. unless account.nil?
  79. query = filter_timeline(query, account)
  80. end
  81. query.with_includes.with_counters
  82. end
  83. def favourites_map(status_ids, account_id)
  84. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  85. end
  86. def reblogs_map(status_ids, account_id)
  87. select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h
  88. end
  89. private
  90. def filter_timeline(query, account)
  91. blocked = Block.where(account: account).pluck(:target_account_id)
  92. query
  93. .joins('LEFT OUTER JOIN statuses AS parents ON statuses.in_reply_to_id = parents.id')
  94. .joins('LEFT OUTER JOIN statuses AS reblogs ON statuses.reblog_of_id = reblogs.id')
  95. .where('parents.account_id NOT IN (?)', blocked)
  96. .where('statuses.account_id NOT IN (?)', blocked)
  97. .where('(reblogs.id IS NULL OR reblogs.account_id NOT IN (?))', blocked)
  98. end
  99. end
  100. before_validation do
  101. text.strip!
  102. end
  103. end