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

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