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.

97 lines
3.5 KiB

8 years ago
8 years ago
  1. class Status < ApplicationRecord
  2. include Paginable
  3. include Streamable
  4. belongs_to :account, 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. 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') }
  17. scope :with_includes, -> { includes(:account, :media_attachments, :stream_entry, mentions: :account, reblog: [:account, mentions: :account], thread: :account) }
  18. def local?
  19. uri.nil?
  20. end
  21. def reblog?
  22. !reblog_of_id.nil?
  23. end
  24. def reply?
  25. !in_reply_to_id.nil?
  26. end
  27. def verb
  28. reblog? ? :share : :post
  29. end
  30. def object_type
  31. reply? ? :comment : :note
  32. end
  33. def content
  34. reblog? ? reblog.text : text
  35. end
  36. def target
  37. reblog
  38. end
  39. def title
  40. content
  41. end
  42. def reblogs_count
  43. attributes['reblogs_count'] || reblogs.count
  44. end
  45. def favourites_count
  46. attributes['favourites_count'] || favourites.count
  47. end
  48. def ancestors
  49. 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)
  50. statuses = Status.where(id: ids).with_counters.with_includes.group_by(&:id)
  51. ids.map { |id| statuses[id].first }
  52. end
  53. def descendants
  54. 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)
  55. statuses = Status.where(id: ids).with_counters.with_includes.group_by(&:id)
  56. ids.map { |id| statuses[id].first }
  57. end
  58. def self.as_home_timeline(account)
  59. where(account: [account] + account.following).with_includes.with_counters
  60. end
  61. def self.as_mentions_timeline(account)
  62. where(id: Mention.where(account: account).pluck(:status_id)).with_includes.with_counters
  63. end
  64. def self.favourites_map(status_ids, account_id)
  65. Favourite.where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  66. end
  67. def self.reblogs_map(status_ids, account_id)
  68. where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h
  69. end
  70. before_validation do
  71. text.strip!
  72. end
  73. end