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.

103 lines
4.0 KiB

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
  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. validates :account, presence: true
  13. validates :uri, uniqueness: true, unless: 'local?'
  14. validates :text, presence: true, length: { maximum: 500 }, if: proc { |s| s.local? && !s.reblog? }
  15. validates :reblog, uniqueness: { scope: :account, message: 'of status already exists' }, if: 'reblog?'
  16. default_scope { order('id desc') }
  17. 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') }
  18. scope :with_includes, -> { includes(:account, :media_attachments, :stream_entry, mentions: :account, reblog: [:account, mentions: :account], thread: :account) }
  19. def local?
  20. uri.nil?
  21. end
  22. def reblog?
  23. !reblog_of_id.nil?
  24. end
  25. def reply?
  26. !in_reply_to_id.nil?
  27. end
  28. def verb
  29. reblog? ? :share : :post
  30. end
  31. def object_type
  32. reply? ? :comment : :note
  33. end
  34. def content
  35. reblog? ? reblog.text : text
  36. end
  37. def target
  38. reblog
  39. end
  40. def title
  41. content
  42. end
  43. def reblogs_count
  44. attributes['reblogs_count'] || reblogs.count
  45. end
  46. def favourites_count
  47. attributes['favourites_count'] || favourites.count
  48. end
  49. def ancestors
  50. 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)
  51. statuses = Status.where(id: ids).with_counters.with_includes.group_by(&:id)
  52. ids.map { |id| statuses[id].first }
  53. end
  54. def descendants
  55. 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)
  56. statuses = Status.where(id: ids).with_counters.with_includes.group_by(&:id)
  57. ids.map { |id| statuses[id].first }
  58. end
  59. def self.as_home_timeline(account)
  60. where(account: [account] + account.following).with_includes.with_counters
  61. end
  62. def self.as_mentions_timeline(account)
  63. where(id: Mention.where(account: account).pluck(:status_id)).with_includes.with_counters
  64. end
  65. def self.as_public_timeline(account)
  66. joins('LEFT JOIN statuses AS reblogs ON reblogs.id = statuses.reblog_of_id').where('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).with_includes.with_counters
  67. end
  68. def self.favourites_map(status_ids, account_id)
  69. Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  70. end
  71. def self.reblogs_map(status_ids, account_id)
  72. select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h
  73. end
  74. before_validation do
  75. text.strip!
  76. end
  77. end