Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions apps/web/actions/videos/new-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { nanoId } from "@cap/database/helpers";
import { comments } from "@cap/database/schema";
import { comments, videos } from "@cap/database/schema";
import { makeCurrentUserLayer, VideosPolicy } from "@cap/web-backend";
import type { ImageUpload } from "@cap/web-domain";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor perf/consistency nit: provideOptionalAuth will call getServerSession() + query users again. Since this action already requires auth and you already have user, you can provide the current-user layer directly and skip the extra DB hit.

Suggested change
import type { ImageUpload } from "@cap/web-domain";
import { makeCurrentUserLayer, VideosPolicy } from "@cap/web-backend";

import { Comment, type Video } from "@cap/web-domain";
import { Comment, Policy, type Video } from "@cap/web-domain";
import { eq } from "drizzle-orm";
import { Effect, Exit } from "effect";
import { revalidatePath } from "next/cache";
import { createNotification } from "@/lib/Notification";
import * as EffectRuntime from "@/lib/server";

export async function newComment(data: {
content: string;
Expand Down Expand Up @@ -37,6 +41,36 @@ export async function newComment(data: {
if (!content || !videoId) {
throw new Error("Content and videoId are required");
}

// The caller controls `videoId`, so being signed in is not enough — without
// this check any authenticated user can insert comments into a video they
// cannot view, including someone else's private recording.
const accessExit = await Effect.gen(function* () {
const videosPolicy = yield* VideosPolicy;

return yield* Effect.promise(() =>
db()
.select({ id: videos.id })
.from(videos)
.where(eq(videos.id, videoId))
.limit(1),
).pipe(Policy.withPublicPolicy(videosPolicy.canView(videoId)));
}).pipe(
// This action already required auth above, so provide the user we have
// rather than `provideOptionalAuth`, which would re-run getServerSession()
// and re-query `users`.
Effect.provide(makeCurrentUserLayer(user)),
EffectRuntime.runPromiseExit,
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following on the above import, this avoids re-fetching the current user inside the Effect env.

Suggested change
}).pipe(Effect.provide(makeCurrentUserLayer(user)), EffectRuntime.runPromiseExit);

if (Exit.isFailure(accessExit)) {
throw new Error("Video not found");
}

if (accessExit.value.length === 0) {
throw new Error("Video not found");
}

const id = Comment.CommentId.make(nanoId());

const newComment = {
Expand Down