2016-11-15 15:56:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-24 11:57:29 +00:00
|
|
|
class UnfollowService < BaseService
|
2019-06-04 21:11:18 +00:00
|
|
|
include Payloadable
|
2022-05-26 20:14:47 +00:00
|
|
|
include Redisable
|
|
|
|
include Lockable
|
2019-06-04 21:11:18 +00:00
|
|
|
|
2016-02-24 11:57:29 +00:00
|
|
|
# Unfollow and notify the remote user
|
|
|
|
# @param [Account] source_account Where to unfollow from
|
|
|
|
# @param [Account] target_account Which to unfollow
|
2019-09-17 06:44:45 +00:00
|
|
|
# @param [Hash] options
|
|
|
|
# @option [Boolean] :skip_unmerge
|
|
|
|
def call(source_account, target_account, options = {})
|
2017-09-02 18:44:41 +00:00
|
|
|
@source_account = source_account
|
|
|
|
@target_account = target_account
|
2019-09-17 06:44:45 +00:00
|
|
|
@options = options
|
2017-09-02 18:44:41 +00:00
|
|
|
|
2023-05-02 16:16:07 +00:00
|
|
|
with_redis_lock("relationship:#{[source_account.id, target_account.id].sort.join(':')}") do
|
2022-05-26 20:14:47 +00:00
|
|
|
unfollow! || undo_follow_request!
|
|
|
|
end
|
2017-09-02 18:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def unfollow!
|
|
|
|
follow = Follow.find_by(account: @source_account, target_account: @target_account)
|
|
|
|
|
2017-04-16 12:55:43 +00:00
|
|
|
return unless follow
|
2017-09-02 18:44:41 +00:00
|
|
|
|
|
|
|
follow.destroy!
|
2019-09-17 06:44:45 +00:00
|
|
|
|
2019-07-06 21:26:16 +00:00
|
|
|
create_notification(follow) if !@target_account.local? && @target_account.activitypub?
|
|
|
|
create_reject_notification(follow) if @target_account.local? && !@source_account.local? && @source_account.activitypub?
|
2024-11-19 10:04:12 +00:00
|
|
|
|
|
|
|
unless @options[:skip_unmerge]
|
|
|
|
UnmergeWorker.perform_async(@target_account.id, @source_account.id, 'home')
|
|
|
|
UnmergeWorker.push_bulk(List.where(account: @source_account).joins(:list_accounts).where(list_accounts: { account_id: @target_account.id }).pluck(:list_id)) do |list_id|
|
|
|
|
[@target_account.id, list_id, 'list']
|
|
|
|
end
|
|
|
|
end
|
2019-09-17 06:44:45 +00:00
|
|
|
|
2017-08-12 22:44:41 +00:00
|
|
|
follow
|
2016-02-24 11:57:29 +00:00
|
|
|
end
|
2017-02-11 23:48:53 +00:00
|
|
|
|
2017-09-02 18:44:41 +00:00
|
|
|
def undo_follow_request!
|
|
|
|
follow_request = FollowRequest.find_by(account: @source_account, target_account: @target_account)
|
|
|
|
|
|
|
|
return unless follow_request
|
|
|
|
|
|
|
|
follow_request.destroy!
|
2019-09-17 06:44:45 +00:00
|
|
|
|
2017-09-02 18:44:41 +00:00
|
|
|
create_notification(follow_request) unless @target_account.local?
|
2019-09-17 06:44:45 +00:00
|
|
|
|
2017-09-02 18:44:41 +00:00
|
|
|
follow_request
|
|
|
|
end
|
2017-02-11 23:48:53 +00:00
|
|
|
|
2017-08-12 22:44:41 +00:00
|
|
|
def create_notification(follow)
|
2019-07-06 21:26:16 +00:00
|
|
|
ActivityPub::DeliveryWorker.perform_async(build_json(follow), follow.account_id, follow.target_account.inbox_url)
|
2017-08-12 22:44:41 +00:00
|
|
|
end
|
|
|
|
|
2019-01-18 14:57:19 +00:00
|
|
|
def create_reject_notification(follow)
|
|
|
|
ActivityPub::DeliveryWorker.perform_async(build_reject_json(follow), follow.target_account_id, follow.account.inbox_url)
|
|
|
|
end
|
|
|
|
|
2017-08-12 22:44:41 +00:00
|
|
|
def build_json(follow)
|
2019-06-04 21:11:18 +00:00
|
|
|
Oj.dump(serialize_payload(follow, ActivityPub::UndoFollowSerializer))
|
2017-08-12 22:44:41 +00:00
|
|
|
end
|
|
|
|
|
2019-01-18 14:57:19 +00:00
|
|
|
def build_reject_json(follow)
|
2019-06-04 21:11:18 +00:00
|
|
|
Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
|
2019-01-18 14:57:19 +00:00
|
|
|
end
|
2016-02-24 11:57:29 +00:00
|
|
|
end
|