2016-11-16 02:56:29 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-24 22:57:29 +11:00
|
|
|
class UnfollowService < BaseService
|
2019-06-05 07:11:18 +10:00
|
|
|
include Payloadable
|
|
|
|
|
2016-02-24 22:57:29 +11:00
|
|
|
# Unfollow and notify the remote user
|
|
|
|
# @param [Account] source_account Where to unfollow from
|
|
|
|
# @param [Account] target_account Which to unfollow
|
|
|
|
def call(source_account, target_account)
|
2017-09-03 04:44:41 +10:00
|
|
|
@source_account = source_account
|
|
|
|
@target_account = target_account
|
|
|
|
|
|
|
|
unfollow! || undo_follow_request!
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def unfollow!
|
|
|
|
follow = Follow.find_by(account: @source_account, target_account: @target_account)
|
|
|
|
|
2017-04-16 22:55:43 +10:00
|
|
|
return unless follow
|
2017-09-03 04:44:41 +10:00
|
|
|
|
|
|
|
follow.destroy!
|
2019-07-07 07:26:16 +10: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?
|
2017-09-03 04:44:41 +10:00
|
|
|
UnmergeWorker.perform_async(@target_account.id, @source_account.id)
|
2017-08-13 08:44:41 +10:00
|
|
|
follow
|
2016-02-24 22:57:29 +11:00
|
|
|
end
|
2017-02-12 10:48:53 +11:00
|
|
|
|
2017-09-03 04:44:41 +10:00
|
|
|
def undo_follow_request!
|
|
|
|
follow_request = FollowRequest.find_by(account: @source_account, target_account: @target_account)
|
|
|
|
|
|
|
|
return unless follow_request
|
|
|
|
|
|
|
|
follow_request.destroy!
|
|
|
|
create_notification(follow_request) unless @target_account.local?
|
|
|
|
follow_request
|
|
|
|
end
|
2017-02-12 10:48:53 +11:00
|
|
|
|
2017-08-13 08:44:41 +10:00
|
|
|
def create_notification(follow)
|
2019-07-07 07:26:16 +10:00
|
|
|
ActivityPub::DeliveryWorker.perform_async(build_json(follow), follow.account_id, follow.target_account.inbox_url)
|
2017-08-13 08:44:41 +10:00
|
|
|
end
|
|
|
|
|
2019-01-19 01:57:19 +11: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-13 08:44:41 +10:00
|
|
|
def build_json(follow)
|
2019-06-05 07:11:18 +10:00
|
|
|
Oj.dump(serialize_payload(follow, ActivityPub::UndoFollowSerializer))
|
2017-08-13 08:44:41 +10:00
|
|
|
end
|
|
|
|
|
2019-01-19 01:57:19 +11:00
|
|
|
def build_reject_json(follow)
|
2019-06-05 07:11:18 +10:00
|
|
|
Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
|
2019-01-19 01:57:19 +11:00
|
|
|
end
|
2016-02-24 22:57:29 +11:00
|
|
|
end
|