diff --git a/src/api/v1/timelines.ts b/src/api/v1/timelines.ts index 03651214..719bf43f 100644 --- a/src/api/v1/timelines.ts +++ b/src/api/v1/timelines.ts @@ -1,6 +1,7 @@ import { zValidator } from "@hono/zod-validator"; import { and, + asc, desc, eq, gt, @@ -78,6 +79,9 @@ app.get( ); } const query = c.req.valid("query"); + const minId = query.min_id; + const isForward = minId != null; + const sinceId = query.since_id; const timeline = await db.query.posts.findMany({ where: and( eq(posts.visibility, "public"), @@ -178,12 +182,17 @@ app.get( ), ), query.max_id == null ? undefined : lt(posts.id, query.max_id), - query.min_id == null ? undefined : gt(posts.id, query.min_id), + minId != null + ? gt(posts.id, minId) + : sinceId == null + ? undefined + : gt(posts.id, sinceId), ), with: getPostRelations(owner.id), - orderBy: [desc(posts.id)], + orderBy: [isForward ? asc(posts.id) : desc(posts.id)], limit: query.limit, }); + if (isForward) timeline.reverse(); const nextMaxId = timeline.length >= query.limit ? timeline[timeline.length - 1].id : null; const nextLink = nextMaxId == null ? undefined : new URL(c.req.url); @@ -209,6 +218,9 @@ app.get( ); } const query = c.req.valid("query"); + const minId = query.min_id; + const isForward = minId != null; + const sinceId = query.since_id; let timeline: Parameters[0][]; if (TIMELINE_INBOXES) { timeline = await db.query.posts.findMany({ @@ -224,12 +236,18 @@ app.get( query.max_id == null ? undefined : lt(timelinePosts.postId, query.max_id), - query.min_id == null - ? undefined - : gt(timelinePosts.postId, query.min_id), + minId != null + ? gt(timelinePosts.postId, minId) + : sinceId == null + ? undefined + : gt(timelinePosts.postId, sinceId), ), ) - .orderBy(desc(timelinePosts.postId)) + .orderBy( + isForward + ? asc(timelinePosts.postId) + : desc(timelinePosts.postId), + ) .limit(Math.min(TIMELINE_INBOX_LIMIT, query.limit)), ), // Hide future posts @@ -474,12 +492,17 @@ app.get( ), ), query.max_id == null ? undefined : lt(posts.id, query.max_id), - query.min_id == null ? undefined : gt(posts.id, query.min_id), + minId != null + ? gt(posts.id, minId) + : sinceId == null + ? undefined + : gt(posts.id, sinceId), ), with: getPostRelations(owner.id), - orderBy: [desc(posts.id)], + orderBy: [isForward ? asc(posts.id) : desc(posts.id)], limit: query.limit, }); + if (isForward) timeline.reverse(); } const nextMaxId = timeline.length >= query.limit ? timeline[timeline.length - 1].id : null; @@ -509,6 +532,9 @@ app.get( ); } const query = c.req.valid("query"); + const minId = query.min_id; + const isForward = minId != null; + const sinceId = query.since_id; const list = await db.query.lists.findFirst({ where: and(eq(lists.id, listId), eq(lists.accountOwnerId, owner.id)), }); @@ -528,12 +554,16 @@ app.get( query.max_id == null ? undefined : lt(listPosts.postId, query.max_id), - query.min_id == null - ? undefined - : gt(listPosts.postId, query.min_id), + minId != null + ? gt(listPosts.postId, minId) + : sinceId == null + ? undefined + : gt(listPosts.postId, sinceId), ), ) - .orderBy(desc(listPosts.postId)) + .orderBy( + isForward ? asc(listPosts.postId) : desc(listPosts.postId), + ) .limit(Math.min(TIMELINE_INBOX_LIMIT, query.limit)), ), // Hide future posts @@ -752,12 +782,17 @@ app.get( ), ), query.max_id == null ? undefined : lt(posts.id, query.max_id), - query.min_id == null ? undefined : gt(posts.id, query.min_id), + minId != null + ? gt(posts.id, minId) + : sinceId == null + ? undefined + : gt(posts.id, sinceId), ), with: getPostRelations(owner.id), - orderBy: [desc(posts.id)], + orderBy: [isForward ? asc(posts.id) : desc(posts.id)], limit: query.limit, }); + if (isForward) timeline.reverse(); } const nextMaxId = timeline.length >= query.limit ? timeline[timeline.length - 1].id : null; @@ -785,6 +820,9 @@ app.get( ); } const query = c.req.valid("query"); + const minId = query.min_id; + const isForward = minId != null; + const sinceId = query.since_id; const hashtag = `#${c.req.param("hashtag")}`; const timeline = await db.query.posts.findMany({ where: and( @@ -862,12 +900,17 @@ app.get( .where(eq(blocks.blockedAccountId, owner.id)), ), query.max_id == null ? undefined : lt(posts.id, query.max_id), - query.min_id == null ? undefined : gt(posts.id, query.min_id), + minId != null + ? gt(posts.id, minId) + : sinceId == null + ? undefined + : gt(posts.id, sinceId), ), with: getPostRelations(owner.id), - orderBy: [desc(posts.id)], + orderBy: [isForward ? asc(posts.id) : desc(posts.id)], limit: query.limit, }); + if (isForward) timeline.reverse(); const nextMaxId = timeline.length >= query.limit ? timeline[timeline.length - 1].id : null; const nextLink = nextMaxId == null ? undefined : new URL(c.req.url);