Bug 1520367 - Accounts API: mark special folders with their type; r=Fallen DONTBUILD
authorGeoff Lankow <geoff@darktrojan.net>
Wed, 16 Jan 2019 22:51:46 +1300
changeset 34219 23ef8a434c94ec580fa8b0bed26da070a655afe0
parent 34218 8e0c009b6d376f1026025819487e1b1c03cab598
child 34220 d5f007cf64f76756b37843e584a6bf68aa5233fa
push id389
push userclokep@gmail.com
push dateMon, 18 Mar 2019 19:01:53 +0000
reviewersFallen
bugs1520367
Bug 1520367 - Accounts API: mark special folders with their type; r=Fallen DONTBUILD
mail/components/extensions/parent/ext-mail.js
mail/components/extensions/schemas/accounts.json
mail/components/extensions/test/xpcshell/test_ext_accounts.js
--- a/mail/components/extensions/parent/ext-mail.js
+++ b/mail/components/extensions/parent/ext-mail.js
@@ -1037,34 +1037,54 @@ function folderURIToPath(uri) {
 function folderPathToURI(accountId, path) {
   let rootURI = MailServices.accounts.getAccount(accountId).incomingServer.rootFolder.URI;
   if (path == "/") {
     return rootURI;
   }
   return rootURI + path.split("/").map(encodeURIComponent).join("/");
 }
 
+const folderTypeMap = new Map([
+  [Ci.nsMsgFolderFlags.Inbox, "inbox"],
+  [Ci.nsMsgFolderFlags.Drafts, "drafts"],
+  [Ci.nsMsgFolderFlags.SentMail, "sent"],
+  [Ci.nsMsgFolderFlags.Trash, "trash"],
+  [Ci.nsMsgFolderFlags.Templates, "templates"],
+  [Ci.nsMsgFolderFlags.Archive, "archives"],
+  [Ci.nsMsgFolderFlags.Junk, "junk"],
+  [Ci.nsMsgFolderFlags.Queue, "outbox"],
+]);
+
 /**
  * Converts an nsIMsgFolder to a simple object for use in messages.
  * @return {Object}
  */
 function convertFolder(folder, accountId) {
   if (!folder) {
     return null;
   }
   if (!accountId) {
     let server = folder.server;
     let account = MailServices.accounts.FindAccountForServer(server);
     accountId = account.key;
   }
-  return {
+
+  let folderObject = {
     accountId,
     name: folder.prettyName,
     path: folderURIToPath(folder.URI),
   };
+
+  for (let [flag, typeName] of folderTypeMap.entries()) {
+    if (folder.flags & flag) {
+      folderObject.type = typeName;
+    }
+  }
+
+  return folderObject;
 }
 
 /**
  * Converts an nsIMsgHdr to a simle object for use in messages.
  * This function WILL change as the API develops.
  * @return {Object}
  */
 function convertMessage(msgHdr, context) {
--- a/mail/components/extensions/schemas/accounts.json
+++ b/mail/components/extensions/schemas/accounts.json
@@ -33,16 +33,22 @@
           "name": {
             "type": "string",
             "optional": true,
             "description": "The human-friendly name of this folder."
           },
           "path": {
             "type": "string",
             "description": "Path to this folder in the account. Although paths look predictable, never guess a folder's path, as there are a number of reasons why it may not be what you think it is."
+          },
+          "type": {
+            "type": "string",
+            "optional": true,
+            "description": "The type of folder, for several common types.",
+            "enum": ["inbox", "drafts", "sent", "trash", "templates", "archives", "junk", "outbox"]
           }
         }
       }
     ],
     "functions": [
       {
         "name": "list",
         "type": "function",
--- a/mail/components/extensions/test/xpcshell/test_ext_accounts.js
+++ b/mail/components/extensions/test/xpcshell/test_ext_accounts.js
@@ -57,64 +57,69 @@ add_task(async function test_accounts() 
       assertDeepEqual({
         id: account1Id,
         name: "Local Folders",
         type: "none",
         folders: [{
           accountId: account1Id,
           name: "Trash",
           path: "/Trash",
+          type: "trash",
         }, {
           accountId: account1Id,
           name: "Outbox",
           path: "/Unsent Messages",
+          type: "outbox",
         }],
       }, result1[0]);
 
       let [account2Id] = await awaitMessage("create account 2");
       let result2 = await browser.accounts.list();
       browser.test.assertEq(2, result2.length);
       assertDeepEqual(result1[0], result2[0]);
       assertDeepEqual({
         id: account2Id,
         name: "Mail for username@hostname",
         type: "imap",
         folders: [{
           accountId: account2Id,
           name: "Inbox",
           path: "/INBOX",
+          type: "inbox",
         }],
       }, result2[1]);
 
       let result3 = await browser.accounts.get(account1Id);
       assertDeepEqual(result1[0], result3);
       let result4 = await browser.accounts.get(account2Id);
       assertDeepEqual(result2[1], result4);
 
       await awaitMessage("create folders");
       let result5 = await browser.accounts.get(account1Id);
       let platformInfo = await browser.runtime.getPlatformInfo();
       assertDeepEqual([{
           accountId: account1Id,
           name: "Trash",
           path: "/Trash",
+          type: "trash",
         }, {
           accountId: account1Id,
           name: "foo bar",
           path: "/Trash/foo bar",
         }, {
           accountId: account1Id,
           name: "Ϟ",
           // This character is not supported on Windows, so it gets hashed,
           // by NS_MsgHashIfNecessary.
           path: platformInfo.os == "win" ? "/Trash/b52bc214" : "/Trash/Ϟ",
         }, {
           accountId: account1Id,
           name: "Outbox",
           path: "/Unsent Messages",
+          type: "outbox",
         }], result5.folders);
 
       browser.test.notifyPass("finished");
     },
     manifest: {
       permissions: ["accountsRead"],
     },
   });