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.

77 lines
2.7 KiB

8 years ago
8 years ago
8 years ago
  1. class Status < ActiveRecord::Base
  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. validates :account, presence: true
  12. validates :uri, uniqueness: true, unless: 'local?'
  13. validates :text, presence: true, if: Proc.new { |s| s.local? && !s.reblog? }
  14. 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') }
  15. scope :with_includes, -> { includes(:account, :mentions, reblog: [:account, :mentions], thread: [:account, :mentions]) }
  16. def local?
  17. self.uri.nil?
  18. end
  19. def reblog?
  20. !self.reblog_of_id.nil?
  21. end
  22. def reply?
  23. !self.in_reply_to_id.nil?
  24. end
  25. def verb
  26. reblog? ? :share : :post
  27. end
  28. def object_type
  29. reply? ? :comment : :note
  30. end
  31. def content
  32. reblog? ? self.reblog.text : self.text
  33. end
  34. def target
  35. self.reblog
  36. end
  37. def title
  38. content
  39. end
  40. def reblogs_count
  41. self.attributes['reblogs_count'] || self.reblogs.count
  42. end
  43. def favourites_count
  44. self.attributes['favourites_count'] || self.favourites.count
  45. end
  46. def ancestors
  47. 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])
  48. end
  49. def descendants
  50. 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])
  51. end
  52. def self.as_home_timeline(account)
  53. self.where(account: [account] + account.following).with_includes.with_counters
  54. end
  55. def self.as_mentions_timeline(account)
  56. self.where(id: Mention.where(account: account).pluck(:status_id)).with_includes.with_counters
  57. end
  58. end