victor HF Staff commited on
Commit
2d68a5c
·
1 Parent(s): ec5d85c

Update conversation pagination and response format

Browse files
src/lib/server/api/routes/groups/conversations.ts CHANGED
@@ -23,6 +23,7 @@ export const conversationGroup = new Elysia().use(authPlugin).group("/conversati
23
  .get(
24
  "",
25
  async ({ locals, query }) => {
 
26
  const convs = await collections.conversations
27
  .find(authCondition(locals))
28
  .project<Pick<Conversation, "_id" | "title" | "updatedAt" | "model">>({
@@ -31,15 +32,12 @@ export const conversationGroup = new Elysia().use(authPlugin).group("/conversati
31
  model: 1,
32
  })
33
  .sort({ updatedAt: -1 })
34
- .skip((query.p ?? 0) * CONV_NUM_PER_PAGE)
35
- .limit(CONV_NUM_PER_PAGE)
36
  .toArray();
37
 
38
- const nConversations = await collections.conversations.countDocuments(
39
- authCondition(locals)
40
- );
41
-
42
- const res = convs.map((conv) => ({
43
  _id: conv._id,
44
  id: conv._id, // legacy param iOS
45
  title: conv.title,
@@ -48,7 +46,7 @@ export const conversationGroup = new Elysia().use(authPlugin).group("/conversati
48
  modelId: conv.model, // legacy param iOS
49
  }));
50
 
51
- return { conversations: res, nConversations };
52
  },
53
  {
54
  query: t.Object({
 
23
  .get(
24
  "",
25
  async ({ locals, query }) => {
26
+ const pageSize = CONV_NUM_PER_PAGE;
27
  const convs = await collections.conversations
28
  .find(authCondition(locals))
29
  .project<Pick<Conversation, "_id" | "title" | "updatedAt" | "model">>({
 
32
  model: 1,
33
  })
34
  .sort({ updatedAt: -1 })
35
+ .skip((query.p ?? 0) * pageSize)
36
+ .limit(pageSize + 1) // fetch one extra to detect next page
37
  .toArray();
38
 
39
+ const hasMore = convs.length > pageSize;
40
+ const res = (hasMore ? convs.slice(0, pageSize) : convs).map((conv) => ({
 
 
 
41
  _id: conv._id,
42
  id: conv._id, // legacy param iOS
43
  title: conv.title,
 
46
  modelId: conv.model, // legacy param iOS
47
  }));
48
 
49
+ return { conversations: res, hasMore };
50
  },
51
  {
52
  query: t.Object({
src/routes/+layout.ts CHANGED
@@ -21,7 +21,7 @@ export const load = async ({ depends, fetch, url }) => {
21
 
22
  const defaultModel = models[0];
23
 
24
- const { conversations: rawConversations, nConversations } = conversationsData;
25
  const conversations = rawConversations.map((conv) => {
26
  const trimmedTitle = conv.title.trim();
27
 
@@ -36,7 +36,6 @@ export const load = async ({ depends, fetch, url }) => {
36
  });
37
 
38
  return {
39
- nConversations,
40
  conversations,
41
  models,
42
  oldModels: [],
 
21
 
22
  const defaultModel = models[0];
23
 
24
+ const { conversations: rawConversations } = conversationsData;
25
  const conversations = rawConversations.map((conv) => {
26
  const trimmedTitle = conv.title.trim();
27
 
 
36
  });
37
 
38
  return {
 
39
  conversations,
40
  models,
41
  oldModels: [],