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.

80 lines
2.2 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. # Avatar upload
  5. attr_reader :avatar_remote_url
  6. has_attached_file :avatar, styles: { large: '300x300#', medium: '96x96#', small: '48x48#' }, default_url: 'avatars/missing.png'
  7. validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
  8. # Timelines
  9. has_many :stream_entries, inverse_of: :account
  10. has_many :statuses, inverse_of: :account
  11. has_many :favourites, inverse_of: :account
  12. # Follow relations
  13. has_many :active_relationships, class_name: 'Follow', foreign_key: 'account_id', dependent: :destroy
  14. has_many :passive_relationships, class_name: 'Follow', foreign_key: 'target_account_id', dependent: :destroy
  15. has_many :following, through: :active_relationships, source: :target_account
  16. has_many :followers, through: :passive_relationships, source: :account
  17. MENTION_RE = /(?:^|\W)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i
  18. def follow!(other_account)
  19. self.active_relationships.first_or_create!(target_account: other_account)
  20. end
  21. def unfollow!(other_account)
  22. self.active_relationships.find_by(target_account: other_account).destroy
  23. end
  24. def following?(other_account)
  25. following.include?(other_account)
  26. end
  27. def local?
  28. self.domain.nil?
  29. end
  30. def acct
  31. local? ? self.username : "#{self.username}@#{self.domain}"
  32. end
  33. def object_type
  34. :person
  35. end
  36. def title
  37. self.username
  38. end
  39. def content
  40. self.note
  41. end
  42. def subscribed?
  43. !(self.secret.blank? || self.verify_token.blank?)
  44. end
  45. def keypair
  46. self.private_key.nil? ? OpenSSL::PKey::RSA.new(self.public_key) : OpenSSL::PKey::RSA.new(self.private_key)
  47. end
  48. def subscription(webhook_url)
  49. @subscription ||= OStatus2::Subscription.new(self.remote_url, secret: self.secret, token: self.verify_token, webhook: webhook_url, hub: self.hub_url)
  50. end
  51. def avatar_remote_url=(url)
  52. self.avatar = URI.parse(url)
  53. @avatar_remote_url = url
  54. end
  55. before_create do
  56. if local?
  57. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 48 : 2048)
  58. self.private_key = keypair.to_pem
  59. self.public_key = keypair.public_key.to_pem
  60. end
  61. end
  62. end