Skip to content

Commit

Permalink
Webhooks for local status.create, status.update, account.update (mast…
Browse files Browse the repository at this point in the history
  • Loading branch information
VyrCossont committed Mar 19, 2023
1 parent 34096bc commit 94cbd80
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ Metrics/BlockNesting:

# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 368
Max: 375

# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Expand Down
7 changes: 7 additions & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -593,4 +595,9 @@ def destroy_canonical_email_block!

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
11 changes: 11 additions & 0 deletions app/models/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -535,4 +538,12 @@ def decrement_counter_caches
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
3 changes: 3 additions & 0 deletions app/models/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion app/workers/webhooks/delivery_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def perform(webhook_id, body)
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',
Expand Down

0 comments on commit 94cbd80

Please sign in to comment.