fix(api): webhooks should only listen on correct events

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-10-03 09:22:00 +02:00
parent e5605745de
commit 761b64d248
2 changed files with 36 additions and 21 deletions

View File

@@ -142,22 +142,24 @@ export async function wsProjectEvents(
getRedisSub().subscribe(subscribeToEvent); getRedisSub().subscribe(subscribeToEvent);
const message = async (channel: string, message: string) => { const message = async (channel: string, message: string) => {
const event = getSuperJson<IServiceEvent>(message); if (channel === subscribeToEvent) {
if (event?.projectId === params.projectId) { const event = getSuperJson<IServiceEvent>(message);
const profile = await getProfileByIdCached( if (event?.projectId === params.projectId) {
event.profileId, const profile = await getProfileByIdCached(
event.projectId, event.profileId,
); event.projectId,
connection.socket.send( );
superjson.stringify( connection.socket.send(
access superjson.stringify(
? { access
...event, ? {
profile, ...event,
} profile,
: transformMinimalEvent(event), }
), : transformMinimalEvent(event),
); ),
);
}
} }
}; };
@@ -207,9 +209,11 @@ export async function wsProjectNotifications(
getRedisSub().subscribe(subscribeToEvent); getRedisSub().subscribe(subscribeToEvent);
const message = async (channel: string, message: string) => { const message = async (channel: string, message: string) => {
const notification = getSuperJson<Notification>(message); if (channel === subscribeToEvent) {
if (notification?.projectId === params.projectId) { const notification = getSuperJson<Notification>(message);
connection.socket.send(superjson.stringify(notification)); if (notification?.projectId === params.projectId) {
connection.socket.send(superjson.stringify(notification));
}
} }
}; };
@@ -234,7 +238,12 @@ export async function wsIntegrationsSlack(
const subscribeToEvent = 'integrations:slack'; const subscribeToEvent = 'integrations:slack';
getRedisSub().subscribe(subscribeToEvent); getRedisSub().subscribe(subscribeToEvent);
const onMessage = (channel: string, message: string) => { const onMessage = (channel: string, message: string) => {
connection.socket.send(JSON.stringify('ok')); if (channel === subscribeToEvent) {
const parsed = getSuperJson<{ organizationId: string }>(message);
if (parsed && parsed.organizationId === req.query.organizationId) {
connection.socket.send(message);
}
}
}; };
getRedisSub().on('message', onMessage); getRedisSub().on('message', onMessage);
connection.socket.on('close', () => { connection.socket.on('close', () => {

View File

@@ -1,4 +1,5 @@
import type { WebhookEvent } from '@clerk/fastify'; import type { WebhookEvent } from '@clerk/fastify';
import { setSuperJson } from '@openpanel/common';
import { AccessLevel, db } from '@openpanel/db'; import { AccessLevel, db } from '@openpanel/db';
import { import {
sendSlackNotification, sendSlackNotification,
@@ -242,7 +243,12 @@ export async function slackWebhook(
}, },
}); });
getRedisPub().publish('integrations:slack', 'ok'); getRedisPub().publish(
'integrations:slack',
setSuperJson({
organizationId: parsedMetadata.data.organizationId,
}),
);
reply.send({ success: true }); reply.send({ success: true });
} catch (err) { } catch (err) {