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.

51 lines
1.4 KiB

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. # Follow relations
  8. has_many :active_relationships, class_name: 'Follow', foreign_key: 'account_id', dependent: :destroy
  9. has_many :passive_relationships, class_name: 'Follow', foreign_key: 'target_account_id', dependent: :destroy
  10. has_many :following, through: :active_relationships, source: :target_account
  11. has_many :followers, through: :passive_relationships, source: :account
  12. def follow!(other_account)
  13. self.active_relationships.create!(target_account: other_account)
  14. end
  15. def unfollow!(other_account)
  16. self.active_relationships.find_by(target_account: other_account).destroy
  17. end
  18. def following?(other_account)
  19. following.include?(other_account)
  20. end
  21. def local?
  22. self.domain.nil?
  23. end
  24. def acct
  25. local? ? self.username : "#{self.username}@#{self.domain}"
  26. end
  27. def object_type
  28. :person
  29. end
  30. def subscribed?
  31. !(self.secret.blank? || self.verify_token.blank?)
  32. end
  33. def keypair
  34. self.private_key.nil? ? OpenSSL::PKey::RSA.new(self.public_key) : OpenSSL::PKey::RSA.new(self.private_key)
  35. end
  36. def subscription(webhook_url)
  37. @subscription ||= OStatus2::Subscription.new(self.remote_url, secret: self.secret, token: self.verify_token, webhook: webhook_url, hub: self.hub_url)
  38. end
  39. end