From cf8f0f4d7fc50188513ae1923a3d7367dd2a6142 Mon Sep 17 00:00:00 2001 From: Marvin Herrmann Date: Fri, 21 Jun 2019 19:40:41 +1200 Subject: [PATCH] Bug 1531317 - Message search WebExtension API. r=mkmelin --- .../extensions/parent/ext-messages.js | 54 +++++++++++++++ .../extensions/schemas/messages.json | 69 +++++++++++++++++++ 2 files changed, 123 insertions(+) diff --git a/mail/components/extensions/parent/ext-messages.js b/mail/components/extensions/parent/ext-messages.js index db518446fd..76510e6465 100644 --- a/mail/components/extensions/parent/ext-messages.js +++ b/mail/components/extensions/parent/ext-messages.js @@ -2,6 +2,7 @@ * 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"); @@ -109,6 +110,59 @@ this.messages = class extends ExtensionAPI { }); }); }, + 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) { diff --git a/mail/components/extensions/schemas/messages.json b/mail/components/extensions/schemas/messages.json index 1749ce3372..bf358f0466 100644 --- a/mail/components/extensions/schemas/messages.json +++ b/mail/components/extensions/schemas/messages.json @@ -70,6 +70,75 @@ } ] }, + { + "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",