correctly selecting the right identity if found in the original message. fixes #31

This commit is contained in:
mic 2024-04-27 21:41:48 +02:00
parent 5680d7d098
commit 2044c5cd15
3 changed files with 44 additions and 4 deletions

View file

@ -18,7 +18,44 @@
export function getLanguageDisplayName(languageCode) {
const languageDisplay = new Intl.DisplayNames([languageCode], {type: 'language'});
let lang_string = languageDisplay.of(languageCode);
return lang_string.charAt(0).toUpperCase() + lang_string.slice(1);
}
const languageDisplay = new Intl.DisplayNames([languageCode], {type: 'language'});
let lang_string = languageDisplay.of(languageCode);
return lang_string.charAt(0).toUpperCase() + lang_string.slice(1);
}
export async function getCurrentIdentity(msgHeader) {
let identities_array = [];
let fallback_identity = '';
let accounts = await browser.accounts.list();
for (let account of accounts) {
for (let identity of account.identities) {
identities_array.push({id: identity.id, email:identity.email})
if(fallback_identity === '') {
fallback_identity = {id: identity.id, email:identity.email}
}
}
}
// check if the author is an identity
let author = extractEmail(msgHeader.author);
let author_identity = identities_array.find(identity => identity.email === author);
if (author_identity) {
return author_identity.id;
}
let correspondents_array = msgHeader.bccList.concat(msgHeader.ccList, msgHeader.recipients);
correspondents_array = correspondents_array.map(correspondent => {
correspondent = extractEmail(correspondent);
return correspondent;
});
const matching_identity = correspondents_array.map(correspondent => identities_array.find(identity => identity.email === correspondent)).find(identity => identity !== undefined);
if(matching_identity) {
return matching_identity.id;
} else { // no identity found. using the fallback one
return fallback_identity.id;
}
}
function extractEmail(text) {
const emailRegex = /[\w.-]+@[\w.-]+\.\w+/;
const match = text.match(emailRegex);
return match ? match[0] : '';
}

View file

@ -28,6 +28,7 @@
"messagesModify",
"tabs",
"activeTab",
"accountsRead",
"<all_urls>"
],
"background": {

View file

@ -19,6 +19,7 @@
import { mzta_script } from './js/mzta-chatgpt.js';
import { prefs_default } from './options/mzta-options-default.js';
import { mzta_Menus } from './js/mzta-menus.js';
import { getCurrentIdentity } from './js/mzta-utils.js';
var createdWindowID = null;
@ -77,6 +78,7 @@ messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) =>
type: "reply",
//body: paragraphsHtmlString,
isPlainText: false,
identityId: await getCurrentIdentity(mailMessage),
})
// Wait for tab loaded.