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.

173 lines
6.4 KiB

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, 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.created_at desc') }, through: :active_relationships, source: :target_account
  29. has_many :followers, -> { order('follows.created_at 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.created_at 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. end
  89. def object_type
  90. :person
  91. end
  92. def to_param
  93. username
  94. end
  95. def common_followers_with(other_account)
  96. 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)
  97. ids = results['data'].map(&:first)
  98. accounts = self.where(id: ids).with_counters.map { |a| [a.id, a] }.to_h
  99. ids.map { |id| accounts[id] }.compact
  100. rescue Neography::NeographyError, Excon::Error::Socket
  101. []
  102. end
  103. def self.find_local!(username)
  104. find_remote!(username, nil)
  105. end
  106. def self.find_remote!(username, domain)
  107. where(arel_table[:username].matches(username)).where(domain.nil? ? { domain: nil } : arel_table[:domain].matches(domain)).take!
  108. end
  109. def self.find_local(username)
  110. find_local!(username)
  111. rescue ActiveRecord::RecordNotFound
  112. nil
  113. end
  114. def self.find_remote(username, domain)
  115. find_remote!(username, domain)
  116. rescue ActiveRecord::RecordNotFound
  117. nil
  118. end
  119. def self.following_map(target_account_ids, account_id)
  120. Follow.where(target_account_id: target_account_ids).where(account_id: account_id).map { |f| [f.target_account_id, true] }.to_h
  121. end
  122. def self.followed_by_map(target_account_ids, account_id)
  123. Follow.where(account_id: target_account_ids).where(target_account_id: account_id).map { |f| [f.account_id, true] }.to_h
  124. end
  125. def self.blocking_map(target_account_ids, account_id)
  126. Block.where(target_account_id: target_account_ids).where(account_id: account_id).map { |b| [b.target_account_id, true] }.to_h
  127. end
  128. before_create do
  129. if local?
  130. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048)
  131. self.private_key = keypair.to_pem
  132. self.public_key = keypair.public_key.to_pem
  133. end
  134. end
  135. end