闭社主体 forked from https://github.com/tootsuite/mastodon
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.

183 lines
6.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. class Account < ApplicationRecord
  3. include Targetable
  4. include PgSearch
  5. MENTION_RE = /(?:^|[^\/\w])@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i
  6. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
  7. # Local users
  8. has_one :user, inverse_of: :account
  9. 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?'
  10. validates :username, presence: true, uniqueness: { scope: :domain, case_sensitive: true }, unless: 'local?'
  11. # Avatar upload
  12. has_attached_file :avatar, styles: { large: '300x300#', medium: '96x96#', small: '48x48#' }
  13. validates_attachment_content_type :avatar, content_type: IMAGE_MIME_TYPES
  14. validates_attachment_size :avatar, less_than: 2.megabytes
  15. # Header upload
  16. has_attached_file :header, styles: { medium: '700x335#' }
  17. validates_attachment_content_type :header, content_type: IMAGE_MIME_TYPES
  18. validates_attachment_size :header, less_than: 2.megabytes
  19. # Local user profile validations
  20. validates :display_name, length: { maximum: 30 }, if: 'local?'
  21. validates :note, length: { maximum: 160 }, if: 'local?'
  22. # Timelines
  23. has_many :stream_entries, inverse_of: :account, dependent: :destroy
  24. has_many :statuses, inverse_of: :account, dependent: :destroy
  25. has_many :favourites, inverse_of: :account, dependent: :destroy
  26. has_many :mentions, inverse_of: :account, dependent: :destroy
  27. has_many :notifications, inverse_of: :account, dependent: :destroy
  28. # Follow relations
  29. has_many :active_relationships, class_name: 'Follow', foreign_key: 'account_id', dependent: :destroy
  30. has_many :passive_relationships, class_name: 'Follow', foreign_key: 'target_account_id', dependent: :destroy
  31. has_many :following, -> { order('follows.id desc') }, through: :active_relationships, source: :target_account
  32. has_many :followers, -> { order('follows.id desc') }, through: :passive_relationships, source: :account
  33. # Block relationships
  34. has_many :block_relationships, class_name: 'Block', foreign_key: 'account_id', dependent: :destroy
  35. has_many :blocking, -> { order('blocks.id desc') }, through: :block_relationships, source: :target_account
  36. has_many :media_attachments, dependent: :destroy
  37. pg_search_scope :search_for, against: { username: 'A', domain: 'B' }, using: { tsearch: { prefix: true } }
  38. scope :remote, -> { where.not(domain: nil) }
  39. scope :local, -> { where(domain: nil) }
  40. scope :without_followers, -> { where('(select count(f.id) from follows as f where f.target_account_id = accounts.id) = 0') }
  41. scope :with_followers, -> { where('(select count(f.id) from follows as f where f.target_account_id = accounts.id) > 0') }
  42. scope :expiring, -> (time) { where(subscription_expires_at: nil).or(where('subscription_expires_at < ?', time)).remote.with_followers }
  43. 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') }
  44. def follow!(other_account)
  45. active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
  46. end
  47. def block!(other_account)
  48. block_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
  49. end
  50. def unfollow!(other_account)
  51. follow = active_relationships.find_by(target_account: other_account)
  52. follow.destroy unless follow.nil?
  53. end
  54. def unblock!(other_account)
  55. block = block_relationships.find_by(target_account: other_account)
  56. block.destroy unless block.nil?
  57. end
  58. def following?(other_account)
  59. following.include?(other_account)
  60. end
  61. def blocking?(other_account)
  62. blocking.include?(other_account)
  63. end
  64. def local?
  65. domain.nil?
  66. end
  67. def acct
  68. local? ? username : "#{username}@#{domain}"
  69. end
  70. def subscribed?
  71. !subscription_expires_at.nil?
  72. end
  73. def favourited?(status)
  74. (status.reblog? ? status.reblog : status).favourites.where(account: self).count.positive?
  75. end
  76. def reblogged?(status)
  77. (status.reblog? ? status.reblog : status).reblogs.where(account: self).count.positive?
  78. end
  79. def keypair
  80. private_key.nil? ? OpenSSL::PKey::RSA.new(public_key) : OpenSSL::PKey::RSA.new(private_key)
  81. end
  82. def subscription(webhook_url)
  83. OStatus2::Subscription.new(remote_url, secret: secret, lease_seconds: 86_400 * 30, webhook: webhook_url, hub: hub_url)
  84. end
  85. def ping!(atom_url, hubs)
  86. return unless local? && !Rails.env.development?
  87. OStatus2::Publication.new(atom_url, hubs).publish
  88. end
  89. def avatar_remote_url=(url)
  90. self.avatar = URI.parse(url) unless self[:avatar_remote_url] == url
  91. self[:avatar_remote_url] = url
  92. rescue OpenURI::HTTPError => e
  93. Rails.logger.debug "Error fetching remote avatar: #{e}"
  94. end
  95. def object_type
  96. :person
  97. end
  98. def to_param
  99. username
  100. end
  101. def common_followers_with(other_account)
  102. 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)
  103. ids = results['data'].map(&:first)
  104. accounts = Account.where(id: ids).with_counters.limit(20).map { |a| [a.id, a] }.to_h
  105. ids.map { |id| accounts[id] }.compact
  106. rescue Neography::NeographyError, Excon::Error::Socket
  107. []
  108. end
  109. class << self
  110. def find_local!(username)
  111. find_remote!(username, nil)
  112. end
  113. def find_remote!(username, domain)
  114. where(arel_table[:username].matches(username.gsub(/[%_]/, '\\\\\0'))).where(domain.nil? ? { domain: nil } : arel_table[:domain].matches(domain.gsub(/[%_]/, '\\\\\0'))).take!
  115. end
  116. def find_local(username)
  117. find_local!(username)
  118. rescue ActiveRecord::RecordNotFound
  119. nil
  120. end
  121. def find_remote(username, domain)
  122. find_remote!(username, domain)
  123. rescue ActiveRecord::RecordNotFound
  124. nil
  125. end
  126. def following_map(target_account_ids, account_id)
  127. Follow.where(target_account_id: target_account_ids).where(account_id: account_id).map { |f| [f.target_account_id, true] }.to_h
  128. end
  129. def followed_by_map(target_account_ids, account_id)
  130. Follow.where(account_id: target_account_ids).where(target_account_id: account_id).map { |f| [f.account_id, true] }.to_h
  131. end
  132. def blocking_map(target_account_ids, account_id)
  133. Block.where(target_account_id: target_account_ids).where(account_id: account_id).map { |b| [b.target_account_id, true] }.to_h
  134. end
  135. end
  136. before_create do
  137. if local?
  138. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048)
  139. self.private_key = keypair.to_pem
  140. self.public_key = keypair.public_key.to_pem
  141. end
  142. end
  143. end