improve webhook
This commit is contained in:
		
							parent
							
								
									7f5d189528
								
							
						
					
					
						commit
						f8e6f3cc73
					
				
					 9 changed files with 36 additions and 28 deletions
				
			
		|  | @ -10,6 +10,15 @@ | ||||||
| You should also include the user name that made the change. | 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) | ## 12.109.2 (2022/04/03) | ||||||
| 
 | 
 | ||||||
| ### Bugfixes | ### Bugfixes | ||||||
|  |  | ||||||
|  | @ -1,4 +1,5 @@ | ||||||
| import httpSignature from 'http-signature'; | import httpSignature from 'http-signature'; | ||||||
|  | import { v4 as uuid } from 'uuid'; | ||||||
| 
 | 
 | ||||||
| import config from '@/config/index.js'; | import config from '@/config/index.js'; | ||||||
| import { envOption } from '../env.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 { systemQueue, dbQueue, deliverQueue, inboxQueue, objectStorageQueue, endedPollNotificationQueue, webhookDeliverQueue } from './queues.js'; | ||||||
| import { ThinUser } from './types.js'; | import { ThinUser } from './types.js'; | ||||||
| import { IActivity } from '@/remote/activitypub/type.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 { | function renderError(e: Error): any { | ||||||
| 	return { | 	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 = { | 	const data = { | ||||||
|  | 		type, | ||||||
| 		content, | 		content, | ||||||
| 		webhookId: webhook.id, | 		webhookId: webhook.id, | ||||||
| 		to: webhook.url, | 		to: webhook.url, | ||||||
| 		secret: webhook.secret, | 		secret: webhook.secret, | ||||||
|  | 		createdAt: Date.now(), | ||||||
|  | 		eventId: uuid(), | ||||||
| 	}; | 	}; | ||||||
| 
 | 
 | ||||||
| 	return webhookDeliverQueue.add(data, { | 	return webhookDeliverQueue.add(data, { | ||||||
|  |  | ||||||
|  | @ -8,13 +8,9 @@ import config from '@/config/index.js'; | ||||||
| 
 | 
 | ||||||
| const logger = new Logger('webhook'); | const logger = new Logger('webhook'); | ||||||
| 
 | 
 | ||||||
| let latest: string | null = null; |  | ||||||
| 
 |  | ||||||
| export default async (job: Bull.Job<WebhookDeliverJobData>) => { | export default async (job: Bull.Job<WebhookDeliverJobData>) => { | ||||||
| 	try { | 	try { | ||||||
| 		if (latest !== (latest = JSON.stringify(job.data.content, null, 2))) { | 		logger.debug(`delivering ${job.data.webhookId}`); | ||||||
| 			logger.debug(`delivering ${latest}`); |  | ||||||
| 		} |  | ||||||
| 
 | 
 | ||||||
| 		const res = await getResponse({ | 		const res = await getResponse({ | ||||||
| 			url: job.data.to, | 			url: job.data.to, | ||||||
|  | @ -25,7 +21,13 @@ export default async (job: Bull.Job<WebhookDeliverJobData>) => { | ||||||
| 				'X-Misskey-Hook-Id': job.data.webhookId, | 				'X-Misskey-Hook-Id': job.data.webhookId, | ||||||
| 				'X-Misskey-Hook-Secret': job.data.secret, | 				'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 }, { | 		Webhooks.update({ id: job.data.webhookId }, { | ||||||
|  |  | ||||||
|  | @ -48,10 +48,13 @@ export type EndedPollNotificationJobData = { | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| export type WebhookDeliverJobData = { | export type WebhookDeliverJobData = { | ||||||
|  | 	type: string; | ||||||
| 	content: unknown; | 	content: unknown; | ||||||
| 	webhookId: Webhook['id']; | 	webhookId: Webhook['id']; | ||||||
| 	to: string; | 	to: string; | ||||||
| 	secret: string; | 	secret: string; | ||||||
|  | 	createdAt: number; | ||||||
|  | 	eventId: string; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| export type ThinUser = { | export type ThinUser = { | ||||||
|  |  | ||||||
|  | @ -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')); | 			const webhooks = (await getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('unfollow')); | ||||||
| 			for (const webhook of webhooks) { | 			for (const webhook of webhooks) { | ||||||
| 				webhookDeliver(webhook, { | 				webhookDeliver(webhook, 'unfollow', { | ||||||
| 					type: 'unfollow', |  | ||||||
| 					user: packed, | 					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')); | 			const webhooks = (await getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('unfollow')); | ||||||
| 			for (const webhook of webhooks) { | 			for (const webhook of webhooks) { | ||||||
| 				webhookDeliver(webhook, { | 				webhookDeliver(webhook, 'unfollow', { | ||||||
| 					type: 'unfollow', |  | ||||||
| 					user: packed, | 					user: packed, | ||||||
| 				}); | 				}); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | @ -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')); | 			const webhooks = (await getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('follow')); | ||||||
| 			for (const webhook of webhooks) { | 			for (const webhook of webhooks) { | ||||||
| 				webhookDeliver(webhook, { | 				webhookDeliver(webhook, 'follow', { | ||||||
| 					type: 'follow', |  | ||||||
| 					user: packed, | 					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')); | 			const webhooks = (await getActiveWebhooks()).filter(x => x.userId === followee.id && x.on.includes('followed')); | ||||||
| 			for (const webhook of webhooks) { | 			for (const webhook of webhooks) { | ||||||
| 				webhookDeliver(webhook, { | 				webhookDeliver(webhook, 'followed', { | ||||||
| 					type: 'followed', |  | ||||||
| 					user: packed, | 					user: packed, | ||||||
| 				}); | 				}); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | @ -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')); | 			const webhooks = (await getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('unfollow')); | ||||||
| 			for (const webhook of webhooks) { | 			for (const webhook of webhooks) { | ||||||
| 				webhookDeliver(webhook, { | 				webhookDeliver(webhook, 'unfollow', { | ||||||
| 					type: 'unfollow', |  | ||||||
| 					user: packed, | 					user: packed, | ||||||
| 				}); | 				}); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | @ -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')); | 	const webhooks = (await getActiveWebhooks()).filter(x => x.userId === follower.id && x.on.includes('unfollow')); | ||||||
| 	for (const webhook of webhooks) { | 	for (const webhook of webhooks) { | ||||||
| 		webhookDeliver(webhook, { | 		webhookDeliver(webhook, 'unfollow', { | ||||||
| 			type: 'unfollow', |  | ||||||
| 			user: packedFollowee, | 			user: packedFollowee, | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -350,8 +350,7 @@ export default async (user: { id: User['id']; username: User['username']; host: | ||||||
| 		getActiveWebhooks().then(webhooks => { | 		getActiveWebhooks().then(webhooks => { | ||||||
| 			webhooks = webhooks.filter(x => x.userId === user.id && x.on.includes('note')); | 			webhooks = webhooks.filter(x => x.userId === user.id && x.on.includes('note')); | ||||||
| 			for (const webhook of webhooks) { | 			for (const webhook of webhooks) { | ||||||
| 				webhookDeliver(webhook, { | 				webhookDeliver(webhook, 'note', { | ||||||
| 					type: 'note', |  | ||||||
| 					note: noteObj, | 					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')); | 					const webhooks = (await getActiveWebhooks()).filter(x => x.userId === data.reply!.userId && x.on.includes('reply')); | ||||||
| 					for (const webhook of webhooks) { | 					for (const webhook of webhooks) { | ||||||
| 						webhookDeliver(webhook, { | 						webhookDeliver(webhook, 'reply', { | ||||||
| 							type: 'reply', |  | ||||||
| 							note: noteObj, | 							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')); | 				const webhooks = (await getActiveWebhooks()).filter(x => x.userId === data.renote!.userId && x.on.includes('renote')); | ||||||
| 				for (const webhook of webhooks) { | 				for (const webhook of webhooks) { | ||||||
| 					webhookDeliver(webhook, { | 					webhookDeliver(webhook, 'renote', { | ||||||
| 						type: 'renote', |  | ||||||
| 						note: noteObj, | 						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')); | 		const webhooks = (await getActiveWebhooks()).filter(x => x.userId === u.id && x.on.includes('mention')); | ||||||
| 		for (const webhook of webhooks) { | 		for (const webhook of webhooks) { | ||||||
| 			webhookDeliver(webhook, { | 			webhookDeliver(webhook, 'mention', { | ||||||
| 				type: 'mention', |  | ||||||
| 				note: detailPackedNote, | 				note: detailPackedNote, | ||||||
| 			}); | 			}); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue