Skip to content

Commit

Permalink
chore: Replace experimental API functions with APIs that Thunderbird …
Browse files Browse the repository at this point in the history
…now supplies.
  • Loading branch information
Standard8 committed Sep 25, 2024
1 parent 66e2d66 commit 2c29407
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 109 deletions.
23 changes: 14 additions & 9 deletions addon/content/esmodules/thunderbirdCompat.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ if (!browser.conversations) {
async makeFriendlyDateAgo() {
return "yesterday";
},
async formatFileSize(size) {
return `${size} bars`;
},
async makePlural(form, string, count) {
return `${string} ${count}`;
},
Expand All @@ -247,12 +244,6 @@ if (!browser.conversations) {
async getLateAttachments() {
return [];
},
async parseMimeLine() {
return [{ email: "", name: "-", fullName: "-" }];
},
async convertSnippetToPlainText() {
return "short snippet";
},
async getReplyOnTop() {
return 1;
},
Expand Down Expand Up @@ -661,4 +652,18 @@ if (!browser.convOpenPgp) {
};
}

if (!browser.messengerUtilities) {
browser.messengerUtilities = {
async convertToPlainText() {
return "short snippet";
},
async formatFileSize(size) {
return `${size} bars`;
},
async parseMailboxString() {
return [{ email: "", name: "-" }];
},
};
}

export { browser };
1 change: 1 addition & 0 deletions addon/content/modules/browserSim.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const SUPPORTED_APIS_NO_EVENTS = [
// MUST be tested very carefully. Last time this was tried, it would end up
// clearing the starred flag when marking a message as read.
"messages",
"messengerUtilities",
"windows",
// This is a temporary workaround so that we can "message" the background script.
"_background",
Expand Down
27 changes: 16 additions & 11 deletions addon/content/reducer/messageEnricher.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ export class MessageEnricher {
msg
);
if (message.realFrom) {
let real = await browser.conversations.parseMimeLine(message.realFrom);
let real = await browser.messengerUtilities.parseMailboxString(
message.realFrom
);
msg.realFrom = real?.[0].email;
}

Expand Down Expand Up @@ -418,13 +420,14 @@ export class MessageEnricher {

if ("x-bugzilla-who" in fullMsg.headers) {
msg.realFrom = msg.parsedLines.from[0]?.email;
msg.parsedLines.from = await browser.conversations.parseMimeLine(
fullMsg.headers["x-bugzilla-who"][0]
);
msg.parsedLines.from =
await browser.messengerUtilities.parseMailboxString(
fullMsg.headers["x-bugzilla-who"][0]
);
}

if ("reply-to" in fullMsg.headers) {
msg.replyTo = await browser.conversations.parseMimeLine(
msg.replyTo = await browser.messengerUtilities.parseMailboxString(
fullMsg.headers["reply-to"][0]
);
}
Expand Down Expand Up @@ -459,9 +462,7 @@ export class MessageEnricher {

let info = checkPart(fullMsg.parts[0]);
if (info.html && info.body) {
msg.snippet = await browser.conversations.convertSnippetToPlainText(
msg.folderAccountId,
msg.folderPath,
msg.snippet = await browser.messengerUtilities.convertToPlainText(
info.body
);
} else {
Expand Down Expand Up @@ -507,7 +508,7 @@ export class MessageEnricher {
async _parseContactLines(contactData, msg) {
msg.parsedLines = {
from: contactData.from
? await browser.conversations.parseMimeLine(contactData.from)
? await browser.messengerUtilities.parseMailboxString(contactData.from)
: [],
};

Expand All @@ -524,7 +525,9 @@ export class MessageEnricher {
continue;
}
for (let i of item) {
let data = i ? await browser.conversations.parseMimeLine(i) : [];
let data = i
? await browser.messengerUtilities.parseMailboxString(i)
: [];
msg.parsedLines[line] = msg.parsedLines[line].concat(data);
}
}
Expand Down Expand Up @@ -568,7 +571,9 @@ export class MessageEnricher {
let formattedSize = this.sizeUnknownString;
// -1 means size unknown
if (att.size != -1) {
formattedSize = await browser.conversations.formatFileSize(att.size);
formattedSize = await browser.messengerUtilities.formatFileSize(
att.size
);
}

// We've got the right data, push it!
Expand Down
2 changes: 1 addition & 1 deletion addon/content/reducer/reducerMessages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const messageActions = {
// -1 means size unknown
let formattedSize = unknown;
if (attachments[i].size != -1) {
formattedSize = await browser.conversations.formatFileSize(
formattedSize = await browser.messengerUtilities.formatFileSize(
attachments[i].size
);
}
Expand Down
33 changes: 0 additions & 33 deletions addon/experiment-api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ var conversations = class extends ExtensionCommon.ExtensionAPI {
}
return msgHdr.folder.getUriForMsg(msgHdr);
},
async formatFileSize(size) {
return messenger.formatFileSize(size);
},
async createTab(createTabProperties) {
const params = {
url: createTabProperties.url,
Expand Down Expand Up @@ -671,36 +668,6 @@ var conversations = class extends ExtensionCommon.ExtensionAPI {
})
);
},
/**
* Wraps the low-level header parser stuff.
*
* @param {string} mimeLine
* A line that looks like "John <[email protected]>, Jane <[email protected]>"
* @returns {Array}
* A list of { email, name, fullName } objects
*/
parseMimeLine(mimeLine) {
if (mimeLine == null) {
console.warn("Empty aMimeLine?!!");
return [{ email: "", name: "-", fullName: "-" }];
}
let addresses =
lazy.MailServices.headerParser.parseDecodedHeader(mimeLine);
if (addresses.length) {
return addresses.map((addr) => {
return {
email: addr.email,
name: addr.name,
fullName: addr.toString(),
};
});
}
return [{ email: "", name: "-", fullName: "-" }];
},
convertSnippetToPlainText(accountId, path, text) {
let msgFolder = context.extension.folderManager.get(accountId, path);
return msgFolder.convertMsgSnippetToPlainText(text);
},
async getAccountOfflineDownload(accountId) {
let account = lazy.MailServices.accounts.getAccount(accountId);
return account?.incomingServer.QueryInterface(
Expand Down
49 changes: 0 additions & 49 deletions addon/experiment-api/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,6 @@
}
]
},
{
"name": "formatFileSize",
"type": "function",
"description": "Temporary. Formats the file size (xref bug 1606575).",
"async": true,
"parameters": [
{
"name": "size",
"type": "integer",
"description": "The size to format"
}
]
},
{
"name": "createTab",
"type": "function",
Expand Down Expand Up @@ -562,19 +549,6 @@
}
]
},
{
"name": "parseMimeLine",
"type": "function",
"description": "Extracts contact details from a mime line.",
"async": true,
"parameters": [
{
"name": "mimeLine",
"type": "string",
"description": "The line to parse for contact details."
}
]
},
{
"name": "getFolderOfflineDownload",
"type": "function",
Expand All @@ -593,29 +567,6 @@
}
]
},
{
"name": "convertSnippetToPlainText",
"type": "function",
"description": "Converts a message snippet to plain text.",
"async": true,
"parameters": [
{
"name": "accountId",
"type": "string",
"description": "The account id"
},
{
"name": "path",
"type": "string",
"description": "The path of the folder"
},
{
"name": "text",
"type": "string",
"description": "The text to convert"
}
]
},
{
"name": "setFolderOfflineDownload",
"type": "function",
Expand Down
2 changes: 1 addition & 1 deletion addon/gallery/gallery.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class MyComponent extends React.Component {
async output(attachments, id, scrollToPartName) {
// Get the initial data first.
for (const attachment of attachments) {
attachment.size = await browser.conversations.formatFileSize(
attachment.size = await browser.messengerUtilities.formatFileSize(
attachment.size
);
}
Expand Down
7 changes: 2 additions & 5 deletions addon/tests/messages.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,11 @@ describe("messageEnricher", () => {
};
});
t.mock
.method(browser.conversations, "parseMimeLine")
.method(browser.messengerUtilities, "parseMailboxString")
.mock.mockImplementation((line) => [
{
email: line,
name: "-",
fullName: "-",
},
]);

Expand Down Expand Up @@ -595,12 +594,11 @@ describe("messageEnricher", () => {
};
});
t.mock
.method(browser.conversations, "parseMimeLine")
.method(browser.messengerUtilities, "parseMailboxString")
.mock.mockImplementation((line) => [
{
email: line,
name: "-",
fullName: "-",
},
]);

Expand All @@ -622,7 +620,6 @@ describe("messageEnricher", () => {
{
email: "[email protected]",
name: "-",
fullName: "-",
},
]);
});
Expand Down

0 comments on commit 2c29407

Please sign in to comment.