feat(guild-messaging): add seq-range pagination with limit for message listing

This commit is contained in:
nav
2026-05-12 10:32:25 +00:00
parent c08fa4756b
commit 4b4755b33b
2 changed files with 20 additions and 2 deletions

View File

@@ -16,6 +16,8 @@ type Message = {
};
const EDIT_WINDOW_MS = 15 * 60 * 1000;
const DEFAULT_PAGE_LIMIT = 50;
const MAX_PAGE_LIMIT = 200;
@Controller('channels/:id/messages')
export class MessagingController {
@@ -85,12 +87,28 @@ export class MessagingController {
@Param('id') channelId: string,
@Query('seq_from') seqFrom?: string,
@Query('seq_to') seqTo?: string,
@Query('limit') limit?: string,
) {
const from = seqFrom ? Number(seqFrom) : 1;
const to = seqTo ? Number(seqTo) : Number.MAX_SAFE_INTEGER;
const requestedLimit = limit ? Number(limit) : DEFAULT_PAGE_LIMIT;
const safeLimit =
Number.isFinite(requestedLimit) && requestedLimit > 0
? Math.min(requestedLimit, MAX_PAGE_LIMIT)
: DEFAULT_PAGE_LIMIT;
const arr = this.messagesByChannel.get(channelId) ?? [];
const filtered = arr.filter((m) => m.seq >= from && m.seq <= to);
const items = filtered.slice(0, safeLimit);
return {
items: arr.filter((m) => m.seq >= from && m.seq <= to),
items,
page: {
seqFrom: from,
seqTo: to,
limit: safeLimit,
returned: items.length,
hasMore: filtered.length > items.length,
},
};
}
}