Merge pull request #79 from wagesj45/codex/add-normalizerules-function-and-refactor
Refactor rule normalization
This commit is contained in:
commit
ea6952655b
1 changed files with 18 additions and 54 deletions
|
@ -30,6 +30,18 @@ let TurndownService = null;
|
||||||
let userTheme = 'auto';
|
let userTheme = 'auto';
|
||||||
let currentTheme = 'light';
|
let currentTheme = 'light';
|
||||||
|
|
||||||
|
function normalizeRules(rules) {
|
||||||
|
return Array.isArray(rules) ? rules.map(r => {
|
||||||
|
if (r.actions) return r;
|
||||||
|
const actions = [];
|
||||||
|
if (r.tag) actions.push({ type: 'tag', tagKey: r.tag });
|
||||||
|
if (r.moveTo) actions.push({ type: 'move', folder: r.moveTo });
|
||||||
|
const rule = { criterion: r.criterion, actions };
|
||||||
|
if (r.stopProcessing) rule.stopProcessing = true;
|
||||||
|
return rule;
|
||||||
|
}) : [];
|
||||||
|
}
|
||||||
|
|
||||||
function iconPaths(name) {
|
function iconPaths(name) {
|
||||||
return {
|
return {
|
||||||
16: `resources/img/${name}-${currentTheme}-16.png`,
|
16: `resources/img/${name}-${currentTheme}-16.png`,
|
||||||
|
@ -184,15 +196,7 @@ async function applyAiRules(idsInput) {
|
||||||
|
|
||||||
if (!aiRules.length) {
|
if (!aiRules.length) {
|
||||||
const { aiRules: stored } = await storage.local.get("aiRules");
|
const { aiRules: stored } = await storage.local.get("aiRules");
|
||||||
aiRules = Array.isArray(stored) ? stored.map(r => {
|
aiRules = normalizeRules(stored);
|
||||||
if (r.actions) return r;
|
|
||||||
const actions = [];
|
|
||||||
if (r.tag) actions.push({ type: 'tag', tagKey: r.tag });
|
|
||||||
if (r.moveTo) actions.push({ type: 'move', folder: r.moveTo });
|
|
||||||
const rule = { criterion: r.criterion, actions };
|
|
||||||
if (r.stopProcessing) rule.stopProcessing = true;
|
|
||||||
return rule;
|
|
||||||
}) : [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const msg of ids) {
|
for (const msg of ids) {
|
||||||
|
@ -275,15 +279,7 @@ async function clearCacheForMessages(idsInput) {
|
||||||
|
|
||||||
if (!aiRules.length) {
|
if (!aiRules.length) {
|
||||||
const { aiRules: stored } = await storage.local.get("aiRules");
|
const { aiRules: stored } = await storage.local.get("aiRules");
|
||||||
aiRules = Array.isArray(stored) ? stored.map(r => {
|
aiRules = normalizeRules(stored);
|
||||||
if (r.actions) return r;
|
|
||||||
const actions = [];
|
|
||||||
if (r.tag) actions.push({ type: 'tag', tagKey: r.tag });
|
|
||||||
if (r.moveTo) actions.push({ type: 'move', folder: r.moveTo });
|
|
||||||
const rule = { criterion: r.criterion, actions };
|
|
||||||
if (r.stopProcessing) rule.stopProcessing = true;
|
|
||||||
return rule;
|
|
||||||
}) : [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const keys = [];
|
const keys = [];
|
||||||
|
@ -330,28 +326,12 @@ async function clearCacheForMessages(idsInput) {
|
||||||
if (typeof timingStats.last !== 'number') {
|
if (typeof timingStats.last !== 'number') {
|
||||||
timingStats.last = -1;
|
timingStats.last = -1;
|
||||||
}
|
}
|
||||||
aiRules = Array.isArray(store.aiRules) ? store.aiRules.map(r => {
|
aiRules = normalizeRules(store.aiRules);
|
||||||
if (r.actions) return r;
|
|
||||||
const actions = [];
|
|
||||||
if (r.tag) actions.push({ type: 'tag', tagKey: r.tag });
|
|
||||||
if (r.moveTo) actions.push({ type: 'move', folder: r.moveTo });
|
|
||||||
const rule = { criterion: r.criterion, actions };
|
|
||||||
if (r.stopProcessing) rule.stopProcessing = true;
|
|
||||||
return rule;
|
|
||||||
}) : [];
|
|
||||||
logger.aiLog("configuration loaded", { debug: true }, store);
|
logger.aiLog("configuration loaded", { debug: true }, store);
|
||||||
storage.onChanged.addListener(async changes => {
|
storage.onChanged.addListener(async changes => {
|
||||||
if (changes.aiRules) {
|
if (changes.aiRules) {
|
||||||
const newRules = changes.aiRules.newValue || [];
|
const newRules = changes.aiRules.newValue || [];
|
||||||
aiRules = newRules.map(r => {
|
aiRules = normalizeRules(newRules);
|
||||||
if (r.actions) return r;
|
|
||||||
const actions = [];
|
|
||||||
if (r.tag) actions.push({ type: 'tag', tagKey: r.tag });
|
|
||||||
if (r.moveTo) actions.push({ type: 'move', folder: r.moveTo });
|
|
||||||
const rule = { criterion: r.criterion, actions };
|
|
||||||
if (r.stopProcessing) rule.stopProcessing = true;
|
|
||||||
return rule;
|
|
||||||
});
|
|
||||||
logger.aiLog("aiRules updated from storage change", { debug: true }, aiRules);
|
logger.aiLog("aiRules updated from storage change", { debug: true }, aiRules);
|
||||||
}
|
}
|
||||||
if (changes.htmlToMarkdown) {
|
if (changes.htmlToMarkdown) {
|
||||||
|
@ -499,15 +479,7 @@ async function clearCacheForMessages(idsInput) {
|
||||||
const subject = hdr?.subject || "";
|
const subject = hdr?.subject || "";
|
||||||
if (!aiRules.length) {
|
if (!aiRules.length) {
|
||||||
const { aiRules: stored } = await storage.local.get("aiRules");
|
const { aiRules: stored } = await storage.local.get("aiRules");
|
||||||
aiRules = Array.isArray(stored) ? stored.map(r => {
|
aiRules = normalizeRules(stored);
|
||||||
if (r.actions) return r;
|
|
||||||
const actions = [];
|
|
||||||
if (r.tag) actions.push({ type: 'tag', tagKey: r.tag });
|
|
||||||
if (r.moveTo) actions.push({ type: 'move', folder: r.moveTo });
|
|
||||||
const rule = { criterion: r.criterion, actions };
|
|
||||||
if (r.stopProcessing) rule.stopProcessing = true;
|
|
||||||
return rule;
|
|
||||||
}) : [];
|
|
||||||
}
|
}
|
||||||
const reasons = [];
|
const reasons = [];
|
||||||
for (const rule of aiRules) {
|
for (const rule of aiRules) {
|
||||||
|
@ -529,15 +501,7 @@ async function clearCacheForMessages(idsInput) {
|
||||||
const subject = hdr?.subject || "";
|
const subject = hdr?.subject || "";
|
||||||
if (!aiRules.length) {
|
if (!aiRules.length) {
|
||||||
const { aiRules: stored } = await storage.local.get("aiRules");
|
const { aiRules: stored } = await storage.local.get("aiRules");
|
||||||
aiRules = Array.isArray(stored) ? stored.map(r => {
|
aiRules = normalizeRules(stored);
|
||||||
if (r.actions) return r;
|
|
||||||
const actions = [];
|
|
||||||
if (r.tag) actions.push({ type: 'tag', tagKey: r.tag });
|
|
||||||
if (r.moveTo) actions.push({ type: 'move', folder: r.moveTo });
|
|
||||||
const rule = { criterion: r.criterion, actions };
|
|
||||||
if (r.stopProcessing) rule.stopProcessing = true;
|
|
||||||
return rule;
|
|
||||||
}) : [];
|
|
||||||
}
|
}
|
||||||
const results = [];
|
const results = [];
|
||||||
for (const rule of aiRules) {
|
for (const rule of aiRules) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue