From 6bb6eafdc06ae9b660cc7166ba0d74c5174e19bd Mon Sep 17 00:00:00 2001
From: Mark Haines <mjark@negativecurvature.net>
Date: Tue, 21 Jul 2015 16:46:17 +0100
Subject: [PATCH] Hook up slash commands to enable and disable encryption for a
 room so that we can experiment with encryption while we wait for the rest of
 the UI to exist

---
 src/SlashCommands.js                    | 22 +++++++++++++++++++++-
 src/controllers/organisms/CreateRoom.js | 23 ++++++++---------------
 2 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/src/SlashCommands.js b/src/SlashCommands.js
index 26a14687d3..2ed6c64acf 100644
--- a/src/SlashCommands.js
+++ b/src/SlashCommands.js
@@ -18,6 +18,7 @@ limitations under the License.
 
 var MatrixClientPeg = require("./MatrixClientPeg");
 var dis = require("./dispatcher");
+var encryption = require("./encryption");
 
 var reject = function(msg) {
     return {
@@ -42,6 +43,25 @@ var commands = {
         return reject("Usage: /nick <display_name>");
     },
 
+    encrypt: function(room_id, args) {
+        if (args == "on") {
+            var client = MatrixClientPeg.get();
+            var members = client.getRoom(room_id).currentState.members;
+            var user_ids = Object.keys(members);
+            return success(
+                encryption.enableEncryption(client, room_id, user_ids)
+            );
+        }
+        if (args == "off") {
+            var client = MatrixClientPeg.get();
+            return success(
+                encryption.disableEncryption(client, room_id)
+            );
+
+        }
+        return reject("Usage: encrypt <on/off>");
+    },
+
     // Change the room topic
     topic: function(room_id, args) {
         if (args) {
@@ -234,4 +254,4 @@ module.exports = {
         }
         return null; // not a command
     }
-};
\ No newline at end of file
+};
diff --git a/src/controllers/organisms/CreateRoom.js b/src/controllers/organisms/CreateRoom.js
index 52b2fd8895..f6404eb231 100644
--- a/src/controllers/organisms/CreateRoom.js
+++ b/src/controllers/organisms/CreateRoom.js
@@ -20,6 +20,7 @@ var React = require("react");
 var MatrixClientPeg = require("../../MatrixClientPeg");
 var PresetValues = require('../atoms/create_room/Presets').Presets;
 var q = require('q');
+var encryption = require("../../encryption");
 
 module.exports = {
     propTypes: {
@@ -103,22 +104,14 @@ module.exports = {
         var response;
 
         if (this.state.encrypt) {
-            var deferred = deferred.then(function(res) {
+            deferred = deferred.then(function(res) {
                 response = res;
-                return cli.downloadKeys([cli.credentials.userId]);
-            }).then(function(res) {
-                // TODO: Check the keys are valid.
-                return cli.downloadKeys(options.invite);
-            }).then(function(res) {
-                return cli.setRoomEncryption(response.room_id, {
-                    algorithm: "m.olm.v1.curve25519-aes-sha2",
-                    members: options.invite,
-                });
-            }).then(function(res) {
-                var d = q.defer();
-                d.resolve(response);
-                return d.promise;
-            });
+                return encryption.enableEncryption(
+                    cli, response.roomId, options.invite
+                );
+            }).then(function() {
+                return q(response) }
+            );
         }
 
         this.setState({
-- 
GitLab