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

164 lines
6.0 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 self.find_local!(username)
  96. find_remote!(username, nil)
  97. end
  98. def self.find_remote!(username, domain)
  99. where(arel_table[:username].matches(username)).where(domain.nil? ? { domain: nil } : arel_table[:domain].matches(domain)).take!
  100. end
  101. def self.find_local(username)
  102. find_local!(username)
  103. rescue ActiveRecord::RecordNotFound
  104. nil
  105. end
  106. def self.find_remote(username, domain)
  107. find_remote!(username, domain)
  108. rescue ActiveRecord::RecordNotFound
  109. nil
  110. end
  111. def self.following_map(target_account_ids, account_id)
  112. Follow.where(target_account_id: target_account_ids).where(account_id: account_id).map { |f| [f.target_account_id, true] }.to_h
  113. end
  114. def self.followed_by_map(target_account_ids, account_id)
  115. Follow.where(account_id: target_account_ids).where(target_account_id: account_id).map { |f| [f.account_id, true] }.to_h
  116. end
  117. def self.blocking_map(target_account_ids, account_id)
  118. Block.where(target_account_id: target_account_ids).where(account_id: account_id).map { |b| [b.target_account_id, true] }.to_h
  119. end
  120. before_create do
  121. if local?
  122. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048)
  123. self.private_key = keypair.to_pem
  124. self.public_key = keypair.public_key.to_pem
  125. end
  126. end
  127. end