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.

92 lines
3.1 KiB

8 years ago
8 years ago
  1. class Status < ActiveRecord::Base
  2. belongs_to :account, inverse_of: :statuses
  3. belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies
  4. belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs
  5. has_one :stream_entry, as: :activity
  6. has_many :favourites, inverse_of: :status, dependent: :destroy
  7. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
  8. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread
  9. has_many :mentioned_accounts, class_name: 'Mention', dependent: :destroy
  10. validates :account, presence: true
  11. validates :uri, uniqueness: true, unless: 'local?'
  12. validates :text, presence: true, if: Proc.new { |s| s.local? && !s.reblog? }
  13. 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') }
  14. scope :with_includes, -> { includes(:account, reblog: :account, thread: :account) }
  15. scope :paginate_by_max_id, -> (limit, max_id) { order('id desc').limit(limit).where(max_id.nil? ? '1=1' : ['id < ?', max_id]) }
  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 mentions
  47. m = []
  48. m << thread.account if reply?
  49. m << reblog.account if reblog?
  50. unless reblog?
  51. self.text.scan(Account::MENTION_RE).each do |match|
  52. uri = match.first
  53. username, domain = uri.split('@')
  54. account = Account.find_by(username: username, domain: domain)
  55. m << account unless account.nil?
  56. end
  57. end
  58. m
  59. end
  60. def ancestors
  61. 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])
  62. end
  63. def descendants
  64. 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])
  65. end
  66. after_create do
  67. self.account.stream_entries.create!(activity: self)
  68. end
  69. end