From e39c99993260b4d097d3267a6e5411b625bb036c Mon Sep 17 00:00:00 2001
From: Scott Nonnenberg <scott@signal.org>
Date: Fri, 26 Oct 2018 10:52:38 -0700
Subject: [PATCH 1/5] Import: Properly handle import with all empty non-convo
 stores

---
 js/modules/backup.js | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/js/modules/backup.js b/js/modules/backup.js
index cb2a50d88..b33f87490 100644
--- a/js/modules/backup.js
+++ b/js/modules/backup.js
@@ -346,11 +346,6 @@ async function importFromJsonString(db, jsonString, targetPath, options) {
     _.each(remainingStoreNames, storeName => {
       window.log.info('Importing items for store', storeName);
 
-      if (!importObject[storeName].length) {
-        delete importObject[storeName];
-        return;
-      }
-
       let count = 0;
       let skipCount = 0;
 
@@ -372,6 +367,10 @@ async function importFromJsonString(db, jsonString, targetPath, options) {
         }
       };
 
+      if (!importObject[storeName].length) {
+        finishStore();
+      }
+
       _.each(importObject[storeName], toAdd => {
         toAdd = unstringify(toAdd);
 
@@ -402,7 +401,7 @@ async function importFromJsonString(db, jsonString, targetPath, options) {
 
       // We have to check here, because we may have skipped every item, resulting
       //   in no onsuccess callback at all.
-      if (count === importObject[storeName].length) {
+      if (skipCount === count) {
         finishStore();
       }
     });

From 2060118e85223d3f3b8f38e7679e3fdf7553d295 Mon Sep 17 00:00:00 2001
From: Scott Nonnenberg <scott@signal.org>
Date: Thu, 25 Oct 2018 15:44:59 -0700
Subject: [PATCH 2/5] Remove messages/conversations/unprocessed IndexedDB
 object stores

---
 js/background.js                              | 22 ++++++++++++-
 ...ions_1_database_without_attachment_data.js | 32 +++++++++++++------
 2 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/js/background.js b/js/background.js
index 714af3f24..5dcede75f 100644
--- a/js/background.js
+++ b/js/background.js
@@ -131,7 +131,10 @@
     deleteAttachmentData,
     getCurrentVersion,
   } = window.Signal.Migrations;
-  const { Migrations0DatabaseWithAttachmentData } = window.Signal.Migrations;
+  const {
+    Migrations0DatabaseWithAttachmentData,
+    Migrations1DatabaseWithoutAttachmentData,
+  } = window.Signal.Migrations;
   const { Views } = window.Signal;
 
   // Implicitly used in `indexeddb-backbonejs-adapter`:
@@ -388,6 +391,23 @@
 
     Views.Initialization.setMessage(window.i18n('optimizingApplication'));
 
+    window.log.info('Running cleanup IndexedDB migrations...');
+    // Close all previous connections to the database first
+    await Whisper.Database.close();
+
+    // Now we clean up IndexedDB database after extracting data from it
+    await Migrations1DatabaseWithoutAttachmentData.run({
+      Backbone,
+      logger: window.log,
+    });
+
+    await Whisper.Database.close();
+
+    const latestDBVersion = _.last(
+      Migrations1DatabaseWithoutAttachmentData.migrations
+    ).version;
+    Whisper.Database.migrations[0].version = latestDBVersion;
+
     window.log.info('Cleanup: starting...');
     const messagesForCleanup = await window.Signal.Data.getOutgoingWithoutExpiresAt(
       {
diff --git a/js/modules/migrations/migrations_1_database_without_attachment_data.js b/js/modules/migrations/migrations_1_database_without_attachment_data.js
index 5377a67a8..2f2154201 100644
--- a/js/modules/migrations/migrations_1_database_without_attachment_data.js
+++ b/js/modules/migrations/migrations_1_database_without_attachment_data.js
@@ -1,25 +1,37 @@
 /* global window */
 
-const { last } = require('lodash');
+const { last, includes } = require('lodash');
 
-const db = require('../database');
+const { open } = require('../database');
 const settings = require('../settings');
 const { runMigrations } = require('./run_migrations');
 
-// These are migrations for after the SQLCipher migration, currently not running
+// These are cleanup migrations, to be run after migration to SQLCipher
 exports.migrations = [
   {
     version: 20,
     migrate(transaction, next) {
       window.log.info('Migration 20');
-      window.log.info(
-        'Removing messages, unprocessed, and conversations object stores'
-      );
+
+      const { db } = transaction;
 
       // This should be run after things are migrated to SQLCipher
-      transaction.db.deleteObjectStore('messages');
-      transaction.db.deleteObjectStore('unprocessed');
-      transaction.db.deleteObjectStore('conversations');
+
+      // We check for existence first, because this removal was present in v1.17.0.beta.1,
+      //  but reverted in v1.17.0-beta.3
+
+      if (includes(db.objectStoreNames, 'messages')) {
+        window.log.info('Removing messages store');
+        db.deleteObjectStore('messages');
+      }
+      if (includes(db.objectStoreNames, 'unprocessed')) {
+        window.log.info('Removing unprocessed store');
+        db.deleteObjectStore('unprocessed');
+      }
+      if (includes(db.objectStoreNames, 'conversations')) {
+        window.log.info('Removing conversations store');
+        db.deleteObjectStore('conversations');
+      }
 
       next();
     },
@@ -48,7 +60,7 @@ exports.run = async ({ Backbone, logger } = {}) => {
 };
 
 exports.getStatus = async ({ database } = {}) => {
-  const connection = await db.open(database.id, database.version);
+  const connection = await open(database.id, database.version);
   const isAttachmentMigrationComplete = await settings.isAttachmentMigrationComplete(
     connection
   );

From 739bd4dbda82a1ce18c7aaf790a1f81dfb47188b Mon Sep 17 00:00:00 2001
From: Scott Nonnenberg <scott@signal.org>
Date: Fri, 26 Oct 2018 10:59:52 -0700
Subject: [PATCH 3/5] Request contact sync only on the first connect after
 upgrade

---
 js/background.js | 1 +
 1 file changed, 1 insertion(+)

diff --git a/js/background.js b/js/background.js
index 5dcede75f..9536028ba 100644
--- a/js/background.js
+++ b/js/background.js
@@ -747,6 +747,7 @@
     //   (but only if we're not the primary device)
     if (
       !firstRun &&
+      connectCount === 1 &&
       newVersion &&
       // eslint-disable-next-line eqeqeq
       textsecure.storage.user.getDeviceId() != '1'

From 6656469f5f0838826b0823d9b05b436fdae22301 Mon Sep 17 00:00:00 2001
From: Scott Nonnenberg <scott@signal.org>
Date: Fri, 26 Oct 2018 15:14:43 -0700
Subject: [PATCH 4/5] Update lint exceptions

---
 ts/util/lint/exceptions.json | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/ts/util/lint/exceptions.json b/ts/util/lint/exceptions.json
index e645ddda3..1ba85f16e 100644
--- a/ts/util/lint/exceptions.json
+++ b/ts/util/lint/exceptions.json
@@ -164,7 +164,7 @@
     "rule": "jQuery-$(",
     "path": "js/background.js",
     "line": "        if ($('.dark-overlay').length) {",
-    "lineNumber": 261,
+    "lineNumber": 264,
     "reasonCategory": "usageTrusted",
     "updated": "2018-09-19T21:59:32.770Z",
     "reasonDetail": "Protected from arbitrary input"
@@ -173,7 +173,7 @@
     "rule": "jQuery-$(",
     "path": "js/background.js",
     "line": "        $(document.body).prepend('<div class=\"dark-overlay\"></div>');",
-    "lineNumber": 264,
+    "lineNumber": 267,
     "reasonCategory": "usageTrusted",
     "updated": "2018-09-19T21:59:32.770Z",
     "reasonDetail": "Protected from arbitrary input"
@@ -182,7 +182,7 @@
     "rule": "jQuery-prepend(",
     "path": "js/background.js",
     "line": "        $(document.body).prepend('<div class=\"dark-overlay\"></div>');",
-    "lineNumber": 264,
+    "lineNumber": 267,
     "reasonCategory": "usageTrusted",
     "updated": "2018-09-19T18:13:29.628Z",
     "reasonDetail": "Hard-coded value"
@@ -191,7 +191,7 @@
     "rule": "jQuery-$(",
     "path": "js/background.js",
     "line": "        $('.dark-overlay').on('click', () => $('.dark-overlay').remove());",
-    "lineNumber": 265,
+    "lineNumber": 268,
     "reasonCategory": "usageTrusted",
     "updated": "2018-09-19T21:59:32.770Z",
     "reasonDetail": "Protected from arbitrary input"
@@ -200,7 +200,7 @@
     "rule": "jQuery-$(",
     "path": "js/background.js",
     "line": "      removeDarkOverlay: () => $('.dark-overlay').remove(),",
-    "lineNumber": 267,
+    "lineNumber": 270,
     "reasonCategory": "usageTrusted",
     "updated": "2018-09-19T21:59:32.770Z",
     "reasonDetail": "Protected from arbitrary input"
@@ -209,7 +209,7 @@
     "rule": "jQuery-$(",
     "path": "js/background.js",
     "line": "        $('body').append(clearDataView.el);",
-    "lineNumber": 270,
+    "lineNumber": 273,
     "reasonCategory": "usageTrusted",
     "updated": "2018-09-19T21:59:32.770Z",
     "reasonDetail": "Protected from arbitrary input"
@@ -218,7 +218,7 @@
     "rule": "jQuery-append(",
     "path": "js/background.js",
     "line": "        $('body').append(clearDataView.el);",
-    "lineNumber": 270,
+    "lineNumber": 273,
     "reasonCategory": "usageTrusted",
     "updated": "2018-09-19T18:13:29.628Z",
     "reasonDetail": "Interacting with already-existing DOM nodes"
@@ -227,7 +227,7 @@
     "rule": "jQuery-load(",
     "path": "js/background.js",
     "line": "      await ConversationController.load();",
-    "lineNumber": 488,
+    "lineNumber": 508,
     "reasonCategory": "falseMatch",
     "updated": "2018-10-02T21:00:44.007Z"
   },
@@ -235,7 +235,7 @@
     "rule": "jQuery-$(",
     "path": "js/background.js",
     "line": "      el: $('body'),",
-    "lineNumber": 542,
+    "lineNumber": 562,
     "reasonCategory": "usageTrusted",
     "updated": "2018-09-19T21:59:32.770Z",
     "reasonDetail": "Protected from arbitrary input"
@@ -244,7 +244,7 @@
     "rule": "jQuery-wrap(",
     "path": "js/background.js",
     "line": "        const profileKey = dcodeIO.ByteBuffer.wrap(details.profileKey).toString(",
-    "lineNumber": 863,
+    "lineNumber": 884,
     "reasonCategory": "falseMatch",
     "updated": "2018-10-02T21:00:44.007Z"
   },
@@ -6801,4 +6801,4 @@
     "updated": "2018-09-17T20:50:40.689Z",
     "reasonDetail": "Hard-coded value"
   }
-]
\ No newline at end of file
+]

From 817cf5ed036241897b93d442c8307f7d3442055e Mon Sep 17 00:00:00 2001
From: Scott Nonnenberg <scott@signal.org>
Date: Fri, 26 Oct 2018 15:15:47 -0700
Subject: [PATCH 5/5] v1.17.2-beta.1

---
 package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/package.json b/package.json
index 9c1d57a95..c14a8a7b6 100644
--- a/package.json
+++ b/package.json
@@ -3,7 +3,7 @@
   "productName": "Signal",
   "description": "Private messaging from your desktop",
   "repository": "https://github.com/signalapp/Signal-Desktop.git",
-  "version": "1.17.1-beta.1",
+  "version": "1.17.2-beta.1",
   "license": "GPL-3.0",
   "author": {
     "name": "Open Whisper Systems",