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

162 lines
5.6 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. def follow!(other_account)
  40. active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
  41. end
  42. def block!(other_account)
  43. block_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
  44. end
  45. def unfollow!(other_account)
  46. follow = active_relationships.find_by(target_account: other_account)
  47. follow.destroy unless follow.nil?
  48. end
  49. def unblock!(other_account)
  50. block = block_relationships.find_by(target_account: other_account)
  51. block.destroy unless block.nil?
  52. end
  53. def following?(other_account)
  54. following.include?(other_account)
  55. end
  56. def blocking?(other_account)
  57. blocking.include?(other_account)
  58. end
  59. def local?
  60. domain.nil?
  61. end
  62. def acct
  63. local? ? username : "#{username}@#{domain}"
  64. end
  65. def subscribed?
  66. !subscription_expires_at.nil?
  67. end
  68. def favourited?(status)
  69. (status.reblog? ? status.reblog : status).favourites.where(account: self).count > 0
  70. end
  71. def reblogged?(status)
  72. (status.reblog? ? status.reblog : status).reblogs.where(account: self).count > 0
  73. end
  74. def keypair
  75. private_key.nil? ? OpenSSL::PKey::RSA.new(public_key) : OpenSSL::PKey::RSA.new(private_key)
  76. end
  77. def subscription(webhook_url)
  78. OStatus2::Subscription.new(remote_url, secret: secret, lease_seconds: 86_400 * 30, webhook: webhook_url, hub: hub_url)
  79. end
  80. def ping!(atom_url, hubs)
  81. return unless local? && !Rails.env.development?
  82. OStatus2::Publication.new(atom_url, hubs).publish
  83. end
  84. def avatar_remote_url=(url)
  85. self.avatar = URI.parse(url) unless self[:avatar_remote_url] == url
  86. self[:avatar_remote_url] = url
  87. end
  88. def object_type
  89. :person
  90. end
  91. def to_param
  92. username
  93. end
  94. def self.find_local!(username)
  95. find_remote!(username, nil)
  96. end
  97. def self.find_remote!(username, domain)
  98. where(arel_table[:username].matches(username)).where(domain.nil? ? { domain: nil } : arel_table[:domain].matches(domain)).take!
  99. end
  100. def self.find_local(username)
  101. find_local!(username)
  102. rescue ActiveRecord::RecordNotFound
  103. nil
  104. end
  105. def self.find_remote(username, domain)
  106. find_remote!(username, domain)
  107. rescue ActiveRecord::RecordNotFound
  108. nil
  109. end
  110. def self.following_map(target_account_ids, account_id)
  111. Follow.where(target_account_id: target_account_ids).where(account_id: account_id).map { |f| [f.target_account_id, true] }.to_h
  112. end
  113. def self.followed_by_map(target_account_ids, account_id)
  114. Follow.where(account_id: target_account_ids).where(target_account_id: account_id).map { |f| [f.account_id, true] }.to_h
  115. end
  116. def self.blocking_map(target_account_ids, account_id)
  117. Block.where(target_account_id: target_account_ids).where(account_id: account_id).map { |b| [b.target_account_id, true] }.to_h
  118. end
  119. before_create do
  120. if local?
  121. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048)
  122. self.private_key = keypair.to_pem
  123. self.public_key = keypair.public_key.to_pem
  124. end
  125. end
  126. end