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.

90 lines
2.8 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. 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') }
  13. scope :with_includes, -> { includes(:account, reblog: :account, thread: :account) }
  14. def local?
  15. self.uri.nil?
  16. end
  17. def reblog?
  18. !self.reblog_of_id.nil?
  19. end
  20. def reply?
  21. !self.in_reply_to_id.nil?
  22. end
  23. def verb
  24. reblog? ? :share : :post
  25. end
  26. def object_type
  27. reply? ? :comment : :note
  28. end
  29. def content
  30. reblog? ? self.reblog.text : self.text
  31. end
  32. def target
  33. self.reblog
  34. end
  35. def title
  36. content
  37. end
  38. def reblogs_count
  39. self.attributes['reblogs_count'] || self.reblogs.count
  40. end
  41. def favourites_count
  42. self.attributes['favourites_count'] || self.favourites.count
  43. end
  44. def mentions
  45. m = []
  46. m << thread.account if reply?
  47. m << reblog.account if reblog?
  48. unless reblog?
  49. self.text.scan(Account::MENTION_RE).each do |match|
  50. uri = match.first
  51. username, domain = uri.split('@')
  52. account = Account.find_by(username: username, domain: domain)
  53. m << account unless account.nil?
  54. end
  55. end
  56. m
  57. end
  58. def ancestors
  59. 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])
  60. end
  61. def descendants
  62. 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])
  63. end
  64. after_create do
  65. self.account.stream_entries.create!(activity: self)
  66. end
  67. end