-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from COS301-SE-2024/feat/editor-sessions
Feat/editor sessions
- Loading branch information
Showing
43 changed files
with
7,273 additions
and
4,764 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* v8 ignore start */ | ||
import { auth } from '../../../../auth'; | ||
import { NextResponse } from 'next/server'; | ||
import { ZodError } from 'zod'; | ||
import { createChapterSchema, editChapterSchema, updateChapterSchema } from '../../../../db/chapter-schema'; | ||
import { chapters, stories, versions } from '../../../../db/schema'; | ||
import { db } from '../../../../db/db'; | ||
import { eq } from 'drizzle-orm'; | ||
import { updateStorySchema } from '../../../../db/story-schema'; | ||
|
||
|
||
type UpdadtedChapter = any; | ||
|
||
|
||
const updateChapter = async (chapter: UpdadtedChapter) => { | ||
|
||
|
||
const result = await db.update(chapters).set({ | ||
cover: chapter.cover, | ||
title: chapter.title, | ||
content: chapter.content, | ||
order: chapter.order, | ||
published: chapter.published, | ||
}).where(eq(chapters.id, chapter.id)).returning({updatedId: chapters.id}); | ||
|
||
return result[0]; | ||
}; | ||
|
||
// const updateChapterMeta = async (chapter: any)=> { | ||
// const result = await db.update(chapters).set({ | ||
|
||
// }) | ||
// } | ||
|
||
export async function PUT(req: Request){ | ||
try { | ||
const session = await auth(); | ||
|
||
if (!session?.user){ | ||
return new NextResponse(JSON.stringify({ | ||
status: 'fail', message: "You are not logged in", | ||
}), { status : 401}) | ||
} | ||
|
||
// console.log(await req.json()); | ||
|
||
const input = editChapterSchema.parse(await req.json()); | ||
|
||
// : ensure user owns story | ||
// console.log(input); | ||
// @ts-ignore | ||
const chapter = await updateChapter({ | ||
...input, | ||
}); | ||
// console.log(story); | ||
|
||
|
||
return NextResponse.json({ | ||
chapter: { | ||
id: chapter.updatedId, | ||
}, | ||
}); | ||
} catch (error: any) { | ||
console.log(error); | ||
if (error instanceof ZodError) { | ||
return NextResponse.json( | ||
{ | ||
status: 'error', | ||
message: 'Validation failed', | ||
errors: error.errors, | ||
}, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
return NextResponse.json( | ||
{ | ||
status: 'error', | ||
message: error.message || 'Internal Server Error', | ||
}, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { auth } from 'apps/writeme/auth'; | ||
import { AccessToken } from 'livekit-server-sdk'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
export async function GET(req: NextRequest) { | ||
const room = req.nextUrl.searchParams.get('room'); | ||
|
||
const session = await auth(); | ||
|
||
// const username = req.nextUrl.searchParams.get("username"); | ||
|
||
const username = session?.user?.name || 'Guest User'; | ||
if (!room) { | ||
return NextResponse.json( | ||
{ error: 'Missing "room" query parameter' }, | ||
{ status: 400 } | ||
); | ||
} else if (!username) { | ||
return NextResponse.json( | ||
{ error: 'Missing "username" query parameter' }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
const apiKey = process.env.LIVEKIT_API_KEY; | ||
const apiSecret = process.env.LIVEKIT_API_SECRET; | ||
const wsUrl = process.env.NEXT_PUBLIC_LIVEKIT_URL; | ||
|
||
if (!apiKey || !apiSecret || !wsUrl) { | ||
return NextResponse.json( | ||
{ error: 'Server misconfigured' }, | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
const at = new AccessToken(apiKey, apiSecret, { identity: username }); | ||
|
||
at.addGrant({ room, roomJoin: true, canPublish: true, canSubscribe: true }); | ||
|
||
return NextResponse.json({ token: await at.toJwt() }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { isChapterOwner } from "apps/writeme/services/chapters"; | ||
import { auth } from "../../../../auth"; | ||
import { NextResponse, NextRequest } from "next/server"; | ||
import { createLiveEditorSession, createViewableSession, isViewableSessionOwner } from "apps/writeme/services/sessions"; | ||
import { z } from "zod"; | ||
import { db } from "apps/writeme/db/db"; | ||
import { viewableSessions } from "apps/writeme/db/schema"; | ||
import { eq } from "drizzle-orm"; | ||
|
||
|
||
export async function POST(req: NextRequest){ | ||
try { | ||
const session = await auth(); | ||
|
||
if (!session?.user){ | ||
return NextResponse.json({ | ||
status: 'fail', | ||
message: 'You are not logged in', | ||
}, { | ||
status: 401 | ||
}) | ||
} | ||
|
||
const { chapterId} = await req.json(); | ||
const userId = session.user.id || ""; | ||
|
||
const isOwner = await isChapterOwner(userId, chapterId); | ||
|
||
if (!isOwner){ | ||
return NextResponse.json({ | ||
status: 'fail', | ||
message: 'Only the owner can create a session' | ||
}, { | ||
status: 403 | ||
}) | ||
} | ||
|
||
const newSession = await createViewableSession(chapterId, userId); | ||
console.log(newSession) | ||
|
||
return NextResponse.json({ | ||
status: 'success', | ||
session: newSession.sessionId | ||
}, { | ||
status: 200 | ||
}) | ||
|
||
|
||
} catch (error: any){ | ||
console.log(error); | ||
return NextResponse.json({ | ||
|
||
}, { | ||
status: 500 | ||
}) | ||
} | ||
} | ||
|
||
export async function DELETE(req:NextRequest) { | ||
const session = await auth(); | ||
|
||
if (!session?.user){ | ||
return NextResponse.json({ | ||
status: 'fail', | ||
message: 'You are not logged in', | ||
}, { | ||
status: 401 | ||
}) | ||
} | ||
|
||
const { sessionId } = z.object({ | ||
sessionId: z.string() | ||
}).parse(await req.json()); | ||
|
||
if (!sessionId){ | ||
return NextResponse.json({ | ||
status: "failed", | ||
message: "No session Specified" | ||
}, {status: 400}) | ||
} | ||
|
||
|
||
const owner = await isViewableSessionOwner(sessionId); | ||
|
||
if (owner){ | ||
await db.delete(viewableSessions).where(eq(viewableSessions.id, sessionId)); | ||
return NextResponse.json({ | ||
status: "success", | ||
message: "Session has been deleted." | ||
}, {status: 200}) | ||
}else { | ||
return NextResponse.json({ | ||
status: "failed", | ||
message: "This Session is not owned by you." | ||
}, {status: 401}) | ||
} | ||
|
||
} |
Oops, something went wrong.