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.

141 lines
4.7 KiB

8 years ago
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'].freeze
  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 }, length: { maximum: 30 }, 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. scope :remote, -> { where.not(domain: nil) }
  32. scope :local, -> { where(domain: nil) }
  33. scope :without_followers, -> { where('(select count(f.id) from follows as f where f.target_account_id = accounts.id) = 0') }
  34. scope :with_followers, -> { where('(select count(f.id) from follows as f where f.target_account_id = accounts.id) > 0') }
  35. scope :expiring, -> (time) { where(subscription_expires_at: nil).or(where('subscription_expires_at < ?', time)).remote.with_followers }
  36. def follow!(other_account)
  37. active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
  38. end
  39. def unfollow!(other_account)
  40. follow = active_relationships.find_by(target_account: other_account)
  41. follow.destroy unless follow.nil?
  42. end
  43. def following?(other_account)
  44. following.include?(other_account)
  45. end
  46. def local?
  47. domain.nil?
  48. end
  49. def acct
  50. local? ? username : "#{username}@#{domain}"
  51. end
  52. def subscribed?
  53. !subscription_expires_at.nil?
  54. end
  55. def favourited?(status)
  56. (status.reblog? ? status.reblog : status).favourites.where(account: self).count > 0
  57. end
  58. def reblogged?(status)
  59. (status.reblog? ? status.reblog : status).reblogs.where(account: self).count > 0
  60. end
  61. def keypair
  62. private_key.nil? ? OpenSSL::PKey::RSA.new(public_key) : OpenSSL::PKey::RSA.new(private_key)
  63. end
  64. def subscription(webhook_url)
  65. OStatus2::Subscription.new(remote_url, secret: secret, lease_seconds: 86_400 * 30, webhook: webhook_url, hub: hub_url)
  66. end
  67. def ping!(atom_url, hubs)
  68. return unless local? && !Rails.env.development?
  69. OStatus2::Publication.new(atom_url, hubs).publish
  70. end
  71. def avatar_remote_url=(url)
  72. self.avatar = URI.parse(url) unless self[:avatar_remote_url] == url
  73. self[:avatar_remote_url] = url
  74. end
  75. def object_type
  76. :person
  77. end
  78. def to_param
  79. username
  80. end
  81. def self.find_local!(username)
  82. find_remote!(username, nil)
  83. end
  84. def self.find_remote!(username, domain)
  85. where(arel_table[:username].matches(username)).where(domain: domain).take!
  86. end
  87. def self.find_local(username)
  88. find_local!(username)
  89. rescue ActiveRecord::RecordNotFound
  90. nil
  91. end
  92. def self.find_remote(username, domain)
  93. find_remote!(username, domain)
  94. rescue ActiveRecord::RecordNotFound
  95. nil
  96. end
  97. def self.following_map(target_account_ids, account_id)
  98. Follow.where(target_account_id: target_account_ids).where(account_id: account_id).map { |f| [f.target_account_id, true] }.to_h
  99. end
  100. def self.followed_by_map(target_account_ids, account_id)
  101. Follow.where(account_id: target_account_ids).where(target_account_id: account_id).map { |f| [f.account_id, true] }.to_h
  102. end
  103. before_create do
  104. if local?
  105. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048)
  106. self.private_key = keypair.to_pem
  107. self.public_key = keypair.public_key.to_pem
  108. end
  109. end
  110. end