Skip to content

Commit 82036e7

Browse files
committed
Update conversation pagination and response format
1 parent e0ebf46 commit 82036e7

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/lib/server/api/routes/groups/conversations.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const conversationGroup = new Elysia().use(authPlugin).group("/conversati
2323
.get(
2424
"",
2525
async ({ locals, query }) => {
26+
const pageSize = CONV_NUM_PER_PAGE;
2627
const convs = await collections.conversations
2728
.find(authCondition(locals))
2829
.project<Pick<Conversation, "_id" | "title" | "updatedAt" | "model">>({
@@ -31,15 +32,12 @@ export const conversationGroup = new Elysia().use(authPlugin).group("/conversati
3132
model: 1,
3233
})
3334
.sort({ updatedAt: -1 })
34-
.skip((query.p ?? 0) * CONV_NUM_PER_PAGE)
35-
.limit(CONV_NUM_PER_PAGE)
35+
.skip((query.p ?? 0) * pageSize)
36+
.limit(pageSize + 1) // fetch one extra to detect next page
3637
.toArray();
3738

38-
const nConversations = await collections.conversations.countDocuments(
39-
authCondition(locals)
40-
);
41-
42-
const res = convs.map((conv) => ({
39+
const hasMore = convs.length > pageSize;
40+
const res = (hasMore ? convs.slice(0, pageSize) : convs).map((conv) => ({
4341
_id: conv._id,
4442
id: conv._id, // legacy param iOS
4543
title: conv.title,
@@ -48,7 +46,7 @@ export const conversationGroup = new Elysia().use(authPlugin).group("/conversati
4846
modelId: conv.model, // legacy param iOS
4947
}));
5048

51-
return { conversations: res, nConversations };
49+
return { conversations: res, hasMore };
5250
},
5351
{
5452
query: t.Object({

src/routes/+layout.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const load = async ({ depends, fetch, url }) => {
2121

2222
const defaultModel = models[0];
2323

24-
const { conversations: rawConversations, nConversations } = conversationsData;
24+
const { conversations: rawConversations } = conversationsData;
2525
const conversations = rawConversations.map((conv) => {
2626
const trimmedTitle = conv.title.trim();
2727

@@ -36,7 +36,6 @@ export const load = async ({ depends, fetch, url }) => {
3636
});
3737

3838
return {
39-
nConversations,
4039
conversations,
4140
models,
4241
oldModels: [],

0 commit comments

Comments
 (0)