闭社主体 forked from https://github.com/tootsuite/mastodon
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.

70 lines
1.8 KiB

8 years ago
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. MENTION_RE = /(?:^|\W)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i
  14. def follow!(other_account)
  15. self.active_relationships.first_or_create!(target_account: other_account)
  16. end
  17. def unfollow!(other_account)
  18. self.active_relationships.find_by(target_account: other_account).destroy
  19. end
  20. def following?(other_account)
  21. following.include?(other_account)
  22. end
  23. def local?
  24. self.domain.nil?
  25. end
  26. def acct
  27. local? ? self.username : "#{self.username}@#{self.domain}"
  28. end
  29. def object_type
  30. :person
  31. end
  32. def title
  33. self.username
  34. end
  35. def content
  36. self.note
  37. end
  38. def subscribed?
  39. !(self.secret.blank? || self.verify_token.blank?)
  40. end
  41. def keypair
  42. self.private_key.nil? ? OpenSSL::PKey::RSA.new(self.public_key) : OpenSSL::PKey::RSA.new(self.private_key)
  43. end
  44. def subscription(webhook_url)
  45. @subscription ||= OStatus2::Subscription.new(self.remote_url, secret: self.secret, token: self.verify_token, webhook: webhook_url, hub: self.hub_url)
  46. end
  47. before_create do
  48. if local?
  49. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 48 : 2048)
  50. self.private_key = keypair.to_pem
  51. self.public_key = keypair.public_key.to_pem
  52. end
  53. end
  54. end