Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): delete media objects with media comments

Richie McIlroy5caae5d5918d
diff --git a/apps/web/actions/videos/delete-comment.ts b/apps/web/actions/videos/delete-comment.ts
index a5ffc7221..29b14bb49 100644
--- a/apps/web/actions/videos/delete-comment.ts
+++ b/apps/web/actions/videos/delete-comment.ts
@@ -3,9 +3,12 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { comments, notifications } from "@cap/database/schema";
+import { Storage, VideosRepo } from "@cap/web-backend";
import type { Comment, Video } from "@cap/web-domain";
import { and, eq, sql } from "drizzle-orm";
+import { Effect, Option } from "effect";
import { revalidatePath } from "next/cache";
+import * as EffectRuntime from "@/lib/server";
export async function deleteComment({
commentId,
@@ -27,10 +30,21 @@ export async function deleteComment({
}
try {
+ let deletedMedia: {
+ videoId: Video.VideoId;
+ mediaKey: string;
+ thumbnailKey: string | null;
+ } | null = null;
+
await db().transaction(async (tx) => {
// First, verify the comment exists and belongs to the current user
const [existingComment] = await tx
- .select({ id: comments.id })
+ .select({
+ id: comments.id,
+ videoId: comments.videoId,
+ mediaKey: comments.mediaKey,
+ mediaMeta: comments.mediaMeta,
+ })
.from(comments)
.where(and(eq(comments.id, commentId), eq(comments.authorId, user.id)))
.limit(1);
@@ -41,6 +55,16 @@ export async function deleteComment({
);
}
+ if (existingComment.mediaKey) {
+ deletedMedia = {
+ // The comment's own videoId, not the client-supplied one, decides
+ // which bucket the objects are deleted from.
+ videoId: existingComment.videoId,
+ mediaKey: existingComment.mediaKey,
+ thumbnailKey: existingComment.mediaMeta?.thumbnailKey ?? null,
+ };
+ }
+
await tx
.delete(comments)
.where(and(eq(comments.id, commentId), eq(comments.authorId, user.id)));
@@ -76,6 +100,30 @@ export async function deleteComment({
}
});
+ // After the row is gone, best-effort delete of the media objects; a
+ // storage failure must not fail the action (the video-delete prefix sweep
+ // reclaims any stragglers).
+ if (deletedMedia) {
+ const { videoId: mediaVideoId, mediaKey, thumbnailKey } = deletedMedia;
+ await Effect.gen(function* () {
+ const repo = yield* VideosRepo;
+ const maybeVideo = yield* repo.getById(mediaVideoId);
+ if (Option.isNone(maybeVideo)) return;
+ const [bucket] = yield* Storage.getAccessForVideo(maybeVideo.value[0]);
+ yield* bucket.deleteObjects([
+ { Key: mediaKey },
+ ...(thumbnailKey ? [{ Key: thumbnailKey }] : []),
+ ]);
+ }).pipe(
+ // catchAllCause, not catchAll: a defect (missing credential, storage
+ // layer panic) must not fail the action either — the row is gone.
+ Effect.catchAllCause((cause) =>
+ Effect.logError("Failed to delete comment media objects", cause),
+ ),
+ EffectRuntime.runPromise,
+ );
+ }
+
revalidatePath(`/s/${videoId}`);
return { success: true };
} catch (error) {