The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

138 lines
4.4 KiB

  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. 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. self.active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
  38. end
  39. def unfollow!(other_account)
  40. follow = self.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. self.domain.nil?
  48. end
  49. def acct
  50. local? ? self.username : "#{self.username}@#{self.domain}"
  51. end
  52. def subscribed?
  53. !(self.secret.blank? || self.verify_token.blank?)
  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. self.private_key.nil? ? OpenSSL::PKey::RSA.new(self.public_key) : OpenSSL::PKey::RSA.new(self.private_key)
  63. end
  64. def subscription(webhook_url)
  65. @subscription ||= OStatus2::Subscription.new(self.remote_url, secret: self.secret, token: self.verify_token, webhook: webhook_url, hub: self.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. unless self[:avatar_remote_url] == url
  73. self.avatar = URI.parse(url)
  74. end
  75. self[:avatar_remote_url] = url
  76. end
  77. def object_type
  78. :person
  79. end
  80. def to_param
  81. self.username
  82. end
  83. def self.find_local!(username)
  84. self.find_remote!(username, nil)
  85. end
  86. def self.find_remote!(username, domain)
  87. table = self.arel_table
  88. self.where(table[:username].matches(username)).where(domain: domain).take!
  89. end
  90. def self.find_local(username)
  91. self.find_local!(username)
  92. rescue ActiveRecord::RecordNotFound
  93. nil
  94. end
  95. def self.find_remote(username, domain)
  96. self.find_remote!(username, domain)
  97. rescue ActiveRecord::RecordNotFound
  98. nil
  99. end
  100. before_create do
  101. if local?
  102. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048)
  103. self.private_key = keypair.to_pem
  104. self.public_key = keypair.public_key.to_pem
  105. end
  106. end
  107. end