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.

131 lines
3.9 KiB

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