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.

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