follow_request.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: follow_requests
  5. #
  6. # id :bigint(8) not null, primary key
  7. # created_at :datetime not null
  8. # updated_at :datetime not null
  9. # account_id :bigint(8) not null
  10. # target_account_id :bigint(8) not null
  11. # show_reblogs :boolean default(TRUE), not null
  12. # uri :string
  13. # notify :boolean default(FALSE), not null
  14. # languages :string is an Array
  15. #
  16. class FollowRequest < ApplicationRecord
  17. include Paginable
  18. include RelationshipCacheable
  19. include RateLimitable
  20. include FollowLimitable
  21. rate_limit by: :account, family: :follows
  22. belongs_to :account
  23. belongs_to :target_account, class_name: 'Account'
  24. has_one :notification, as: :activity, dependent: :destroy
  25. validates :account_id, uniqueness: { scope: :target_account_id }
  26. validates :languages, language: true
  27. def authorize!
  28. follow = account.follow!(target_account, reblogs: show_reblogs, notify: notify, languages: languages, uri: uri, bypass_limit: true)
  29. ListAccount.where(follow_request: self).update_all(follow_request_id: nil, follow_id: follow.id) # rubocop:disable Rails/SkipsModelValidations
  30. MergeWorker.perform_async(target_account.id, account.id) if account.local?
  31. destroy!
  32. end
  33. alias reject! destroy!
  34. def local?
  35. false # Force uri_for to use uri attribute
  36. end
  37. before_validation :set_uri, only: :create
  38. private
  39. def set_uri
  40. self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil?
  41. end
  42. end