Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
refactor(web): simplify anon view notification with joined query and dedup key
Richie McIlroy9f35e6198d8d
diff --git a/apps/web/lib/Notification.ts b/apps/web/lib/Notification.tsindex 2d7e00379..eca95d27e 100644--- a/apps/web/lib/Notification.ts+++ b/apps/web/lib/Notification.ts@@ -221,57 +221,36 @@ export async function createAnonymousViewNotification({anonName: string;location: string | null;}) {- try {- const sessionHash = getSessionHash(sessionId);-- const [videoExists] = await db()- .select({ id: videos.id, ownerId: videos.ownerId })- .from(videos)- .where(eq(videos.id, Video.VideoId.make(videoId)))- .limit(1);-- if (!videoExists) return;-- const [ownerResult] = await db()- .select({- id: users.id,- activeOrganizationId: users.activeOrganizationId,- preferences: users.preferences,- })- .from(users)- .where(eq(users.id, videoExists.ownerId))- .limit(1);-- if (!ownerResult?.activeOrganizationId) return;-- const preferences = ownerResult.preferences as UserPreferences;- if (preferences?.notifications?.pauseViews) return;-- const [existing] = await db()- .select({ id: notifications.id })- .from(notifications)- .where(- and(- eq(notifications.type, "anon_view"),- eq(notifications.recipientId, ownerResult.id),- sql`JSON_EXTRACT(${notifications.data}, '$.videoId') = ${videoId}`,- sql`JSON_EXTRACT(${notifications.data}, '$.sessionHash') = ${sessionHash}`,- ),- )- .limit(1);-- if (existing) return;-- await db().insert(notifications).values({+ const sessionHash = getSessionHash(sessionId);++ const [videoWithOwner] = await db()+ .select({+ videoId: videos.id,+ ownerId: users.id,+ activeOrganizationId: users.activeOrganizationId,+ preferences: users.preferences,+ })+ .from(videos)+ .innerJoin(users, eq(users.id, videos.ownerId))+ .where(eq(videos.id, Video.VideoId.make(videoId)))+ .limit(1);++ if (!videoWithOwner?.activeOrganizationId) return;++ const preferences = videoWithOwner.preferences as UserPreferences;+ if (preferences?.notifications?.pauseViews) return;++ const dedupKey = `anon_view:${videoId}:${sessionHash}`;++ await db()+ .insert(notifications)+ .values({id: nanoId(),- orgId: ownerResult.activeOrganizationId,- recipientId: ownerResult.id,+ orgId: videoWithOwner.activeOrganizationId,+ recipientId: videoWithOwner.ownerId,type: "anon_view",data: { videoId, sessionHash, anonName, location },- });-- revalidatePath("/dashboard");- } catch (error) {- console.error("Error creating anonymous view notification:", error);- }+ dedupKey,+ })+ .onDuplicateKeyUpdate({ set: { id: sql`id` } });}