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.

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