follow_service.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # frozen_string_literal: true
  2. class FollowService < BaseService
  3. include Redisable
  4. include Payloadable
  5. include DomainControlHelper
  6. # Follow a remote user, notify remote user about the follow
  7. # @param [Account] source_account From which to follow
  8. # @param [Account] target_account Account to follow
  9. # @param [Hash] options
  10. # @option [Boolean] :reblogs Whether or not to show reblogs, defaults to true
  11. # @option [Boolean] :notify Whether to create notifications about new posts, defaults to false
  12. # @option [Array<String>] :languages Which languages to allow on the home feed from this account, defaults to all
  13. # @option [Boolean] :bypass_locked
  14. # @option [Boolean] :bypass_limit Allow following past the total follow number
  15. # @option [Boolean] :with_rate_limit
  16. def call(source_account, target_account, options = {})
  17. @source_account = source_account
  18. @target_account = target_account
  19. @options = { bypass_locked: false, bypass_limit: false, with_rate_limit: false }.merge(options)
  20. raise ActiveRecord::RecordNotFound if following_not_possible?
  21. raise Mastodon::NotPermittedError if following_not_allowed?
  22. if @source_account.following?(@target_account)
  23. return change_follow_options!
  24. elsif @source_account.requested?(@target_account)
  25. return change_follow_request_options!
  26. end
  27. ActivityTracker.increment('activity:interactions')
  28. # When an account follows someone for the first time, avoid showing
  29. # an empty home feed while the follow request is being processed
  30. # and the feeds are being merged
  31. mark_home_feed_as_partial! if @source_account.not_following_anyone?
  32. if (@target_account.locked? && !@options[:bypass_locked]) || @source_account.silenced? || @target_account.activitypub?
  33. request_follow!
  34. elsif @target_account.local?
  35. direct_follow!
  36. end
  37. end
  38. private
  39. def mark_home_feed_as_partial!
  40. redis.set("account:#{@source_account.id}:regeneration", true, nx: true, ex: 1.day.seconds)
  41. end
  42. def following_not_possible?
  43. @target_account.nil? || @target_account.id == @source_account.id || @target_account.suspended?
  44. end
  45. def following_not_allowed?
  46. domain_not_allowed?(@target_account.domain) || @target_account.blocking?(@source_account) || @source_account.blocking?(@target_account) || @target_account.moved? || (!@target_account.local? && @target_account.ostatus?) || @source_account.domain_blocking?(@target_account.domain)
  47. end
  48. def change_follow_options!
  49. @source_account.follow!(@target_account, **follow_options)
  50. end
  51. def change_follow_request_options!
  52. @source_account.request_follow!(@target_account, **follow_options)
  53. end
  54. def request_follow!
  55. follow_request = @source_account.request_follow!(@target_account, **follow_options.merge(rate_limit: @options[:with_rate_limit], bypass_limit: @options[:bypass_limit]))
  56. if @target_account.local?
  57. LocalNotificationWorker.perform_async(@target_account.id, follow_request.id, follow_request.class.name, 'follow_request')
  58. elsif @target_account.activitypub?
  59. ActivityPub::DeliveryWorker.perform_async(build_json(follow_request), @source_account.id, @target_account.inbox_url)
  60. end
  61. follow_request
  62. end
  63. def direct_follow!
  64. follow = @source_account.follow!(@target_account, **follow_options.merge(rate_limit: @options[:with_rate_limit], bypass_limit: @options[:bypass_limit]))
  65. LocalNotificationWorker.perform_async(@target_account.id, follow.id, follow.class.name, 'follow')
  66. MergeWorker.perform_async(@target_account.id, @source_account.id)
  67. follow
  68. end
  69. def build_json(follow_request)
  70. Oj.dump(serialize_payload(follow_request, ActivityPub::FollowSerializer))
  71. end
  72. def follow_options
  73. @options.slice(:reblogs, :notify, :languages)
  74. end
  75. end