Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/lib/server/services/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,24 @@ export async function search(
Object.keys(criteria).length > 0 ? criteria : { all: true };

const result = await client.search(searchCriteria, { uid: true });
// search() returns number[] | false
const uids: number[] = result === false ? [] : result;

if (uids.length === 0) return [];

// Take the most recent `limit` UIDs (largest UIDs = newest)
const slicedUids = uids.slice(-limit);

// Build UID range string — some IMAP servers reject array-based fetch
const uidRange = slicedUids.length === 1
? String(slicedUids[0])
: `${slicedUids[0]}:${slicedUids[slicedUids.length - 1]}`;

const results: MessageSummary[] = [];

for await (const msg of client.fetch(
slicedUids,
uidRange,
{ uid: true, envelope: true, flags: true, bodyStructure: true },
{ uid: true },
)) {
const env = msg.envelope;
const hasAttachments = hasAttachmentInStructure(msg.bodyStructure);
Expand Down
Loading