Bug 1529785 part 2 - Add WebExtension function to archive messages; r=mkmelin
authorGeoff Lankow <geoff@darktrojan.net>
Wed, 10 Apr 2019 16:17:27 +1200
changeset 26356 ffedede7fa10
parent 26355 1b5861b5c09a
child 26357 3f1eb0d82f7c
push id15798
push usergeoff@darktrojan.net
push dateTue, 16 Apr 2019 04:15:07 +0000
treeherdercomm-central@ffedede7fa10 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkmelin
bugs1529785
Bug 1529785 part 2 - Add WebExtension function to archive messages; r=mkmelin
mail/components/extensions/parent/ext-messages.js
mail/components/extensions/schemas/messages.json
mail/components/extensions/test/xpcshell/test_ext_messages.js
--- a/mail/components/extensions/parent/ext-messages.js
+++ b/mail/components/extensions/parent/ext-messages.js
@@ -1,13 +1,14 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 ChromeUtils.defineModuleGetter(this, "MailServices", "resource:///modules/MailServices.jsm");
+ChromeUtils.defineModuleGetter(this, "MessageArchiver", "resource:///modules/MessageArchiver.jsm");
 ChromeUtils.defineModuleGetter(this, "MsgHdrToMimeMessage", "resource:///modules/gloda/mimemsg.js");
 ChromeUtils.defineModuleGetter(this, "toXPCOMArray", "resource:///modules/iteratorUtils.jsm");
 
 var { DefaultMap } = ExtensionUtils;
 
 /**
  * Takes a part of a MIME message (as retrieved with MsgHdrToMimeMessage) and filters
  * out the properties we don't want to send to extensions.
@@ -168,16 +169,32 @@ this.messages = class extends ExtensionA
           }
           try {
             await Promise.all(promises);
           } catch (ex) {
             Cu.reportError(ex);
             throw new ExtensionError(`Unexpected error deleting messages: ${ex}`);
           }
         },
+        async archive(messageIds) {
+          let messages = [];
+          for (let id of messageIds) {
+            let msgHdr = messageTracker.getMessage(id);
+            if (!msgHdr) {
+              continue;
+            }
+            messages.push(msgHdr);
+          }
+
+          return new Promise((resolve) => {
+            let archiver = new MessageArchiver();
+            archiver.oncomplete = resolve;
+            archiver.archiveMessages(messages);
+          });
+        },
         async listTags() {
           return MailServices.tags.getAllTags({}).map(({ key, tag, color, ordinal }) => {
             return {
               key,
               tag,
               color,
               ordinal,
             };
--- a/mail/components/extensions/schemas/messages.json
+++ b/mail/components/extensions/schemas/messages.json
@@ -179,16 +179,36 @@
             "name": "skipTrash",
             "type": "boolean",
             "description": "If true, the message will be permanently deleted without warning the user. If false or not specified, it will be moved to the trash folder.",
             "optional": true
           }
         ]
       },
       {
+        "name": "archive",
+        "type": "function",
+        "description": "Archives messages using the current settings.",
+        "async": true,
+        "permissions": [
+          "messagesMove"
+        ],
+        "parameters": [
+          {
+            "name": "messageIds",
+            "type": "array",
+            "description": "The IDs of the messages to archive.",
+            "items": {
+              "type": "integer",
+              "minimum": 1
+            }
+          }
+        ]
+      },
+      {
         "name": "listTags",
         "type": "function",
         "description": "Returns a list of tags that can be set on messages, and their human-friendly name, colour, and sort order.",
         "async": true,
         "parameters": []
       }
     ]
   }
--- a/mail/components/extensions/test/xpcshell/test_ext_messages.js
+++ b/mail/components/extensions/test/xpcshell/test_ext_messages.js
@@ -319,9 +319,79 @@ add_task(async function test_move_copy_d
   Services.prefs.setIntPref("extensions.webextensions.messagesPerPage", 1000);
 
   await extension.startup();
   extension.sendMessage(account.key);
   await extension.awaitFinish("finished");
   await extension.unload();
 
   Services.prefs.clearUserPref("extensions.webextensions.messagesPerPage");
+  cleanUpAccount(account);
 });
+
+add_task(async function test_archive() {
+  let account2 = createAccount();
+  account2.addIdentity(MailServices.accounts.createIdentity());
+
+  let rootFolder2 = account2.incomingServer.rootFolder;
+  rootFolder2.createSubfolder("test", null);
+  let inbox2 = [...rootFolder2.subFolders][2];
+  createMessages(inbox2, 15);
+
+  let month = 10;
+  for (let message of inbox2.messages) {
+    message.date = new Date(2018, month++, 15) * 1000;
+  }
+
+  let extension = ExtensionTestUtils.loadExtension({
+    async background() {
+      function awaitMessage() {
+        return new Promise(resolve => {
+          browser.test.onMessage.addListener(function listener(...args) {
+            browser.test.onMessage.removeListener(listener);
+            resolve(args);
+          });
+        });
+      }
+
+      let [accountId] = await awaitMessage();
+
+      let accountBefore = await browser.accounts.get(accountId);
+      browser.test.assertEq(3, accountBefore.folders.length);
+      browser.test.assertEq("/test", accountBefore.folders[2].path);
+
+      let messagesBefore = await browser.messages.list(accountBefore.folders[2]);
+      await browser.messages.archive(messagesBefore.messages.map(m => m.id));
+
+      let accountAfter = await browser.accounts.get(accountId);
+      browser.test.assertEq(7, accountAfter.folders.length);
+      browser.test.assertEq("/test", accountAfter.folders[2].path);
+      browser.test.assertEq("/Archives", accountAfter.folders[3].path);
+      browser.test.assertEq("/Archives/2018", accountAfter.folders[4].path);
+      browser.test.assertEq("/Archives/2019", accountAfter.folders[5].path);
+      browser.test.assertEq("/Archives/2020", accountAfter.folders[6].path);
+
+      let messagesAfter = await browser.messages.list(accountAfter.folders[2]);
+      browser.test.assertEq(0, messagesAfter.messages.length);
+
+      let messages2018 = await browser.messages.list(accountAfter.folders[4]);
+      browser.test.assertEq(2, messages2018.messages.length);
+
+      let messages2019 = await browser.messages.list(accountAfter.folders[5]);
+      browser.test.assertEq(12, messages2019.messages.length);
+
+      let messages2020 = await browser.messages.list(accountAfter.folders[6]);
+      browser.test.assertEq(1, messages2020.messages.length);
+
+      browser.test.notifyPass("finished");
+    },
+    manifest: {
+      permissions: ["accountsRead", "messagesMove", "messagesRead"],
+    },
+  });
+
+  await extension.startup();
+  extension.sendMessage(account2.key);
+  await extension.awaitFinish("finished");
+  await extension.unload();
+
+  cleanUpAccount(account2);
+});