From 67522cf0f837757d4ad0f897025f8b738fb7030b Mon Sep 17 00:00:00 2001 From: martgil Date: Thu, 24 Oct 2024 17:20:03 +0800 Subject: [PATCH] remove code duplicates --- extension/js/common/browser/browser-msg.ts | 6 ++---- .../thunderbird/thunderbird-element-replacer.ts | 9 +++------ extension/js/service_worker/bg-handlers.ts | 17 ++++++++--------- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/extension/js/common/browser/browser-msg.ts b/extension/js/common/browser/browser-msg.ts index c72f4a0c937..cdf867c2e96 100644 --- a/extension/js/common/browser/browser-msg.ts +++ b/extension/js/common/browser/browser-msg.ts @@ -99,7 +99,6 @@ export namespace Bm { export type PgpBlockRetry = { frameId: string; messageSender: Dest }; export type PgpBlockReady = { frameId: string; messageSender: Dest }; export type ThunderbirdOpenPassphraseDialog = { acctEmail: string; longids: string }; - export type ThunderbirdGetDownloadableAttachment = { attachments: messenger.messages.MessageAttachment[] }; export type ThunderbirdInitiateAttachmentDownload = { decryptedFileName: string; decryptedContent: Buf }; export namespace Res { @@ -181,7 +180,6 @@ export namespace Bm { | PgpBlockReady | PgpBlockRetry | ConfirmationResult - | ThunderbirdGetDownloadableAttachment | ThunderbirdOpenPassphraseDialog | ThunderbirdInitiateAttachmentDownload | Ajax; @@ -247,8 +245,8 @@ export class BrowserMsg { BrowserMsg.sendAwait(undefined, 'expirationCacheSet', bm, true) as Promise, expirationCacheDeleteExpired: (bm: Bm.ExpirationCacheDeleteExpired) => BrowserMsg.sendAwait(undefined, 'expirationCacheDeleteExpired', bm, true) as Promise, - thunderbirdGetDownloadableAttachment: (bm: Bm.ThunderbirdGetDownloadableAttachment) => - BrowserMsg.sendAwait(undefined, 'thunderbirdGetDownloadableAttachment', bm, true) as Promise, + thunderbirdGetDownloadableAttachment: () => + BrowserMsg.sendAwait(undefined, 'thunderbirdGetDownloadableAttachment', undefined, true) as Promise, thunderbirdInitiateAttachmentDownload: (bm: Bm.ThunderbirdInitiateAttachmentDownload) => BrowserMsg.sendAwait(undefined, 'thunderbirdInitiateAttachmentDownload', bm, true) as Promise, thunderbirdGetCurrentUser: () => diff --git a/extension/js/content_scripts/webmail/thunderbird/thunderbird-element-replacer.ts b/extension/js/content_scripts/webmail/thunderbird/thunderbird-element-replacer.ts index 13674b096ef..0772bffc93f 100644 --- a/extension/js/content_scripts/webmail/thunderbird/thunderbird-element-replacer.ts +++ b/extension/js/content_scripts/webmail/thunderbird/thunderbird-element-replacer.ts @@ -31,7 +31,6 @@ export class ThunderbirdElementReplacer extends WebmailElementReplacer { public replaceThunderbirdMsgPane = async () => { const emailBodyToParse = $('div.moz-text-plain').text().trim() || $('div.moz-text-html').text().trim(); if (Catch.isThunderbirdMail()) { - const { attachments } = await BrowserMsg.send.bg.await.thunderbirdMsgGet(); const pgpRegex = /-----BEGIN PGP MESSAGE-----(.*?)-----END PGP MESSAGE-----/s; const pgpRegexMatch = new RegExp(pgpRegex).exec(emailBodyToParse); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -43,11 +42,9 @@ export class ThunderbirdElementReplacer extends WebmailElementReplacer { } else if (this.resemblesSignedMsg(emailBodyToParse)) { await this.messageVerify(signerKeys); } - if (attachments.length) { - const fcAttachments = await BrowserMsg.send.bg.await.thunderbirdGetDownloadableAttachment({ attachments }); - for (const fcAttachment of fcAttachments) { - await this.attachmentUiRenderer(fcAttachment, signerKeys, emailBodyToParse); - } + const fcAttachments = await BrowserMsg.send.bg.await.thunderbirdGetDownloadableAttachment(); + for (const fcAttachment of fcAttachments) { + await this.attachmentUiRenderer(fcAttachment, signerKeys, emailBodyToParse); } } }; diff --git a/extension/js/service_worker/bg-handlers.ts b/extension/js/service_worker/bg-handlers.ts index 428da4702a1..ac0d65ad563 100644 --- a/extension/js/service_worker/bg-handlers.ts +++ b/extension/js/service_worker/bg-handlers.ts @@ -163,23 +163,22 @@ export class BgHandlers { }); }; - public static thunderbirdGetDownloadableAttachment = async ( - r: Bm.ThunderbirdGetDownloadableAttachment - ): Promise => { + public static thunderbirdGetDownloadableAttachment = async (): Promise => { const processableAttachments: Bm.Res.ThunderbirdGetDownloadableAttachment = []; const [tab] = await messenger.mailTabs.query({ active: true, currentWindow: true }); const message = await messenger.messageDisplay.getDisplayedMessage(tab.id); if (tab.id && message?.id) { + const attachments = await messenger.messages.listAttachments(message.id); const fcAttachments: Attachment[] = []; // convert Thunderbird Attachments to FlowCrypt recognizable Attachments - for (const tbAttachment of r.attachments) { - const rawAttachment = await messenger.messages.getAttachmentFile(message.id, tbAttachment.partName); + for (const attachment of attachments) { + const file = await messenger.messages.getAttachmentFile(message.id, attachment.partName); fcAttachments.push( new Attachment({ - data: new Uint8Array(await rawAttachment.arrayBuffer()), - type: tbAttachment.contentType, - name: tbAttachment.name, - length: tbAttachment.size, + data: new Uint8Array(await file.arrayBuffer()), + type: attachment.contentType, + name: attachment.name, + length: attachment.size, }) ); }