From c40cc9e2f8267ebe07e5f3b209eef12e9ed84941 Mon Sep 17 00:00:00 2001 From: CSDUMMI Date: Fri, 26 Jan 2024 13:30:19 +0100 Subject: [PATCH] Add follow/mute/block.removed events for Webhooks and associate seemingly fitting permissions with them. --- app/models/webhook.rb | 7 ++++++- app/services/unblock_service.rb | 3 +++ app/services/unfollow_service.rb | 2 ++ app/services/unmute_service.rb | 4 +++- app/workers/trigger_webhook_with_object_worker.rb | 13 +++++++++++++ 5 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 app/workers/trigger_webhook_with_object_worker.rb diff --git a/app/models/webhook.rb b/app/models/webhook.rb index 304b2b1f18..9d90efdd7d 100644 --- a/app/models/webhook.rb +++ b/app/models/webhook.rb @@ -23,6 +23,9 @@ class Webhook < ApplicationRecord report.updated status.created status.updated + follow.removed + block.removed + mute.removed ).freeze attr_writer :current_account @@ -58,10 +61,12 @@ class Webhook < ApplicationRecord def self.permission_for_event(event) case event - when 'account.approved', 'account.created', 'account.updated' + when 'account.approved', 'account.created', 'account.updated', 'follow.removed' :manage_users when 'report.created', 'report.updated' :manage_reports + when 'block.removed', 'mute.removed' + :manage_blocks when 'status.created', 'status.updated' :view_devops end diff --git a/app/services/unblock_service.rb b/app/services/unblock_service.rb index c263ac8afe..1dcab21824 100644 --- a/app/services/unblock_service.rb +++ b/app/services/unblock_service.rb @@ -7,6 +7,9 @@ class UnblockService < BaseService return unless account.blocking?(target_account) unblock = account.unblock!(target_account) + + TriggerWebhookWithObjectWorker.perform_async('block.removed', Oj.to_json({ 'account_id': unblock.account_id, 'target_account_id': unblock.target_account_id, 'id': unblock.id })) + create_notification(unblock) if !target_account.local? && target_account.activitypub? unblock end diff --git a/app/services/unfollow_service.rb b/app/services/unfollow_service.rb index fe9a7f0d87..1d8ec992cb 100644 --- a/app/services/unfollow_service.rb +++ b/app/services/unfollow_service.rb @@ -29,6 +29,8 @@ class UnfollowService < BaseService follow.destroy! + TriggerWebhookWithObjectWorker.perform_async('follow.removed', Oj.to_json({ 'account_id': follow.account_id, 'target_account_id': follow.target_account_id, 'id': follow.id })) + create_notification(follow) if !@target_account.local? && @target_account.activitypub? create_reject_notification(follow) if @target_account.local? && !@source_account.local? && @source_account.activitypub? UnmergeWorker.perform_async(@target_account.id, @source_account.id) unless @options[:skip_unmerge] diff --git a/app/services/unmute_service.rb b/app/services/unmute_service.rb index 6aeea358f7..991da8e08b 100644 --- a/app/services/unmute_service.rb +++ b/app/services/unmute_service.rb @@ -4,7 +4,9 @@ class UnmuteService < BaseService def call(account, target_account) return unless account.muting?(target_account) - account.unmute!(target_account) + unmute = account.unmute!(target_account) + + TriggerWebhookWithObjectWorker.perform_async('mute.removed', Oj.to_json({ 'account_id': unmute.account_id, 'target_account_id': unmute.target_account_id, 'id': unmute.id })) MergeWorker.perform_async(target_account.id, account.id) if account.following?(target_account) end diff --git a/app/workers/trigger_webhook_with_object_worker.rb b/app/workers/trigger_webhook_with_object_worker.rb new file mode 100644 index 0000000000..1a02a9783e --- /dev/null +++ b/app/workers/trigger_webhook_with_object_worker.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class TriggerWebhookWithObjectWorker + include Sidekiq::Worker + + # Triggers a webhook for a destructive event (e.g. an unfollow), + # where the object cannot be queried from the database. + # @param event [String] type of the event + # @param object [String] event payload serialized with JSON + def perform(event, object) + WebhookService.new.call(event, Oj.load(object)) + end +end