From 2044c5cd158297b25eb3025053e4d794ba5f47ea Mon Sep 17 00:00:00 2001 From: mic Date: Sat, 27 Apr 2024 21:41:48 +0200 Subject: [PATCH] correctly selecting the right identity if found in the original message. fixes #31 --- js/mzta-utils.js | 45 +++++++++++++++++++++++++++++++++++++++++---- manifest.json | 1 + mzta-background.js | 2 ++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/js/mzta-utils.js b/js/mzta-utils.js index ac3d6c0b..189a8e15 100644 --- a/js/mzta-utils.js +++ b/js/mzta-utils.js @@ -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); - } \ No newline at end of file + 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] : ''; +} \ No newline at end of file diff --git a/manifest.json b/manifest.json index 90cb7a98..8af54ac1 100644 --- a/manifest.json +++ b/manifest.json @@ -28,6 +28,7 @@ "messagesModify", "tabs", "activeTab", + "accountsRead", "" ], "background": { diff --git a/mzta-background.js b/mzta-background.js index 63225d51..c0579db2 100644 --- a/mzta-background.js +++ b/mzta-background.js @@ -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.