From 726ee7b50b25678dedf4f59dcc8b4048ecac41af Mon Sep 17 00:00:00 2001
From: Mark Haines <mjark@negativecurvature.net>
Date: Tue, 21 Jul 2015 12:03:15 +0100
Subject: [PATCH] Hook up the encrypt button when creating rooms

---
 skins/base/views/organisms/CreateRoom.js |  8 +++++++-
 src/MatrixClientPeg.js                   | 15 +++++++++++++++
 src/controllers/organisms/CreateRoom.js  | 24 +++++++++++++++++++++++-
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/skins/base/views/organisms/CreateRoom.js b/skins/base/views/organisms/CreateRoom.js
index 4fd7a93ee4..21769621eb 100644
--- a/skins/base/views/organisms/CreateRoom.js
+++ b/skins/base/views/organisms/CreateRoom.js
@@ -118,6 +118,12 @@ module.exports = React.createClass({
         })
     },
 
+    onEncryptChanged: function(ev) {
+        this.setState({
+            encrypt: ev.target.checked,
+        });
+    },
+
     render: function() {
         var curr_phase = this.state.phase;
         if (curr_phase == this.phases.CREATING) {
@@ -142,7 +148,7 @@ module.exports = React.createClass({
                     <Presets ref="presets" onChange={this.onPresetChanged} preset={this.state.preset}/> <br />
                     <label><input type="checkbox" ref="is_private" checked={this.state.is_private} onChange={this.onPrivateChanged}/> Make this room private</label>
                     <label><input type="checkbox" ref="share_history" checked={this.state.share_history} onChange={this.onShareHistoryChanged}/> Share message history with new users</label>
-                    <label><input type="checkbox"/> Encrypt room</label>
+                    <label><input type="checkbox" ref="encrypt" checked={this.state.encrypt} onChange={this.onEncryptChanged}/> Encrypt room</label>
                     <CreateRoomButton onCreateRoom={this.onCreateRoom} /> <br />
                     {error_box}
                 </div>
diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js
index 1acd2a7015..322b70f48c 100644
--- a/src/MatrixClientPeg.js
+++ b/src/MatrixClientPeg.js
@@ -23,6 +23,16 @@ var matrixClient = null;
 
 var localStorage = window.localStorage;
 
+function deviceId() {
+    var id = Math.floor(Math.random()*16777215).toString(16);
+    id = "W" + "000000".substring(id.length) + id;
+    if (localStorage) {
+        id = localStorage.getItem("mx_device_id") || id;
+        localStorage.setItem("mx_device_id", id);
+    }
+    return id;
+}
+
 function createClient(hs_url, is_url, user_id, access_token) {
     var opts = {
         baseUrl: hs_url,
@@ -31,6 +41,11 @@ function createClient(hs_url, is_url, user_id, access_token) {
         userId: user_id
     };
 
+    if (localStorage) {
+        opts.sessionStore = new Matrix.WebStorageSessionStore(localStorage);
+        opts.deviceId = deviceId();
+    }
+
     matrixClient = Matrix.createClient(opts);
 }
 
diff --git a/src/controllers/organisms/CreateRoom.js b/src/controllers/organisms/CreateRoom.js
index 107e94b357..52b2fd8895 100644
--- a/src/controllers/organisms/CreateRoom.js
+++ b/src/controllers/organisms/CreateRoom.js
@@ -19,6 +19,7 @@ limitations under the License.
 var React = require("react");
 var MatrixClientPeg = require("../../MatrixClientPeg");
 var PresetValues = require('../atoms/create_room/Presets').Presets;
+var q = require('q');
 
 module.exports = {
     propTypes: {
@@ -97,7 +98,28 @@ module.exports = {
             return;
         }
 
-        var deferred = MatrixClientPeg.get().createRoom(options);
+        var deferred = cli.createRoom(options);
+
+        var response;
+
+        if (this.state.encrypt) {
+            var 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;
+            });
+        }
 
         this.setState({
             phase: this.phases.CREATING,
-- 
GitLab