Skip to content

Commit

Permalink
remove code duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
martgil committed Oct 24, 2024
1 parent f707f6f commit 67522cf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
6 changes: 2 additions & 4 deletions extension/js/common/browser/browser-msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -181,7 +180,6 @@ export namespace Bm {
| PgpBlockReady
| PgpBlockRetry
| ConfirmationResult
| ThunderbirdGetDownloadableAttachment
| ThunderbirdOpenPassphraseDialog
| ThunderbirdInitiateAttachmentDownload
| Ajax;
Expand Down Expand Up @@ -247,8 +245,8 @@ export class BrowserMsg {
BrowserMsg.sendAwait(undefined, 'expirationCacheSet', bm, true) as Promise<Bm.Res.ExpirationCacheSet>,
expirationCacheDeleteExpired: (bm: Bm.ExpirationCacheDeleteExpired) =>
BrowserMsg.sendAwait(undefined, 'expirationCacheDeleteExpired', bm, true) as Promise<Bm.Res.ExpirationCacheDeleteExpired>,
thunderbirdGetDownloadableAttachment: (bm: Bm.ThunderbirdGetDownloadableAttachment) =>
BrowserMsg.sendAwait(undefined, 'thunderbirdGetDownloadableAttachment', bm, true) as Promise<Bm.Res.ThunderbirdGetDownloadableAttachment>,
thunderbirdGetDownloadableAttachment: () =>
BrowserMsg.sendAwait(undefined, 'thunderbirdGetDownloadableAttachment', undefined, true) as Promise<Bm.Res.ThunderbirdGetDownloadableAttachment>,
thunderbirdInitiateAttachmentDownload: (bm: Bm.ThunderbirdInitiateAttachmentDownload) =>
BrowserMsg.sendAwait(undefined, 'thunderbirdInitiateAttachmentDownload', bm, true) as Promise<Bm.Res.ThunderbirdInitiateAttachmentDownload>,
thunderbirdGetCurrentUser: () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
};
Expand Down
17 changes: 8 additions & 9 deletions extension/js/service_worker/bg-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,22 @@ export class BgHandlers {
});
};

public static thunderbirdGetDownloadableAttachment = async (
r: Bm.ThunderbirdGetDownloadableAttachment
): Promise<Bm.Res.ThunderbirdGetDownloadableAttachment> => {
public static thunderbirdGetDownloadableAttachment = async (): Promise<Bm.Res.ThunderbirdGetDownloadableAttachment> => {
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,
})
);
}
Expand Down

0 comments on commit 67522cf

Please sign in to comment.