follow_request.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. if account.local?
  30. ListAccount.where(follow_request: self).update_all(follow_request_id: nil, follow_id: follow.id)
  31. MergeWorker.perform_async(target_account.id, account.id, 'home')
  32. MergeWorker.push_bulk(List.where(account: account).joins(:list_accounts).where(list_accounts: { account_id: target_account.id }).pluck(:id)) do |list_id|
  33. [target_account.id, list_id, 'list']
  34. end
  35. end
  36. destroy!
  37. end
  38. alias reject! destroy!
  39. def local?
  40. false # Force uri_for to use uri attribute
  41. end
  42. before_validation :set_uri, only: :create
  43. after_commit :invalidate_follow_recommendations_cache
  44. private
  45. def set_uri
  46. self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil?
  47. end
  48. def invalidate_follow_recommendations_cache
  49. Rails.cache.delete("follow_recommendations/#{account_id}")
  50. end
  51. end