Fetch message history through the client REST layer

This commit is contained in:
Fisto
2026-09-17 00:09:47 +02:00
parent f4d71cf4f9
commit 089bca0d4f
4 changed files with 61 additions and 14 deletions
+52 -12
View File
@@ -23,6 +23,7 @@ import {
MessageStore,
PresenceStore,
ReadStateStore,
RestAPI,
UserStore,
VoiceStateStore
} from "@vencord/types/webpack/common";
@@ -961,24 +962,63 @@ export async function handlePuppetAction(action: string, params: Record<string,
const before = params.before as string | undefined;
const after = params.after as string | undefined;
await fetchMessages({
channelId,
limit: Number(limit),
...(before ? { before } : {}),
...(after ? { after } : {})
});
const all = MessageStore.getMessages(channelId)?._array ?? [];
const filtered = all.filter(message => {
const inRange = (message: any) => {
if (after && compareIds(message.id, after) <= 0) return false;
if (before && compareIds(message.id, before) >= 0) return false;
return true;
});
};
const normalize = (list: any[]) => {
const seen = new Set<string>();
return list
.filter(message => inRange(message) && !seen.has(message.id) && seen.add(message.id))
.sort((a, b) => compareIds(a.id, b.id))
.slice(-Number(limit));
};
// Preferred: the client's REST layer paginates properly. The store keeps only
// the newest messages plus the channel's first, so reading it back yields tiny,
// non-contiguous pages.
let messages: any[] | null = null;
let transport = "rest";
try {
const query: Record<string, string | number> = { limit: Number(limit) };
if (before) query.before = before;
if (after) query.after = after;
const response = await RestAPI.get({
url: `/channels/${channelId}/messages`,
query: { ...query }
});
const list = Array.isArray(response)
? response
: Array.isArray((response as any)?.body)
? (response as any).body
: null;
if (list) messages = normalize(list);
} catch {
messages = null;
}
if (messages === null) {
transport = "store";
await fetchMessages({
channelId,
limit: Number(limit),
...(before ? { before } : {}),
...(after ? { after } : {})
});
const all = MessageStore.getMessages(channelId)?._array ?? [];
messages = normalize(all);
}
return ok({
channelId,
fetched: filtered.length,
messages: filtered.slice(-Number(limit)).map(mapMessage)
fetched: messages.length,
transport,
messages: messages.map(mapMessage)
});
}