2019-08-30 08:02:41 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AccountSubscribeService < BaseService
|
|
|
|
# Subscribe a remote user
|
|
|
|
# @param [Account] source_account From which to subscribe
|
|
|
|
# @param [String, Account] uri User URI to subscribe in the form of username@domain (or account record)
|
2020-03-21 04:25:30 +00:00
|
|
|
def call(source_account, target_acct, options = {})
|
2020-11-15 06:57:58 +00:00
|
|
|
@source_account = source_account
|
|
|
|
@options = { show_reblogs: true, list_id: nil, media_only: false }.merge(options)
|
2020-03-21 04:25:30 +00:00
|
|
|
|
2020-01-05 06:36:28 +00:00
|
|
|
if target_acct.class.name == 'Account'
|
2020-11-15 06:57:58 +00:00
|
|
|
@target_account = target_acct
|
2020-01-05 06:36:28 +00:00
|
|
|
else
|
|
|
|
begin
|
2020-11-15 06:57:58 +00:00
|
|
|
@target_account = ResolveAccountService.new.call(target_acct, skip_webfinger: true)
|
|
|
|
@target_account ||= ResolveAccountService.new.call(target_acct, skip_webfinger: false)
|
2020-01-05 06:36:28 +00:00
|
|
|
rescue
|
2020-11-15 06:57:58 +00:00
|
|
|
@target_account = nil
|
2020-01-05 06:36:28 +00:00
|
|
|
end
|
2019-08-30 08:02:41 +00:00
|
|
|
end
|
|
|
|
|
2020-11-15 06:57:58 +00:00
|
|
|
raise ActiveRecord::RecordNotFound if @target_account.nil? || @target_account.id == @source_account.id || @target_account.suspended?
|
|
|
|
raise Mastodon::NotPermittedError if @target_account.blocking?(@source_account) || @source_account.blocking?(@target_account) || (!@target_account.local? && @target_account.ostatus?) || @source_account.domain_blocking?(@target_account.domain)
|
2019-08-30 08:02:41 +00:00
|
|
|
|
2020-11-15 06:57:58 +00:00
|
|
|
if @source_account.subscribing?(@target_account, @options[:list_id])
|
|
|
|
return change_subscribe_options!
|
2019-08-30 08:02:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
ActivityTracker.increment('activity:interactions')
|
|
|
|
|
2020-11-15 06:57:58 +00:00
|
|
|
subscribe = @source_account.subscribe!(@target_account, @options)
|
|
|
|
MergeWorker.perform_async(@target_account.id, @source_account.id, @options.merge(public_only: true))
|
2019-08-30 08:02:41 +00:00
|
|
|
subscribe
|
|
|
|
end
|
2020-11-15 06:57:58 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def change_subscribe_options!
|
|
|
|
@source_account.subscribe!(@target_account, @options)
|
|
|
|
end
|
|
|
|
|
2019-08-30 08:02:41 +00:00
|
|
|
end
|