Preserve existing tags when applying multiple rules

This commit is contained in:
Jordan Wages 2025-06-29 00:07:37 -05:00
commit 3e1df7be3f

View file

@ -111,6 +111,13 @@ async function applyAiRules(idsInput) {
try { try {
const full = await messenger.messages.getFull(id); const full = await messenger.messages.getFull(id);
const text = buildEmailText(full); const text = buildEmailText(full);
let currentTags = [];
try {
const hdr = await messenger.messages.get(id);
currentTags = Array.isArray(hdr.tags) ? [...hdr.tags] : [];
} catch (e) {
currentTags = [];
}
for (const rule of aiRules) { for (const rule of aiRules) {
const cacheKey = await AiClassifier.buildCacheKey(id, rule.criterion); const cacheKey = await AiClassifier.buildCacheKey(id, rule.criterion);
@ -118,7 +125,10 @@ async function applyAiRules(idsInput) {
if (matched) { if (matched) {
for (const act of (rule.actions || [])) { for (const act of (rule.actions || [])) {
if (act.type === 'tag' && act.tagKey) { if (act.type === 'tag' && act.tagKey) {
await messenger.messages.update(id, { tags: [act.tagKey] }); if (!currentTags.includes(act.tagKey)) {
currentTags.push(act.tagKey);
await messenger.messages.update(id, { tags: currentTags });
}
} else if (act.type === 'move' && act.folder) { } else if (act.type === 'move' && act.folder) {
await messenger.messages.move([id], act.folder); await messenger.messages.move([id], act.folder);
} else if (act.type === 'junk') { } else if (act.type === 'junk') {