Bug 1531317 - Message search WebExtension API. r=mkmelin
authorMarvin Herrmann <hermar05@web.de>
Fri, 21 Jun 2019 19:40:41 +1200
changeset 35933 14390389783d5a46cca22ceafa2ae777028159fa
parent 35932 f8de60138717a74dfb72395da77f38bf926bb8ac
child 35934 450a66aeeaa8bd80ee95808f2e4d1a274c7bcb14
push id392
push userclokep@gmail.com
push dateMon, 02 Sep 2019 20:17:19 +0000
reviewersmkmelin
bugs1531317
Bug 1531317 - Message search WebExtension API. r=mkmelin
mail/components/extensions/parent/ext-messages.js
mail/components/extensions/schemas/messages.json
--- a/mail/components/extensions/parent/ext-messages.js
+++ b/mail/components/extensions/parent/ext-messages.js
@@ -1,12 +1,13 @@
 /* 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, "Gloda", "resource:///modules/gloda/public.js");
 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;
 
 /**
@@ -104,16 +105,69 @@ this.messages = class extends ExtensionA
         async getFull(messageId) {
           return new Promise(resolve => {
             let msgHdr = messageTracker.getMessage(messageId);
             MsgHdrToMimeMessage(msgHdr, null, (_msgHdr, mimeMsg) => {
               resolve(convertMessagePart(mimeMsg));
             });
           });
         },
+        async query(queryInfo) {
+          let query = Gloda.newQuery(Gloda.NOUN_MESSAGE);
+
+          if (queryInfo.subject) {
+            query.subjectMatches(queryInfo.subject);
+          }
+          if (queryInfo.fullText) {
+            query.fulltextMatches(queryInfo.fullText);
+          }
+          if (queryInfo.body) {
+            query.bodyMatches(queryInfo.body);
+          }
+          if (queryInfo.author) {
+            query.authorMatches(queryInfo.author);
+          }
+          if (queryInfo.recipients) {
+            query.recipientsMatch(queryInfo.recipients);
+          }
+          if (queryInfo.fromMe) {
+            query.fromMe();
+          }
+          if (queryInfo.toMe) {
+            query.toMe();
+          }
+          if (queryInfo.flagged !== null) {
+            query.starred(queryInfo.flagged);
+          }
+          if (queryInfo.folder) {
+            let folder = MailServices.folderLookup.getFolderForURL(
+              folderPathToURI(queryInfo.folder.accountId, queryInfo.folder.path)
+            );
+            query.folder(folder);
+          }
+          if (queryInfo.fromDate || queryInfo.toDate) {
+              query.dateRange([queryInfo.fromDate, queryInfo.toDate]);
+          }
+
+          let collectionArray = await new Promise((resolve) => {
+            query.getCollection({
+              onItemsAdded(items, collection) {
+              },
+              onItemsModified(items, collection) {
+              },
+              onItemsRemoved(items, collection) {
+              },
+              onQueryCompleted(collection) {
+                resolve(collection.items.map(glodaMsg => glodaMsg.folderMessage));
+              },
+            });
+          });
+
+          return messageListTracker.startList(collectionArray, context.extension);
+        },
         async update(messageId, newProperties) {
           let msgHdr = messageTracker.getMessage(messageId);
           if (!msgHdr) {
             return;
           }
           if (newProperties.read !== null) {
             msgHdr.markRead(newProperties.read);
           }
--- a/mail/components/extensions/schemas/messages.json
+++ b/mail/components/extensions/schemas/messages.json
@@ -66,16 +66,85 @@
         "parameters": [
           {
             "name": "messageId",
             "type": "integer"
           }
         ]
       },
       {
+        "name": "query",
+        "type": "function",
+        "description": "Gets all messages that have the specified properties, or all messages if no properties are specified.",
+        "async": true,
+        "parameters": [
+          {
+            "type": "object",
+            "name": "queryInfo",
+            "properties": {
+              "subject": {
+                "type": "string",
+                "optional": true,
+                "description": "Returns only messages with this value matching the subject."
+              },
+              "fullText": {
+                "type": "string",
+                "optional": true,
+                "description": "Returns only messages with this value somewhere in the mail (subject, body or author)."
+              },
+              "body": {
+                "type": "string",
+                "optional": true,
+                "description": "Returns only messages with this value in the body of the mail."
+              },
+              "author": {
+                "type": "string",
+                "optional": true,
+                "description": "Returns only messages with this value matching the author."
+              },
+              "recipients": {
+                "type": "string",
+                "optional": true,
+                "description": "Returns only messages with this value matching one or more recipients."
+              },
+              "fromMe": {
+                "type": "boolean",
+                "optional": true,
+                "description": "Returns only messages with the author matching any configured identity."
+              },
+              "toMe": {
+                "type": "boolean",
+                "optional": true,
+                "description": "Returns only messages with one or more recipients matching any configured identity."
+              },
+              "flagged": {
+                "type": "boolean",
+                "optional": true,
+                "description": "Returns only flagged (or unflagged if false) messages."
+              },
+              "folder": {
+                "$ref": "folders.MailFolder",
+                "optional": true,
+                "description": "Returns only messages from the specified folder."
+              },
+              "fromDate": {
+                "$ref": "extensionTypes.Date",
+                "optional": true,
+                "description": "Returns only messages with a date after this value."
+              },
+              "toDate": {
+                "$ref": "extensionTypes.Date",
+                "optional": true,
+                "description": "Returns only messages with a date before this value."
+              }
+            }
+          }
+        ]
+      },
+      {
         "name": "update",
         "type": "function",
         "description": "Marks or unmarks a message as read, flagged, or tagged.",
         "async": true,
         "parameters": [
           {
             "name": "messageId",
             "type": "integer",