@@ -5,12 +5,14 @@ import {
55import { updateIfDefined } from "@cap/database" ;
66import * as Db from "@cap/database/schema" ;
77import { serverEnv } from "@cap/env" ;
8+ import { HttpApiError } from "@effect/platform" ;
89import {
910 AwsCredentials ,
1011 Database ,
1112 provideOptionalAuth ,
1213 S3Buckets ,
1314 Videos ,
15+ VideosRepo ,
1416} from "@cap/web-backend" ;
1517import { CurrentUser , Video } from "@cap/web-domain" ;
1618import { zValidator } from "@hono/zod-validator" ;
@@ -52,11 +54,14 @@ app.post(
5254 const videoId = Video . VideoId . make ( videoIdRaw ) ;
5355
5456 const resp = await Effect . gen ( function * ( ) {
55- const videos = yield * Videos ;
57+ const user = yield * CurrentUser ;
58+ const repo = yield * VideosRepo ;
5659 const db = yield * Database ;
5760
58- const video = yield * videos . getByIdForOwner ( videoId ) ;
61+ const video = yield * repo . getById ( videoId ) ;
5962 if ( Option . isNone ( video ) ) return yield * new Video . NotFoundError ( ) ;
63+ if ( video . value [ 0 ] . ownerId !== user . id )
64+ return yield * new HttpApiError . Unauthorized ( ) ;
6065
6166 yield * db . use ( ( db ) =>
6267 db
@@ -71,6 +76,11 @@ app.post(
7176 if ( e . _tag === "VideoNotFoundError" )
7277 return Effect . succeed < Response > ( c . text ( "Video not found" , 404 ) ) ;
7378
79+ if ( e . _tag === "Unauthorized" )
80+ return Effect . succeed < Response > (
81+ c . text ( "User not authenticated" , 401 ) ,
82+ ) ;
83+
7484 return Effect . succeed < Response > (
7585 c . json ( { error : "Error initiating multipart upload" } , 500 ) ,
7686 ) ;
@@ -471,10 +481,6 @@ app.post(
471481 ) ;
472482 } ) ,
473483 ) ;
474- } ) . pipe (
475- provideOptionalAuth ,
476- Effect . provideService ( CurrentUser , user ) ,
477- runPromise ,
478- ) ;
484+ } ) . pipe ( Effect . provideService ( CurrentUser , user ) , runPromise ) ;
479485 } ,
480486) ;
0 commit comments