Skip to content

Commit 94cbd80

Browse files
authored
Webhooks for local status.create, status.update, account.update (#24133)
1 parent 34096bc commit 94cbd80

File tree

5 files changed

+23
-2
lines changed

5 files changed

+23
-2
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Metrics/BlockNesting:
245245

246246
# Configuration parameters: CountComments, CountAsOne.
247247
Metrics/ClassLength:
248-
Max: 368
248+
Max: 375
249249

250250
# Configuration parameters: AllowedMethods, AllowedPatterns.
251251
Metrics/CyclomaticComplexity:

app/models/account.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class Account < ApplicationRecord
122122
scope :not_excluded_by_account, ->(account) { where.not(id: account.excluded_from_timeline_account_ids) }
123123
scope :not_domain_blocked_by_account, ->(account) { where(arel_table[:domain].eq(nil).or(arel_table[:domain].not_in(account.excluded_from_timeline_domains))) }
124124

125+
after_update_commit :trigger_update_webhooks
126+
125127
delegate :email,
126128
:unconfirmed_email,
127129
:current_sign_in_at,
@@ -593,4 +595,9 @@ def destroy_canonical_email_block!
593595

594596
CanonicalEmailBlock.where(reference_account: self).delete_all
595597
end
598+
599+
# NOTE: the `account.created` webhook is triggered by the `User` model, not `Account`.
600+
def trigger_update_webhooks
601+
TriggerWebhookWorker.perform_async('account.updated', 'Account', id) if local?
602+
end
596603
end

app/models/status.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ class Status < ApplicationRecord
111111
where('NOT EXISTS (SELECT * FROM statuses_tags forbidden WHERE forbidden.status_id = statuses.id AND forbidden.tag_id IN (?))', tag_ids)
112112
}
113113

114+
after_create_commit :trigger_create_webhooks
115+
after_update_commit :trigger_update_webhooks
116+
114117
cache_associated :application,
115118
:media_attachments,
116119
:conversation,
@@ -535,4 +538,12 @@ def decrement_counter_caches
535538
reblog&.decrement_count!(:reblogs_count) if reblog?
536539
thread&.decrement_count!(:replies_count) if in_reply_to_id.present? && distributable?
537540
end
541+
542+
def trigger_create_webhooks
543+
TriggerWebhookWorker.perform_async('status.created', 'Status', id) if local?
544+
end
545+
546+
def trigger_update_webhooks
547+
TriggerWebhookWorker.perform_async('status.updated', 'Status', id) if local?
548+
end
538549
end

app/models/webhook.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ class Webhook < ApplicationRecord
1717
EVENTS = %w(
1818
account.approved
1919
account.created
20+
account.updated
2021
report.created
22+
status.created
23+
status.updated
2124
).freeze
2225

2326
scope :enabled, -> { where(enabled: true) }

app/workers/webhooks/delivery_worker.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def perform(webhook_id, body)
1919
private
2020

2121
def perform_request
22-
request = Request.new(:post, @webhook.url, body: @body)
22+
request = Request.new(:post, @webhook.url, body: @body, allow_local: true)
2323

2424
request.add_headers(
2525
'Content-Type' => 'application/json',

0 commit comments

Comments
 (0)