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.

86 lines
3.2 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, if: Proc.new { |s| s.local? && !s.reblog? }
  15. 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') }
  16. scope :with_includes, -> { includes(:account, :media_attachments, :stream_entry, mentions: :account, reblog: [:account, mentions: :account], thread: :account) }
  17. def local?
  18. self.uri.nil?
  19. end
  20. def reblog?
  21. !self.reblog_of_id.nil?
  22. end
  23. def reply?
  24. !self.in_reply_to_id.nil?
  25. end
  26. def verb
  27. reblog? ? :share : :post
  28. end
  29. def object_type
  30. reply? ? :comment : :note
  31. end
  32. def content
  33. reblog? ? self.reblog.text : self.text
  34. end
  35. def target
  36. self.reblog
  37. end
  38. def title
  39. content
  40. end
  41. def reblogs_count
  42. self.attributes['reblogs_count'] || self.reblogs.count
  43. end
  44. def favourites_count
  45. self.attributes['favourites_count'] || self.favourites.count
  46. end
  47. def ancestors
  48. Status.where(id: 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', self.id]) - [self])
  49. end
  50. def descendants
  51. Status.where(id: 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', self.id]) - [self])
  52. end
  53. def self.as_home_timeline(account)
  54. self.where(account: [account] + account.following).with_includes.with_counters
  55. end
  56. def self.as_mentions_timeline(account)
  57. self.where(id: Mention.where(account: account).pluck(:status_id)).with_includes.with_counters
  58. end
  59. def self.favourites_map(status_ids, account_id)
  60. Favourite.where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h
  61. end
  62. def self.reblogs_map(status_ids, account_id)
  63. self.where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h
  64. end
  65. end