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.

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