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.

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