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.

128 lines
3.7 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#' }
  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. has_many :media_attachments, dependent: :destroy
  27. MENTION_RE = /(?:^|\s|\.)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i
  28. def follow!(other_account)
  29. self.active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
  30. end
  31. def unfollow!(other_account)
  32. follow = self.active_relationships.find_by(target_account: other_account)
  33. follow.destroy unless follow.nil?
  34. end
  35. def following?(other_account)
  36. following.include?(other_account)
  37. end
  38. def local?
  39. self.domain.nil?
  40. end
  41. def acct
  42. local? ? self.username : "#{self.username}@#{self.domain}"
  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 > 0
  49. end
  50. def reblogged?(status)
  51. (status.reblog? ? status.reblog : status).reblogs.where(account: self).count > 0
  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? && !Rails.env.development?
  61. OStatus2::Publication.new(atom_url, hubs).publish
  62. end
  63. def avatar_remote_url=(url)
  64. unless self[:avatar_remote_url] == url
  65. self.avatar = URI.parse(url)
  66. end
  67. self[:avatar_remote_url] = url
  68. end
  69. def object_type
  70. :person
  71. end
  72. def to_param
  73. self.username
  74. end
  75. def self.find_local!(username)
  76. self.find_remote!(username, nil)
  77. end
  78. def self.find_remote!(username, domain)
  79. table = self.arel_table
  80. self.where(table[:username].matches(username)).where(domain: domain).take!
  81. end
  82. def self.find_local(username)
  83. self.find_local!(username)
  84. rescue ActiveRecord::RecordNotFound
  85. nil
  86. end
  87. def self.find_remote(username, domain)
  88. self.find_remote!(username, domain)
  89. rescue ActiveRecord::RecordNotFound
  90. nil
  91. end
  92. before_create do
  93. if local?
  94. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048)
  95. self.private_key = keypair.to_pem
  96. self.public_key = keypair.public_key.to_pem
  97. end
  98. end
  99. end