Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webhooks for local status.create, status.update, account.update #24133

Merged
merged 4 commits into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 13 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,14 @@ 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?
TriggerWebhookWorker.perform_async('status.created.distributable', 'Status', id) if local? && distributable?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This concept gives me a bit of pause... It'd be easier for me to accept the PR without it. If we decide to change this naming scheme or how this works later it'll be a bother so I would prefer not to hastily add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can split this out and resubmit that half when we have a better name for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gargron Done.

end

def trigger_update_webhooks
TriggerWebhookWorker.perform_async('status.updated', 'Status', id) if local?
TriggerWebhookWorker.perform_async('status.updated.distributable', 'Status', id) if local? && distributable?
end
end
5 changes: 5 additions & 0 deletions app/models/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ class Webhook < ApplicationRecord
EVENTS = %w(
account.approved
account.created
account.updated
report.created
status.created
status.created.distributable
status.updated
status.updated.distributable
).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