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.

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