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.

116 lines
3.4 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. class Account < ApplicationRecord
  2. include Targetable
  3. # Local users
  4. has_one :user, inverse_of: :account
  5. validates :username, presence: true, uniqueness: { scope: :domain, case_sensitive: false }, if: 'local?'
  6. validates :username, presence: true, uniqueness: { scope: :domain, case_sensitive: true }, unless: 'local?'
  7. # Avatar upload
  8. has_attached_file :avatar, styles: { large: '300x300#', medium: '96x96#', small: '48x48#' }, default_url: 'avatars/missing.png'
  9. validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
  10. # Header upload
  11. has_attached_file :header, styles: { medium: '700x335#' }
  12. validates_attachment_content_type :header, content_type: /\Aimage\/.*\Z/
  13. # Local user profile validations
  14. validates :display_name, length: { maximum: 30 }, if: 'local?'
  15. validates :note, length: { maximum: 124 }, if: 'local?'
  16. # Timelines
  17. has_many :stream_entries, inverse_of: :account
  18. has_many :statuses, inverse_of: :account
  19. has_many :favourites, inverse_of: :account
  20. has_many :mentions, inverse_of: :account
  21. # Follow relations
  22. has_many :active_relationships, class_name: 'Follow', foreign_key: 'account_id', dependent: :destroy
  23. has_many :passive_relationships, class_name: 'Follow', foreign_key: 'target_account_id', dependent: :destroy
  24. has_many :following, through: :active_relationships, source: :target_account
  25. has_many :followers, through: :passive_relationships, source: :account
  26. MENTION_RE = /(?:^|\s|\.)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i
  27. def follow!(other_account)
  28. self.active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
  29. end
  30. def unfollow!(other_account)
  31. follow = self.active_relationships.find_by(target_account: other_account)
  32. follow.destroy unless follow.nil?
  33. end
  34. def following?(other_account)
  35. following.include?(other_account)
  36. end
  37. def local?
  38. self.domain.nil?
  39. end
  40. def acct
  41. local? ? self.username : "#{self.username}@#{self.domain}"
  42. end
  43. def subscribed?
  44. !(self.secret.blank? || self.verify_token.blank?)
  45. end
  46. def favourited?(status)
  47. (status.reblog? ? status.reblog : status).favourites.where(account: self).count == 1
  48. end
  49. def reblogged?(status)
  50. (status.reblog? ? status.reblog : status).reblogs.where(account: self).count == 1
  51. end
  52. def keypair
  53. self.private_key.nil? ? OpenSSL::PKey::RSA.new(self.public_key) : OpenSSL::PKey::RSA.new(self.private_key)
  54. end
  55. def subscription(webhook_url)
  56. @subscription ||= OStatus2::Subscription.new(self.remote_url, secret: self.secret, token: self.verify_token, webhook: webhook_url, hub: self.hub_url)
  57. end
  58. def ping!(atom_url, hubs)
  59. return unless local?
  60. OStatus2::Publication.new(atom_url, hubs).publish
  61. end
  62. def avatar_remote_url=(url)
  63. unless self[:avatar_remote_url] == url
  64. self.avatar = URI.parse(url)
  65. end
  66. self[:avatar_remote_url] = url
  67. end
  68. def object_type
  69. :person
  70. end
  71. def to_param
  72. self.username
  73. end
  74. def self.find_local!(username)
  75. table = self.arel_table
  76. self.where(table[:username].matches(username)).where(domain: nil).take!
  77. end
  78. def self.find_local(username)
  79. self.find_local!(username)
  80. rescue ActiveRecord::RecordNotFound
  81. nil
  82. end
  83. before_create do
  84. if local?
  85. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048)
  86. self.private_key = keypair.to_pem
  87. self.public_key = keypair.public_key.to_pem
  88. end
  89. end
  90. end