Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(web): use safe LIKE query and handle race conditions in anon notifications
Richie McIlroya75b9ecce8b8
diff --git a/apps/web/lib/Notification.ts b/apps/web/lib/Notification.tsindex 002379e64..3ba4b7970 100644--- a/apps/web/lib/Notification.ts+++ b/apps/web/lib/Notification.ts@@ -247,62 +247,69 @@ export async function createAnonymousViewNotification({const sessionHash = getSessionHash(sessionId);const dedupKey = `anon_view:${videoId}:${sessionHash}`;const rateWindowStart = new Date(Date.now() - ANON_NOTIF_WINDOW_MS);- const dedupPrefix = `anon_view:${videoId}:%`;+ const dedupPrefix = `anon_view:${escapeLikePattern(videoId)}:%`;let created = false;- await db().transaction(async (tx) => {- const [videoWithOwner] = await tx- .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)- .for("update");+ try {+ await db().transaction(async (tx) => {+ const [videoWithOwner] = await tx+ .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)+ .for("update");- if (!videoWithOwner?.activeOrganizationId) return;+ if (!videoWithOwner?.activeOrganizationId) return;- const preferences = videoWithOwner.preferences as UserPreferences;- if (preferences?.notifications?.pauseViews) return;+ const preferences = videoWithOwner.preferences as UserPreferences;+ if (preferences?.notifications?.pauseViews) return;- const [existingNotification] = await tx- .select({ id: notifications.id })- .from(notifications)- .where(eq(notifications.dedupKey, dedupKey))- .limit(1);+ const [existingNotification] = await tx+ .select({ id: notifications.id })+ .from(notifications)+ .where(eq(notifications.dedupKey, dedupKey))+ .limit(1);- if (existingNotification) return;-- const [recentNotificationCount] = await tx- .select({ count: sql<number>`COUNT(*)` })- .from(notifications)- .where(- and(- eq(notifications.type, "anon_view"),- eq(notifications.recipientId, videoWithOwner.ownerId),- gte(notifications.createdAt, rateWindowStart),- like(notifications.dedupKey, dedupPrefix),- ),- )- .limit(1);+ if (existingNotification) return;- if (Number(recentNotificationCount?.count ?? 0) >= ANON_NOTIF_MAX_PER_VIDEO)- return;+ const [recentNotificationCount] = await tx+ .select({ count: sql<number>`COUNT(*)` })+ .from(notifications)+ .where(+ and(+ eq(notifications.type, "anon_view"),+ eq(notifications.recipientId, videoWithOwner.ownerId),+ gte(notifications.createdAt, rateWindowStart),+ sql`${notifications.dedupKey} LIKE ${dedupPrefix} ESCAPE '\\\\'`,+ ),+ )+ .limit(1);- await tx.insert(notifications).values({- id: nanoId(),- orgId: videoWithOwner.activeOrganizationId,- recipientId: videoWithOwner.ownerId,- type: "anon_view",- data: { videoId, sessionHash, anonName, location },- dedupKey,+ if (+ Number(recentNotificationCount?.count ?? 0) >= ANON_NOTIF_MAX_PER_VIDEO+ )+ return;++ await tx.insert(notifications).values({+ id: nanoId(),+ orgId: videoWithOwner.activeOrganizationId,+ recipientId: videoWithOwner.ownerId,+ type: "anon_view",+ data: { videoId, sessionHash, anonName, location },+ dedupKey,+ });+ created = true;});- created = true;- });+ } catch (error) {+ if (isDuplicateEntryError(error)) return;+ throw error;+ }if (created) revalidatePath("/dashboard");}