ignore spam and trash emails

This commit is contained in:
Arjun 2026-05-13 14:05:52 +05:30
parent b01af12148
commit f371cd4bb1

View file

@ -164,6 +164,16 @@ async function processThread(auth: OAuth2Client, threadId: string, syncDir: stri
if (!messages || messages.length === 0) return null; if (!messages || messages.length === 0) return null;
// Skip threads in SPAM or TRASH (Gmail labels them at the message level).
const isExcluded = messages.some(m => {
const labels = m.labelIds ?? [];
return labels.includes('SPAM') || labels.includes('TRASH');
});
if (isExcluded) {
console.log(`Skipping thread ${threadId} (SPAM/TRASH)`);
return null;
}
// Subject from first message // Subject from first message
const firstHeader = messages[0].payload?.headers; const firstHeader = messages[0].payload?.headers;
const subject = firstHeader?.find(h => h.name === 'Subject')?.value || '(No Subject)'; const subject = firstHeader?.find(h => h.name === 'Subject')?.value || '(No Subject)';
@ -279,7 +289,7 @@ async function fullSync(auth: OAuth2Client, syncDir: string, attachmentsDir: str
do { do {
const res = await gmail.users.threads.list({ const res = await gmail.users.threads.list({
userId: 'me', userId: 'me',
q: `after:${dateQuery}`, q: `after:${dateQuery} -in:spam -in:trash`,
pageToken pageToken
}); });
@ -393,6 +403,8 @@ async function partialSync(auth: OAuth2Client, startHistoryId: string, syncDir:
for (const record of changes) { for (const record of changes) {
if (record.messagesAdded) { if (record.messagesAdded) {
for (const item of record.messagesAdded) { for (const item of record.messagesAdded) {
const labels = item.message?.labelIds ?? [];
if (labels.includes('SPAM') || labels.includes('TRASH')) continue;
if (item.message?.threadId) { if (item.message?.threadId) {
threadIds.add(item.message.threadId); threadIds.add(item.message.threadId);
} }