From f8e6f3cc73b041acd7c8e8186090a297db3100a9 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 3 Apr 2022 22:36:30 +0900 Subject: [PATCH] improve webhook --- CHANGELOG.md | 9 +++++++++ packages/backend/src/queue/index.ts | 8 ++++++-- .../src/queue/processors/webhook-deliver.ts | 14 ++++++++------ packages/backend/src/queue/types.ts | 3 +++ packages/backend/src/services/blocking/create.ts | 6 ++---- packages/backend/src/services/following/create.ts | 6 ++---- packages/backend/src/services/following/delete.ts | 3 +-- packages/backend/src/services/following/reject.ts | 3 +-- packages/backend/src/services/note/create.ts | 12 ++++-------- 9 files changed, 36 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c874648ef..5b233f0a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,15 @@ You should also include the user name that made the change. --> +## 12.x.x (unreleased) + +### Improvements +- Improve webhook @syuilo + +### Bugfixes +- + + ## 12.109.2 (2022/04/03) ### Bugfixes diff --git a/packages/backend/src/queue/index.ts b/packages/backend/src/queue/index.ts index a570400b7..5bd922468 100644 --- a/packages/backend/src/queue/index.ts +++ b/packages/backend/src/queue/index.ts @@ -1,4 +1,5 @@ import httpSignature from 'http-signature'; +import { v4 as uuid } from 'uuid'; import config from '@/config/index.js'; import { envOption } from '../env.js'; @@ -16,7 +17,7 @@ import { getJobInfo } from './get-job-info.js'; import { systemQueue, dbQueue, deliverQueue, inboxQueue, objectStorageQueue, endedPollNotificationQueue, webhookDeliverQueue } from './queues.js'; import { ThinUser } from './types.js'; import { IActivity } from '@/remote/activitypub/type.js'; -import { Webhook } from '@/models/entities/webhook.js'; +import { Webhook, webhookEventTypes } from '@/models/entities/webhook.js'; function renderError(e: Error): any { return { @@ -262,12 +263,15 @@ export function createCleanRemoteFilesJob() { }); } -export function webhookDeliver(webhook: Webhook, content: unknown) { +export function webhookDeliver(webhook: Webhook, type: typeof webhookEventTypes[number], content: unknown) { const data = { + type, content, webhookId: webhook.id, to: webhook.url, secret: webhook.secret, + createdAt: Date.now(), + eventId: uuid(), }; return webhookDeliverQueue.add(data, { diff --git a/packages/backend/src/queue/processors/webhook-deliver.ts b/packages/backend/src/queue/processors/webhook-deliver.ts index a4d39d86e..01d779311 100644 --- a/packages/backend/src/queue/processors/webhook-deliver.ts +++ b/packages/backend/src/queue/processors/webhook-deliver.ts @@ -8,13 +8,9 @@ import config from '@/config/index.js'; const logger = new Logger('webhook'); -let latest: string | null = null; - export default async (job: Bull.Job) => { try { - if (latest !== (latest = JSON.stringify(job.data.content, null, 2))) { - logger.debug(`delivering ${latest}`); - } + logger.debug(`delivering ${job.data.webhookId}`); const res = await getResponse({ url: job.data.to, @@ -25,7 +21,13 @@ export default async (job: Bull.Job) => { 'X-Misskey-Hook-Id': job.data.webhookId, 'X-Misskey-Hook-Secret': job.data.secret, }, - body: JSON.stringify(job.data.content), + body: JSON.stringify({ + hookId: job.data.webhookId, + eventId: job.data.eventId, + createdAt: job.data.createdAt, + type: job.data.type, + body: job.data.content, + }), }); Webhooks.update({ id: job.data.webhookId }, { diff --git a/packages/backend/src/queue/types.ts b/packages/backend/src/queue/types.ts index 8aeacf462..c005f78ec 100644 --- a/packages/backend/src/queue/types.ts +++ b/packages/backend/src/queue/types.ts @@ -48,10 +48,13 @@ export type EndedPollNotificationJobData = { }; export type WebhookDeliverJobData = { + type: string; content: unknown; webhookId: Webhook['id']; to: string; secret: string; + createdAt: number; + eventId: string; }; export type ThinUser = { diff --git a/packages/backend/src/services/blocking/create.ts b/packages/backend/src/services/blocking/create.ts index 5c6719007..5e96e5037 100644 --- a/packages/backend/src/services/blocking/create.ts +++ b/packages/backend/src/services/blocking/create.ts @@ -65,8 +65,7 @@ async function cancelRequest(follower: User, followee: User) { const webhooks = (await getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('unfollow')); for (const webhook of webhooks) { - webhookDeliver(webhook, { - type: 'unfollow', + webhookDeliver(webhook, 'unfollow', { user: packed, }); } @@ -118,8 +117,7 @@ async function unFollow(follower: User, followee: User) { const webhooks = (await getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('unfollow')); for (const webhook of webhooks) { - webhookDeliver(webhook, { - type: 'unfollow', + webhookDeliver(webhook, 'unfollow', { user: packed, }); } diff --git a/packages/backend/src/services/following/create.ts b/packages/backend/src/services/following/create.ts index 7bc839c62..7491c44f8 100644 --- a/packages/backend/src/services/following/create.ts +++ b/packages/backend/src/services/following/create.ts @@ -97,8 +97,7 @@ export async function insertFollowingDoc(followee: { id: User['id']; host: User[ const webhooks = (await getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('follow')); for (const webhook of webhooks) { - webhookDeliver(webhook, { - type: 'follow', + webhookDeliver(webhook, 'follow', { user: packed, }); } @@ -112,8 +111,7 @@ export async function insertFollowingDoc(followee: { id: User['id']; host: User[ const webhooks = (await getActiveWebhooks()).filter(x => x.userId === followee.id && x.on.includes('followed')); for (const webhook of webhooks) { - webhookDeliver(webhook, { - type: 'followed', + webhookDeliver(webhook, 'followed', { user: packed, }); } diff --git a/packages/backend/src/services/following/delete.ts b/packages/backend/src/services/following/delete.ts index 85e40f136..241f9606e 100644 --- a/packages/backend/src/services/following/delete.ts +++ b/packages/backend/src/services/following/delete.ts @@ -38,8 +38,7 @@ export default async function(follower: { id: User['id']; host: User['host']; ur const webhooks = (await getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('unfollow')); for (const webhook of webhooks) { - webhookDeliver(webhook, { - type: 'unfollow', + webhookDeliver(webhook, 'unfollow', { user: packed, }); } diff --git a/packages/backend/src/services/following/reject.ts b/packages/backend/src/services/following/reject.ts index e1744e05b..691fca245 100644 --- a/packages/backend/src/services/following/reject.ts +++ b/packages/backend/src/services/following/reject.ts @@ -115,8 +115,7 @@ async function publishUnfollow(followee: Both, follower: Local) { const webhooks = (await getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('unfollow')); for (const webhook of webhooks) { - webhookDeliver(webhook, { - type: 'unfollow', + webhookDeliver(webhook, 'unfollow', { user: packedFollowee, }); } diff --git a/packages/backend/src/services/note/create.ts b/packages/backend/src/services/note/create.ts index 6f373aaf4..f14bc2059 100644 --- a/packages/backend/src/services/note/create.ts +++ b/packages/backend/src/services/note/create.ts @@ -350,8 +350,7 @@ export default async (user: { id: User['id']; username: User['username']; host: getActiveWebhooks().then(webhooks => { webhooks = webhooks.filter(x => x.userId === user.id && x.on.includes('note')); for (const webhook of webhooks) { - webhookDeliver(webhook, { - type: 'note', + webhookDeliver(webhook, 'note', { note: noteObj, }); } @@ -380,8 +379,7 @@ export default async (user: { id: User['id']; username: User['username']; host: const webhooks = (await getActiveWebhooks()).filter(x => x.userId === data.reply!.userId && x.on.includes('reply')); for (const webhook of webhooks) { - webhookDeliver(webhook, { - type: 'reply', + webhookDeliver(webhook, 'reply', { note: noteObj, }); } @@ -407,8 +405,7 @@ export default async (user: { id: User['id']; username: User['username']; host: const webhooks = (await getActiveWebhooks()).filter(x => x.userId === data.renote!.userId && x.on.includes('renote')); for (const webhook of webhooks) { - webhookDeliver(webhook, { - type: 'renote', + webhookDeliver(webhook, 'renote', { note: noteObj, }); } @@ -650,8 +647,7 @@ async function createMentionedEvents(mentionedUsers: MinimumUser[], note: Note, const webhooks = (await getActiveWebhooks()).filter(x => x.userId === u.id && x.on.includes('mention')); for (const webhook of webhooks) { - webhookDeliver(webhook, { - type: 'mention', + webhookDeliver(webhook, 'mention', { note: detailPackedNote, }); }