-
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 #8 from YubaNeupane/dev
Dev
- Loading branch information
Showing
26 changed files
with
1,704 additions
and
216 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
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,80 @@ | ||
import { currentProfile } from "@/lib/current-profile"; | ||
import { db } from "@/lib/db"; | ||
import { Message } from "@prisma/client"; | ||
import { NextResponse } from "next/server"; | ||
|
||
const MESSAGE_BATCH = 10; | ||
|
||
export async function GET(req: Request) { | ||
try { | ||
const profile = await currentProfile(); | ||
const { searchParams } = new URL(req.url); | ||
|
||
const cursor = searchParams.get("cursor"); | ||
const channelId = searchParams.get("channelId"); | ||
|
||
if (!profile) { | ||
return new NextResponse("Unauthorized", { status: 401 }); | ||
} | ||
|
||
if (!channelId) { | ||
return new NextResponse("Channel ID missing", { status: 400 }); | ||
} | ||
|
||
let messages: Message[] = []; | ||
|
||
if (cursor) { | ||
messages = await db.message.findMany({ | ||
take: MESSAGE_BATCH, | ||
skip: 1, | ||
cursor: { | ||
id: cursor, | ||
}, | ||
where: { | ||
channelId, | ||
}, | ||
include: { | ||
member: { | ||
include: { | ||
profile: true, | ||
}, | ||
}, | ||
}, | ||
orderBy: { | ||
createdAt: "desc", | ||
}, | ||
}); | ||
} else { | ||
messages = await db.message.findMany({ | ||
take: MESSAGE_BATCH, | ||
where: { | ||
channelId, | ||
}, | ||
include: { | ||
member: { | ||
include: { | ||
profile: true, | ||
}, | ||
}, | ||
}, | ||
orderBy: { | ||
createdAt: "desc", | ||
}, | ||
}); | ||
} | ||
|
||
let nextCursor = null; | ||
|
||
if (messages.length === MESSAGE_BATCH) { | ||
nextCursor = messages[messages.length - 1].id; | ||
} | ||
|
||
return NextResponse.json({ | ||
items: messages, | ||
nextCursor, | ||
}); | ||
} catch (error) { | ||
console.log("[MESSAGES_GET]", error); | ||
return new NextResponse("Internal 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
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
Oops, something went wrong.
dd60c20
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
discord-clone – ./
discord-clone-git-main-yubaneupane.vercel.app
discord-clone-yubaneupane.vercel.app