1
0

follow_request.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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)
  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. after_commit :invalidate_follow_recommendations_cache
  39. private
  40. def set_uri
  41. self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil?
  42. end
  43. def invalidate_follow_recommendations_cache
  44. Rails.cache.delete("follow_recommendations/#{account_id}")
  45. end
  46. end