ThunderAI/js/mzta-storage.js

249 lines
9 KiB
JavaScript

/*
* ThunderAI [https://micz.it/thunderbird-addon-thunderai/]
* Copyright (C) 2024 - 2026 Mic (m@micz.it)
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { taLogger } from './mzta-logger.js';
export class taStorage {
static STORAGE_KEY_PREFIX = 'msg:';
static SCHEMA_VERSION = 1;
taLog = null;
constructor(do_debug = false) {
this.taLog = new taLogger("mzta-storage", do_debug);
}
/**
* Build the storage key for a given Message-ID.
* @param {string} messageId - The Message-ID header string.
* @returns {string} The prefixed storage key.
*/
_buildKey(messageId) {
return taStorage.STORAGE_KEY_PREFIX + messageId;
}
/**
* Read the full record for a given Message-ID.
* @param {string} messageId - The Message-ID header string.
* @returns {Promise<object|null>} The record object or null if not found.
*/
async getRecord(messageId) {
try {
let key = this._buildKey(messageId);
let result = await messenger.storage.local.get(key);
return result[key] || null;
} catch (e) {
this.taLog.error('getRecord error: ' + e);
return null;
}
}
/**
* Check if a record contains the specified field.
* @param {object|null} record - The record object (from getRecord).
* @param {string} field - The field name to check ("spam", "summary", or "translation").
* @returns {boolean} True if the record exists and the field is present.
*/
hasField(record, field) {
try {
return record !== null && record !== undefined && field in record;
} catch (e) {
this.taLog.error('hasField error: ' + e);
return false;
}
}
/**
* Write the spam field for a given Message-ID.
* @param {string} messageId - The Message-ID header string.
* @param {object} report_data - The full spam report object with fields:
* spamValue, explanation, subject, from, message_date, moved, SpamThreshold.
* @param {boolean} [force=true] - If true, overwrite existing spam data.
*/
async writeSpam(messageId, report_data, force = true) {
try {
let key = this._buildKey(messageId);
let record = await this.getRecord(messageId) || { v: taStorage.SCHEMA_VERSION };
if ('spam' in record && !force) {
return;
}
let now = Date.now();
record.spam = {
spamValue: report_data.spamValue,
explanation: report_data.explanation,
subject: report_data.subject,
from: report_data.from,
message_date: report_data.message_date instanceof Date
? report_data.message_date.toISOString()
: report_data.message_date,
moved: report_data.moved,
SpamThreshold: report_data.SpamThreshold,
ts: now,
};
record.ts = now;
await messenger.storage.local.set({ [key]: record });
} catch (e) {
this.taLog.error('writeSpam error: ' + e);
}
}
/**
* Get all records that contain a spam field.
* @returns {Promise<object>} Map of messageId -> spam data object (legacy shape).
*/
async getAllSpamRecords() {
try {
let all = await messenger.storage.local.get(null);
let result = {};
for (let [key, record] of Object.entries(all)) {
if (!key.startsWith(taStorage.STORAGE_KEY_PREFIX)) continue;
if (!this.hasField(record, 'spam')) continue;
let messageId = key.slice(taStorage.STORAGE_KEY_PREFIX.length);
let spam = record.spam;
result[messageId] = {
headerMessageId: messageId,
spamValue: spam.spamValue,
explanation: spam.explanation,
report_date: new Date(spam.ts),
subject: spam.subject,
from: spam.from,
message_date: spam.message_date,
moved: spam.moved,
SpamThreshold: spam.SpamThreshold,
};
}
return result;
} catch (e) {
this.taLog.error('getAllSpamRecords error: ' + e);
return {};
}
}
/**
* Delete only the spam field from a record.
* Deletes the entire record if no other data fields remain.
* @param {string} messageId - The Message-ID header string.
*/
async deleteSpamField(messageId) {
try {
let key = this._buildKey(messageId);
let record = await this.getRecord(messageId);
if (!record || !('spam' in record)) return;
delete record.spam;
const remainingFields = Object.keys(record).filter(k => k !== 'v' && k !== 'ts');
if (remainingFields.length === 0) {
await messenger.storage.local.remove(key);
} else {
await messenger.storage.local.set({ [key]: record });
}
} catch (e) {
this.taLog.error('deleteSpamField error: ' + e);
}
}
/**
* Write the summary field for a given Message-ID.
* @param {string} messageId - The Message-ID header string.
* @param {string} text - The summary text.
* @param {boolean} [force=true] - If true, overwrite existing summary data.
*/
async writeSummary(messageId, text, force = true) {
try {
let key = this._buildKey(messageId);
let record = await this.getRecord(messageId) || { v: taStorage.SCHEMA_VERSION };
if ('summary' in record && !force) {
return;
}
let now = Date.now();
record.summary = { text: text, ts: now };
record.ts = now;
await messenger.storage.local.set({ [key]: record });
} catch (e) {
this.taLog.error('writeSummary error: ' + e);
}
}
/**
* Write the translation field for a given Message-ID.
* @param {string} messageId - The Message-ID header string.
* @param {string} translated_text - The translated text.
* @param {string} lang - Target language code.
* @param {boolean} [force=true] - If true, overwrite existing translation data.
*/
async writeTranslation(messageId, translated_text, lang, force = true) {
try {
let key = this._buildKey(messageId);
let record = await this.getRecord(messageId) || { v: taStorage.SCHEMA_VERSION };
if ('translation' in record && !force) {
return;
}
let now = Date.now();
record.translation = { translated_text: translated_text, lang: lang, ts: now };
record.ts = now;
await messenger.storage.local.set({ [key]: record });
} catch (e) {
this.taLog.error('writeTranslation error: ' + e);
}
}
/**
* Delete the entire record for a given Message-ID.
* @param {string} messageId - The Message-ID header string.
*/
async deleteRecord(messageId) {
try {
let key = this._buildKey(messageId);
await messenger.storage.local.remove(key);
} catch (e) {
this.taLog.error('deleteRecord error: ' + e);
}
}
/**
* Remove all records older than maxAgeDays.
* @param {number} maxAgeDays - Maximum age in days. If 0, does nothing.
* @returns {Promise<number>} The number of deleted records.
*/
async cleanup(maxAgeDays) {
if (maxAgeDays === 0) {
return 0;
}
try {
let all = await messenger.storage.local.get(null);
let cutoff = Date.now() - (maxAgeDays * 24 * 60 * 60 * 1000);
let keysToDelete = [];
for (let key of Object.keys(all)) {
if (!key.startsWith(taStorage.STORAGE_KEY_PREFIX)) {
continue;
}
let record = all[key];
if (record.ts && record.ts < cutoff) {
keysToDelete.push(key);
}
}
if (keysToDelete.length > 0) {
await messenger.storage.local.remove(keysToDelete);
}
return keysToDelete.length;
} catch (e) {
this.taLog.error('cleanup error: ' + e);
return 0;
}
}
}