Bug 1522453 - Prevent cards without an email address being added to a mailing list. r=mkmelin a=jorgk
authorGeoff Lankow <geoff@darktrojan.net>
Mon, 11 Feb 2019 14:00:20 +1300
changeset 34347 415d630ef78a7911889452dd8350d92fb6e457c3
parent 34346 89ebd1b371c673db7506a7ab80df43549629f6c2
child 34348 3a5638b42d2d6d93629c8b84fa028e80e2c1deab
push id389
push userclokep@gmail.com
push dateMon, 18 Mar 2019 19:01:53 +0000
reviewersmkmelin, jorgk
bugs1522453
Bug 1522453 - Prevent cards without an email address being added to a mailing list. r=mkmelin a=jorgk
mail/components/compose/content/addressingWidgetOverlay.js
mailnews/addrbook/src/nsAddrDatabase.cpp
mailnews/addrbook/test/unit/test_bug1522453.js
--- a/mail/components/compose/content/addressingWidgetOverlay.js
+++ b/mail/components/compose/content/addressingWidgetOverlay.js
@@ -1,15 +1,17 @@
 /* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  * 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/. */
 
 /* import-globals-from MsgComposeCommands.js */
 
+var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
+
 top.MAX_RECIPIENTS = 1; /* for the initial listitem created in the XUL */
 
 var inputElementType = "";
 var selectElementType = "";
 var selectElementIndexTable = null;
 
 var gNumberOfCols = 0;
 
--- a/mailnews/addrbook/src/nsAddrDatabase.cpp
+++ b/mailnews/addrbook/src/nsAddrDatabase.cpp
@@ -1195,16 +1195,19 @@ NS_IMETHODIMP nsAddrDatabase::CreateNewL
 
   uint32_t count;
   addressList->GetLength(&count);
 
   nsAutoString newEmail;
   rv = newCard->GetPrimaryEmail(newEmail);
   NS_ENSURE_SUCCESS(rv,rv);
 
+  if (newEmail.IsEmpty())
+    return NS_OK;
+
   uint32_t i;
   for (i = 0; i < count; i++) {
     nsCOMPtr<nsIAbCard> currentCard = do_QueryElementAt(addressList, i, &rv);
     NS_ENSURE_SUCCESS(rv,rv);
 
     bool equals;
     rv = newCard->Equals(currentCard, &equals);
     NS_ENSURE_SUCCESS(rv,rv);
--- a/mailnews/addrbook/test/unit/test_bug1522453.js
+++ b/mailnews/addrbook/test/unit/test_bug1522453.js
@@ -43,9 +43,44 @@ function run_test() {
 
   // list.childCards should contain contacts 1 and 3, and crucially, not die at 2.
   let listCards = list.childCards;
   ok(listCards.hasMoreElements());
   equal(contact1.UID, listCards.getNext().QueryInterface(Ci.nsIAbCard).UID);
   ok(listCards.hasMoreElements());
   equal(contact3.UID, listCards.getNext().QueryInterface(Ci.nsIAbCard).UID);
   ok(!listCards.hasMoreElements());
+
+  // list.addressLists should contain contacts 1 and 3.
+  let listEnum = list.addressLists.enumerate();
+  ok(listEnum.hasMoreElements());
+  equal(contact1.UID, listEnum.getNext().QueryInterface(Ci.nsIAbCard).UID);
+  ok(listEnum.hasMoreElements());
+  equal(contact3.UID, listEnum.getNext().QueryInterface(Ci.nsIAbCard).UID);
+  ok(!listEnum.hasMoreElements());
+
+  // Reload the address book manager.
+  Services.obs.notifyObservers(null, "addrbook-reload");
+
+  MailServices.ab.directories;
+  book = MailServices.ab.getDirectory(kPABData.URI);
+
+  // For some unknown reason this is necessary for book.addressLists to be populated.
+  book.QueryInterface(Ci.nsIAbMDBDirectory).database.getMailingListsFromDB(book);
+  equal(1, book.addressLists.Count());
+  list = book.addressLists.GetElementAt(0).QueryInterface(Ci.nsIAbDirectory);
+
+  // list.childCards should contain contacts 1 and 3.
+  listCards = list.childCards;
+  ok(listCards.hasMoreElements());
+  equal(contact1.UID, listCards.getNext().QueryInterface(Ci.nsIAbCard).UID);
+  ok(listCards.hasMoreElements());
+  equal(contact3.UID, listCards.getNext().QueryInterface(Ci.nsIAbCard).UID);
+  ok(!listCards.hasMoreElements());
+
+  // list.addressLists should contain contacts 1 and 3.
+  listEnum = list.addressLists.enumerate();
+  ok(listEnum.hasMoreElements());
+  equal(contact1.UID, listEnum.getNext().QueryInterface(Ci.nsIAbCard).UID);
+  ok(listEnum.hasMoreElements());
+  equal(contact3.UID, listEnum.getNext().QueryInterface(Ci.nsIAbCard).UID);
+  ok(!listEnum.hasMoreElements());
 }