闭社主体 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.

180 lines
6.7 KiB

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