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.

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