From d1b3f02926860470fd22d7cbd2bd620feadd224e Mon Sep 17 00:00:00 2001 From: Vyr Cossont Date: Sat, 18 Mar 2023 23:47:54 -0700 Subject: [PATCH] Webhooks for local status.create, status.update, account.update (#24133) --- .rubocop_todo.yml | 2 +- app/models/account.rb | 7 +++++++ app/models/status.rb | 11 +++++++++++ app/models/webhook.rb | 3 +++ app/workers/webhooks/delivery_worker.rb | 2 +- 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a3536e2023..b2f8bf52cb 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -245,7 +245,7 @@ Metrics/BlockNesting: # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 368 + Max: 375 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/CyclomaticComplexity: diff --git a/app/models/account.rb b/app/models/account.rb index 1ff083e54a..c4df488789 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -122,6 +122,8 @@ class Account < ApplicationRecord scope :not_excluded_by_account, ->(account) { where.not(id: account.excluded_from_timeline_account_ids) } scope :not_domain_blocked_by_account, ->(account) { where(arel_table[:domain].eq(nil).or(arel_table[:domain].not_in(account.excluded_from_timeline_domains))) } + after_update_commit :trigger_update_webhooks + delegate :email, :unconfirmed_email, :current_sign_in_at, @@ -593,4 +595,9 @@ class Account < ApplicationRecord CanonicalEmailBlock.where(reference_account: self).delete_all end + + # NOTE: the `account.created` webhook is triggered by the `User` model, not `Account`. + def trigger_update_webhooks + TriggerWebhookWorker.perform_async('account.updated', 'Account', id) if local? + end end diff --git a/app/models/status.rb b/app/models/status.rb index e7ea191a80..2e32c3f16b 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -111,6 +111,9 @@ class Status < ApplicationRecord where('NOT EXISTS (SELECT * FROM statuses_tags forbidden WHERE forbidden.status_id = statuses.id AND forbidden.tag_id IN (?))', tag_ids) } + after_create_commit :trigger_create_webhooks + after_update_commit :trigger_update_webhooks + cache_associated :application, :media_attachments, :conversation, @@ -535,4 +538,12 @@ class Status < ApplicationRecord reblog&.decrement_count!(:reblogs_count) if reblog? thread&.decrement_count!(:replies_count) if in_reply_to_id.present? && distributable? end + + def trigger_create_webhooks + TriggerWebhookWorker.perform_async('status.created', 'Status', id) if local? + end + + def trigger_update_webhooks + TriggerWebhookWorker.perform_async('status.updated', 'Status', id) if local? + end end diff --git a/app/models/webhook.rb b/app/models/webhook.rb index 4aafb1257b..9a056a3862 100644 --- a/app/models/webhook.rb +++ b/app/models/webhook.rb @@ -17,7 +17,10 @@ class Webhook < ApplicationRecord EVENTS = %w( account.approved account.created + account.updated report.created + status.created + status.updated ).freeze scope :enabled, -> { where(enabled: true) } diff --git a/app/workers/webhooks/delivery_worker.rb b/app/workers/webhooks/delivery_worker.rb index b1e345c5ef..f8ed669fb5 100644 --- a/app/workers/webhooks/delivery_worker.rb +++ b/app/workers/webhooks/delivery_worker.rb @@ -19,7 +19,7 @@ class Webhooks::DeliveryWorker private def perform_request - request = Request.new(:post, @webhook.url, body: @body) + request = Request.new(:post, @webhook.url, body: @body, allow_local: true) request.add_headers( 'Content-Type' => 'application/json',