gearheads
/
mastodon
Archived
2
0
Fork 0

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

gh/dev
Vyr Cossont 2023-03-18 23:47:54 -07:00 committed by GitHub
parent 34096bc6ea
commit 94cbd808b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 2 deletions

View File

@ -245,7 +245,7 @@ Metrics/BlockNesting:
# Configuration parameters: CountComments, CountAsOne. # Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength: Metrics/ClassLength:
Max: 368 Max: 375
# Configuration parameters: AllowedMethods, AllowedPatterns. # Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity: Metrics/CyclomaticComplexity:

View File

@ -122,6 +122,8 @@ class Account < ApplicationRecord
scope :not_excluded_by_account, ->(account) { where.not(id: account.excluded_from_timeline_account_ids) } 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))) } 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, delegate :email,
:unconfirmed_email, :unconfirmed_email,
:current_sign_in_at, :current_sign_in_at,
@ -593,4 +595,9 @@ class Account < ApplicationRecord
CanonicalEmailBlock.where(reference_account: self).delete_all CanonicalEmailBlock.where(reference_account: self).delete_all
end 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 end

View File

@ -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) 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, cache_associated :application,
:media_attachments, :media_attachments,
:conversation, :conversation,
@ -535,4 +538,12 @@ class Status < ApplicationRecord
reblog&.decrement_count!(:reblogs_count) if reblog? reblog&.decrement_count!(:reblogs_count) if reblog?
thread&.decrement_count!(:replies_count) if in_reply_to_id.present? && distributable? thread&.decrement_count!(:replies_count) if in_reply_to_id.present? && distributable?
end 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 end

View File

@ -17,7 +17,10 @@ class Webhook < ApplicationRecord
EVENTS = %w( EVENTS = %w(
account.approved account.approved
account.created account.created
account.updated
report.created report.created
status.created
status.updated
).freeze ).freeze
scope :enabled, -> { where(enabled: true) } scope :enabled, -> { where(enabled: true) }

View File

@ -19,7 +19,7 @@ class Webhooks::DeliveryWorker
private private
def perform_request 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( request.add_headers(
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',