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

191 lines
6.6 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: { original: '120x120#' }, convert_options: { all: '-quality 80 -strip' }
  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: { original: '700x335#' }, convert_options: { all: '-quality 80 -strip' }
  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. # Media
  37. has_many :media_attachments, dependent: :destroy
  38. # PuSH subscriptions
  39. has_many :subscriptions, dependent: :destroy
  40. pg_search_scope :search_for, against: { username: 'A', domain: 'B' },
  41. using: { tsearch: { prefix: true } }
  42. scope :remote, -> { where.not(domain: nil) }
  43. scope :local, -> { where(domain: nil) }
  44. scope :without_followers, -> { where('(select count(f.id) from follows as f where f.target_account_id = accounts.id) = 0') }
  45. scope :with_followers, -> { where('(select count(f.id) from follows as f where f.target_account_id = accounts.id) > 0') }
  46. scope :expiring, ->(time) { where(subscription_expires_at: nil).or(where('subscription_expires_at < ?', time)).remote.with_followers }
  47. scope :silenced, -> { where(silenced: true) }
  48. scope :suspended, -> { where(suspended: true) }
  49. scope :recent, -> { reorder('id desc') }
  50. scope :alphabetic, -> { order('domain ASC, username ASC') }
  51. def follow!(other_account)
  52. active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
  53. end
  54. def block!(other_account)
  55. block_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
  56. end
  57. def unfollow!(other_account)
  58. follow = active_relationships.find_by(target_account: other_account)
  59. follow&.destroy
  60. end
  61. def unblock!(other_account)
  62. block = block_relationships.find_by(target_account: other_account)
  63. block&.destroy
  64. end
  65. def following?(other_account)
  66. following.include?(other_account)
  67. end
  68. def blocking?(other_account)
  69. blocking.include?(other_account)
  70. end
  71. def local?
  72. domain.nil?
  73. end
  74. def acct
  75. local? ? username : "#{username}@#{domain}"
  76. end
  77. def subscribed?
  78. !subscription_expires_at.nil?
  79. end
  80. def favourited?(status)
  81. (status.reblog? ? status.reblog : status).favourites.where(account: self).count.positive?
  82. end
  83. def reblogged?(status)
  84. (status.reblog? ? status.reblog : status).reblogs.where(account: self).count.positive?
  85. end
  86. def keypair
  87. private_key.nil? ? OpenSSL::PKey::RSA.new(public_key) : OpenSSL::PKey::RSA.new(private_key)
  88. end
  89. def subscription(webhook_url)
  90. OStatus2::Subscription.new(remote_url, secret: secret, lease_seconds: 86_400 * 30, webhook: webhook_url, hub: hub_url)
  91. end
  92. def save_with_optional_avatar!
  93. save!
  94. rescue ActiveRecord::RecordInvalid => invalid
  95. if invalid.record.errors[:avatar_file_size] || invalid[:avatar_content_type]
  96. self.avatar = nil
  97. retry
  98. end
  99. raise invalid
  100. end
  101. def avatar_remote_url=(url)
  102. parsed_url = URI.parse(url)
  103. return if !%w(http https).include?(parsed_url.scheme) || self[:avatar_remote_url] == url
  104. self.avatar = parsed_url
  105. self[:avatar_remote_url] = url
  106. rescue OpenURI::HTTPError => e
  107. Rails.logger.debug "Error fetching remote avatar: #{e}"
  108. end
  109. def object_type
  110. :person
  111. end
  112. def to_param
  113. username
  114. end
  115. class << self
  116. def find_local!(username)
  117. find_remote!(username, nil)
  118. end
  119. def find_remote!(username, domain)
  120. where(arel_table[:username].matches(username.gsub(/[%_]/, '\\\\\0'))).where(domain.nil? ? { domain: nil } : arel_table[:domain].matches(domain.gsub(/[%_]/, '\\\\\0'))).take!
  121. end
  122. def find_local(username)
  123. find_local!(username)
  124. rescue ActiveRecord::RecordNotFound
  125. nil
  126. end
  127. def find_remote(username, domain)
  128. find_remote!(username, domain)
  129. rescue ActiveRecord::RecordNotFound
  130. nil
  131. end
  132. def following_map(target_account_ids, account_id)
  133. Follow.where(target_account_id: target_account_ids).where(account_id: account_id).map { |f| [f.target_account_id, true] }.to_h
  134. end
  135. def followed_by_map(target_account_ids, account_id)
  136. Follow.where(account_id: target_account_ids).where(target_account_id: account_id).map { |f| [f.account_id, true] }.to_h
  137. end
  138. def blocking_map(target_account_ids, account_id)
  139. Block.where(target_account_id: target_account_ids).where(account_id: account_id).map { |b| [b.target_account_id, true] }.to_h
  140. end
  141. end
  142. before_create do
  143. if local?
  144. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048)
  145. self.private_key = keypair.to_pem
  146. self.public_key = keypair.public_key.to_pem
  147. end
  148. end
  149. end