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.

177 lines
6.5 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. class Account < ApplicationRecord
  2. include Targetable
  3. MENTION_RE = /(?:^|[^\/\w])@([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: 160 }, if: 'local?'
  20. # Timelines
  21. has_many :stream_entries, inverse_of: :account, dependent: :destroy
  22. has_many :statuses, inverse_of: :account, dependent: :destroy
  23. has_many :favourites, inverse_of: :account, dependent: :destroy
  24. has_many :mentions, inverse_of: :account, dependent: :destroy
  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, -> { order('follows.id desc') }, through: :active_relationships, source: :target_account
  29. has_many :followers, -> { order('follows.id desc') }, through: :passive_relationships, source: :account
  30. # Block relationships
  31. has_many :block_relationships, class_name: 'Block', foreign_key: 'account_id', dependent: :destroy
  32. has_many :blocking, -> { order('blocks.id desc') }, through: :block_relationships, source: :target_account
  33. has_many :media_attachments, dependent: :destroy
  34. scope :remote, -> { where.not(domain: nil) }
  35. scope :local, -> { where(domain: nil) }
  36. scope :without_followers, -> { where('(select count(f.id) from follows as f where f.target_account_id = accounts.id) = 0') }
  37. scope :with_followers, -> { where('(select count(f.id) from follows as f where f.target_account_id = accounts.id) > 0') }
  38. scope :expiring, -> (time) { where(subscription_expires_at: nil).or(where('subscription_expires_at < ?', time)).remote.with_followers }
  39. scope :with_counters, -> { select('accounts.*, (select count(f.id) from follows as f where f.target_account_id = accounts.id) as followers_count, (select count(f.id) from follows as f where f.account_id = accounts.id) as following_count, (select count(s.id) from statuses as s where s.account_id = accounts.id) as statuses_count') }
  40. def follow!(other_account)
  41. active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
  42. end
  43. def block!(other_account)
  44. block_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
  45. end
  46. def unfollow!(other_account)
  47. follow = active_relationships.find_by(target_account: other_account)
  48. follow.destroy unless follow.nil?
  49. end
  50. def unblock!(other_account)
  51. block = block_relationships.find_by(target_account: other_account)
  52. block.destroy unless block.nil?
  53. end
  54. def following?(other_account)
  55. following.include?(other_account)
  56. end
  57. def blocking?(other_account)
  58. blocking.include?(other_account)
  59. end
  60. def local?
  61. domain.nil?
  62. end
  63. def acct
  64. local? ? username : "#{username}@#{domain}"
  65. end
  66. def subscribed?
  67. !subscription_expires_at.nil?
  68. end
  69. def favourited?(status)
  70. (status.reblog? ? status.reblog : status).favourites.where(account: self).count > 0
  71. end
  72. def reblogged?(status)
  73. (status.reblog? ? status.reblog : status).reblogs.where(account: self).count > 0
  74. end
  75. def keypair
  76. private_key.nil? ? OpenSSL::PKey::RSA.new(public_key) : OpenSSL::PKey::RSA.new(private_key)
  77. end
  78. def subscription(webhook_url)
  79. OStatus2::Subscription.new(remote_url, secret: secret, lease_seconds: 86_400 * 30, webhook: webhook_url, hub: hub_url)
  80. end
  81. def ping!(atom_url, hubs)
  82. return unless local? && !Rails.env.development?
  83. OStatus2::Publication.new(atom_url, hubs).publish
  84. end
  85. def avatar_remote_url=(url)
  86. self.avatar = URI.parse(url) unless self[:avatar_remote_url] == url
  87. self[:avatar_remote_url] = url
  88. rescue OpenURI::HTTPError
  89. #
  90. end
  91. def object_type
  92. :person
  93. end
  94. def to_param
  95. username
  96. end
  97. def common_followers_with(other_account)
  98. results = Neography::Rest.new.execute_query('MATCH (a {account_id: {a_id}})-[:follows]->(b)-[:follows]->(c {account_id: {c_id}}) RETURN b.account_id', a_id: id, c_id: other_account.id)
  99. ids = results['data'].map(&:first)
  100. accounts = Account.where(id: ids).with_counters.limit(20).map { |a| [a.id, a] }.to_h
  101. ids.map { |id| accounts[id] }.compact
  102. rescue Neography::NeographyError, Excon::Error::Socket
  103. []
  104. end
  105. class << self
  106. def find_local!(username)
  107. find_remote!(username, nil)
  108. end
  109. def find_remote!(username, domain)
  110. where(arel_table[:username].matches(username)).where(domain.nil? ? { domain: nil } : arel_table[:domain].matches(domain)).take!
  111. end
  112. def find_local(username)
  113. find_local!(username)
  114. rescue ActiveRecord::RecordNotFound
  115. nil
  116. end
  117. def find_remote(username, domain)
  118. find_remote!(username, domain)
  119. rescue ActiveRecord::RecordNotFound
  120. nil
  121. end
  122. def following_map(target_account_ids, account_id)
  123. Follow.where(target_account_id: target_account_ids).where(account_id: account_id).map { |f| [f.target_account_id, true] }.to_h
  124. end
  125. def followed_by_map(target_account_ids, account_id)
  126. Follow.where(account_id: target_account_ids).where(target_account_id: account_id).map { |f| [f.account_id, true] }.to_h
  127. end
  128. def blocking_map(target_account_ids, account_id)
  129. Block.where(target_account_id: target_account_ids).where(account_id: account_id).map { |b| [b.target_account_id, true] }.to_h
  130. end
  131. end
  132. before_create do
  133. if local?
  134. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048)
  135. self.private_key = keypair.to_pem
  136. self.public_key = keypair.public_key.to_pem
  137. end
  138. end
  139. end