FIX: Make protocol changes backwards compatible.

FIX: Make protocol changes backwards compatible.

diff --git a/assets/javascripts/discourse/initializers/hook-save.js.es6 b/assets/javascripts/discourse/initializers/hook-save.js.es6
index 34a0183..0d78569 100644
--- a/assets/javascripts/discourse/initializers/hook-save.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-save.js.es6
@@ -78,7 +78,7 @@ export default {
         }
 
         return getTopicKey(topic.id)
-          .then(key => encrypt(key, props.title))
+          .then(key => encrypt(key, { raw: props.title }))
           .then(encryptedTitle => {
             props.title = I18n.t("encrypt.encrypted_topic_title");
             props.encrypted_title = encryptedTitle;
@@ -105,7 +105,7 @@ export default {
 
         let titlePromise = title
           ? topicKey
-              .then(key => encrypt(key, title))
+              .then(key => encrypt(key, { raw: title }))
               .then(encryptedTitle => {
                 args.encrypted_title = encryptedTitle;
                 args.title = I18n.t("encrypt.encrypted_topic_title");
diff --git a/assets/javascripts/lib/discourse.js.es6 b/assets/javascripts/lib/discourse.js.es6
index 1c4c05a..7c04e17 100644
--- a/assets/javascripts/lib/discourse.js.es6
+++ b/assets/javascripts/lib/discourse.js.es6
@@ -167,9 +167,9 @@ export function getTopicTitle(topicId) {
   } else if (
     !(title instanceof Promise || title instanceof Ember.RSVP.Promise)
   ) {
-    topicTitles[topicId] = getTopicKey(topicId).then(key =>
-      decrypt(key, title)
-    );
+    topicTitles[topicId] = getTopicKey(topicId)
+      .then(key => decrypt(key, title))
+      .then(decrypted => decrypted.raw);
   }
 
   return topicTitles[topicId];
diff --git a/db/migrate/20190803111542_update_protocol.rb b/db/migrate/20190803111542_update_protocol.rb
index 939846f..47d73ac 100644
--- a/db/migrate/20190803111542_update_protocol.rb
+++ b/db/migrate/20190803111542_update_protocol.rb
@@ -28,6 +28,21 @@ class UpdateProtocol < ActiveRecord::Migration[5.2]
       DELETE FROM user_custom_fields
       WHERE name IN ('encrypt_public_key', 'encrypt_private_key', 'encrypt_salt')
     SQL
+
+    execute <<~SQL
+      UPDATE topic_custom_fields
+      SET value = '0$' || value
+      WHERE name = 'encrypted_title'
+    SQL
+
+    execute <<~SQL
+      UPDATE posts
+      SET raw = '0$' || raw
+      FROM topic_custom_fields tcf
+      WHERE posts.topic_id = tcf.topic_id AND
+          tcf.name = 'encrypted_title' AND
+          posts.raw ~ '^[A-Za-z0-9+\\\/=$]+(\n.*)?$';
+    SQL
   end
 
   def down
diff --git a/db/migrate/20190916115531_add_paper_keys.rb b/db/migrate/20190916115531_add_paper_keys.rb
new file mode 100644
index 0000000..47b4995
--- /dev/null
+++ b/db/migrate/20190916115531_add_paper_keys.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class AddPaperKeys < ActiveRecord::Migration[5.2]
+  def up
+    execute <<~SQL
+      UPDATE user_custom_fields
+      SET value = '{"passphrase": "' || value || '"}'
+      WHERE name = 'encrypt_private'
+    SQL
+  end
+
+  def down
+    raise ActiveRecord::IrreversibleMigration
+  end
+end

GitHub sha: 846e38e9

1 Like