Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

refactor(web): remove transaction and sessionHash from anon view notifications

Richie McIlroyaa2fb7588c26
diff --git a/apps/web/lib/Notification.ts b/apps/web/lib/Notification.ts
index c71af496a..8c6b1a7e6 100644
--- a/apps/web/lib/Notification.ts
+++ b/apps/web/lib/Notification.ts
@@ -249,68 +249,62 @@ export async function createAnonymousViewNotification({
const dedupKey = `anon_view:${videoId}:${sessionHash}`;
const rateWindowStart = new Date(Date.now() - ANON_NOTIF_WINDOW_MS);
const dedupPrefix = `anon_view:${escapeLikePattern(videoId)}:%`;
- let created = false;
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");
+ const database = db();
- if (!videoWithOwner?.activeOrganizationId) 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 database
+ .select({ id: notifications.id })
+ .from(notifications)
+ .where(eq(notifications.dedupKey, dedupKey))
+ .limit(1);
- if (existingNotification) return;
+ 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),
- sql`${notifications.dedupKey} LIKE ${dedupPrefix} ESCAPE '\\\\'`,
- ),
- )
- .limit(1);
+ const [videoWithOwner] = await database
+ .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 (
- Number(recentNotificationCount?.count ?? 0) >= ANON_NOTIF_MAX_PER_VIDEO
+ if (!videoWithOwner?.activeOrganizationId) return;
+
+ const preferences = videoWithOwner.preferences as UserPreferences;
+ if (preferences?.notifications?.pauseViews) return;
+
+ const [recentNotificationCount] = await database
+ .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 '\\\\'`,
+ ),
)
- return;
+ .limit(1);
- await tx.insert(notifications).values({
- id: nanoId(),
- orgId: videoWithOwner.activeOrganizationId,
- recipientId: videoWithOwner.ownerId,
- type: "anon_view",
- data: { videoId, sessionHash, anonName, location },
- dedupKey,
- });
- created = true;
+ if (Number(recentNotificationCount?.count ?? 0) >= ANON_NOTIF_MAX_PER_VIDEO)
+ return;
+
+ await database.insert(notifications).values({
+ id: nanoId(),
+ orgId: videoWithOwner.activeOrganizationId,
+ recipientId: videoWithOwner.ownerId,
+ type: "anon_view",
+ data: { videoId, anonName, location },
+ dedupKey,
});
+ revalidatePath("/dashboard");
} catch (error) {
if (isDuplicateEntryError(error)) return;
throw error;
}
-
- if (created) revalidatePath("/dashboard");
}
diff --git a/packages/database/schema.ts b/packages/database/schema.ts
index 902a2345d..358330bf8 100644
--- a/packages/database/schema.ts
+++ b/packages/database/schema.ts
@@ -441,7 +441,6 @@ export const notifications = mysqlTable(
id: string;
content: string;
};
- sessionHash?: string;
anonName?: string;
location?: string | null;
}>()