aboutsummaryrefslogtreecommitdiff
path: root/javascript/demo
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2016-05-26 17:14:33 +0100
committerRichard van der Hoff <richard@matrix.org>2016-05-26 17:14:33 +0100
commit7c5ab63fd82eee3c597cd54bae76557af497b0d5 (patch)
tree05eb58a5400695e842cf114cfdf6e07059e1c840 /javascript/demo
parent952a3e15cf498b21eef9a13152ca704bca2f9723 (diff)
js group demo: use a remoteOps object
To make sure that we don't sneakily use methods which we wouldn't be able to for remote users, expose an interface object which contains the remote interface.
Diffstat (limited to 'javascript/demo')
-rw-r--r--javascript/demo/group_demo.js51
1 files changed, 35 insertions, 16 deletions
diff --git a/javascript/demo/group_demo.js b/javascript/demo/group_demo.js
index c8939d3..bd54ea0 100644
--- a/javascript/demo/group_demo.js
+++ b/javascript/demo/group_demo.js
@@ -24,8 +24,9 @@ function DemoUser(name) {
this.olmAccount = new Olm.Account();
this.olmAccount.create();
- /* a list of the people in our chat */
- this.peers = [];
+ /* the people in our chat, indexed by their Curve25519 identity key.
+ */
+ this.peers = {};
/* for each peer, a one-to-one session - indexed by id key and created on
* demand */
@@ -41,6 +42,18 @@ function DemoUser(name) {
/* a list of pending tasks */
this.tasks = [];
this.taskWorker = undefined;
+
+ /* the operations our peers are allowed to do on us */
+ var publicOps = [
+ "getIdKey", "getOneTimeKey",
+ "receiveOneToOne", "receiveGroup",
+ ];
+
+ this.remoteOps = {};
+ for (var i=0; i<publicOps.length; i++) {
+ var op = publicOps[i];
+ this.remoteOps[op] = this[op].bind(this);
+ }
}
DemoUser.prototype._progress = function(message) {
@@ -121,8 +134,9 @@ DemoUser.prototype.addTask = function(description, task, callback) {
}
};
-DemoUser.prototype.addPeer = function(peer) {
- this.peers.push(peer);
+DemoUser.prototype.addPeer = function(peerOps) {
+ var id = peerOps.getIdKey();
+ this.peers[id] = peerOps;
};
DemoUser.prototype.getIdKey = function() {
@@ -158,14 +172,15 @@ DemoUser.prototype.getOneTimeKey = function() {
/**
* retrieve, or initiate, a one-to-one session to a given peer
*/
-DemoUser.prototype.getPeerSession = function(peer, callback) {
+DemoUser.prototype.getPeerSession = function(peerId, callback) {
var self = this;
- var peerId = peer.getIdKey();
+
if (this.peerSessions[peerId]) {
callback(this.peerSessions[peerId]);
return;
}
+ var peer = this.peers[peerId];
this.addTask("get peer keys", function(done) {
key = peer.getOneTimeKey();
done(key);
@@ -182,9 +197,9 @@ DemoUser.prototype.getPeerSession = function(peer, callback) {
/**
* encrypt a one-to-one message and prepare it for sending to a peer
*/
-DemoUser.prototype.sendToPeer = function(peer, message, callback) {
+DemoUser.prototype.sendToPeer = function(peerId, message, callback) {
var self = this;
- this.getPeerSession(peer, function(session) {
+ this.getPeerSession(peerId, function(session) {
self.addTask("encrypt one-to-one message", function(done) {
var encrypted = session.encrypt(message);
var packet = {
@@ -194,7 +209,7 @@ DemoUser.prototype.sendToPeer = function(peer, message, callback) {
var json = JSON.stringify(packet);
var el = buttonAndTextElement("send", json, function(ev) {
- peer.receiveOneToOne(json);
+ self.peers[peerId].receiveOneToOne(json);
});
self.cipherOutputDiv.appendChild(el);
done();
@@ -285,8 +300,10 @@ DemoUser.prototype.getGroupSession = function() {
};
var jsonmsg = JSON.stringify(keymsg);
- for (var i = 0; i < this.peers.length; i++) {
- var peer = this.peers[i];
+ for (var peer in this.peers) {
+ if (!this.peers.hasOwnProperty(peer)) {
+ continue;
+ }
this.sendToPeer(peer, jsonmsg);
}
@@ -376,9 +393,11 @@ DemoUser.prototype.encrypt = function(message) {
var json = JSON.stringify(packet);
var el = buttonAndTextElement("send", json, function(ev) {
- for (var i = 0; i < self.peers.length; i++) {
- var peer = self.peers[i];
- peer.receiveGroup(json);
+ for (var peer in self.peers) {
+ if (!self.peers.hasOwnProperty(peer)) {
+ continue;
+ }
+ self.peers[peer].receiveGroup(json);
}
});
self.groupOutputDiv.appendChild(el);
@@ -413,8 +432,8 @@ function startDemo() {
initUserDiv(user2, document.getElementById("user2"));
user2.generateKeys();
- user1.addPeer(user2);
- user2.addPeer(user1);
+ user1.addPeer(user2.remoteOps);
+ user2.addPeer(user1.remoteOps);
}