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.

60 lines
1.5 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. class Account < ActiveRecord::Base
  2. # Local users
  3. has_one :user, inverse_of: :account
  4. # Timelines
  5. has_many :stream_entries, inverse_of: :account
  6. has_many :statuses, inverse_of: :account
  7. has_many :favourites, inverse_of: :account
  8. # Follow relations
  9. has_many :active_relationships, class_name: 'Follow', foreign_key: 'account_id', dependent: :destroy
  10. has_many :passive_relationships, class_name: 'Follow', foreign_key: 'target_account_id', dependent: :destroy
  11. has_many :following, through: :active_relationships, source: :target_account
  12. has_many :followers, through: :passive_relationships, source: :account
  13. def follow!(other_account)
  14. self.active_relationships.first_or_create!(target_account: other_account)
  15. end
  16. def unfollow!(other_account)
  17. self.active_relationships.find_by(target_account: other_account).destroy
  18. end
  19. def following?(other_account)
  20. following.include?(other_account)
  21. end
  22. def local?
  23. self.domain.nil?
  24. end
  25. def acct
  26. local? ? self.username : "#{self.username}@#{self.domain}"
  27. end
  28. def object_type
  29. :person
  30. end
  31. def title
  32. self.username
  33. end
  34. def content
  35. self.note
  36. end
  37. def subscribed?
  38. !(self.secret.blank? || self.verify_token.blank?)
  39. end
  40. def keypair
  41. self.private_key.nil? ? OpenSSL::PKey::RSA.new(self.public_key) : OpenSSL::PKey::RSA.new(self.private_key)
  42. end
  43. def subscription(webhook_url)
  44. @subscription ||= OStatus2::Subscription.new(self.remote_url, secret: self.secret, token: self.verify_token, webhook: webhook_url, hub: self.hub_url)
  45. end
  46. end