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.

71 lines
1.5 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'
  4. belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status'
  5. has_one :stream_entry, as: :activity, dependent: :destroy
  6. has_many :favourites, inverse_of: :status, dependent: :destroy
  7. has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status'
  8. has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status'
  9. validates :account, presence: true
  10. validates :uri, uniqueness: true, unless: 'local?'
  11. def local?
  12. self.uri.nil?
  13. end
  14. def reblog?
  15. !self.reblog_of_id.nil?
  16. end
  17. def reply?
  18. !self.in_reply_to_id.nil?
  19. end
  20. def verb
  21. reblog? ? :share : :post
  22. end
  23. def object_type
  24. reply? ? :comment : :note
  25. end
  26. def content
  27. reblog? ? self.reblog.text : self.text
  28. end
  29. def target
  30. self.reblog
  31. end
  32. def title
  33. content.truncate(80, omission: "...")
  34. end
  35. def mentions
  36. m = []
  37. m << thread.account if reply?
  38. m << reblog.account if reblog?
  39. unless reblog?
  40. self.text.scan(Account::MENTION_RE).each do |match|
  41. uri = match.first
  42. username = uri.split('@').first
  43. domain = uri.split('@').size == 2 ? uri.split('@').last : nil
  44. account = Account.find_by(username: username, domain: domain)
  45. m << account unless account.nil?
  46. end
  47. end
  48. m
  49. end
  50. after_create do
  51. self.account.stream_entries.create!(activity: self)
  52. end
  53. end