Commit 7c5ab63f authored by Richard van der Hoff's avatar Richard van der Hoff
Browse files

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.
parent 952a3e15
...@@ -24,8 +24,9 @@ function DemoUser(name) { ...@@ -24,8 +24,9 @@ function DemoUser(name) {
this.olmAccount = new Olm.Account(); this.olmAccount = new Olm.Account();
this.olmAccount.create(); this.olmAccount.create();
/* a list of the people in our chat */ /* the people in our chat, indexed by their Curve25519 identity key.
this.peers = []; */
this.peers = {};
/* for each peer, a one-to-one session - indexed by id key and created on /* for each peer, a one-to-one session - indexed by id key and created on
* demand */ * demand */
...@@ -41,6 +42,18 @@ function DemoUser(name) { ...@@ -41,6 +42,18 @@ function DemoUser(name) {
/* a list of pending tasks */ /* a list of pending tasks */
this.tasks = []; this.tasks = [];
this.taskWorker = undefined; 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) { DemoUser.prototype._progress = function(message) {
...@@ -121,8 +134,9 @@ DemoUser.prototype.addTask = function(description, task, callback) { ...@@ -121,8 +134,9 @@ DemoUser.prototype.addTask = function(description, task, callback) {
} }
}; };
DemoUser.prototype.addPeer = function(peer) { DemoUser.prototype.addPeer = function(peerOps) {
this.peers.push(peer); var id = peerOps.getIdKey();
this.peers[id] = peerOps;
}; };
DemoUser.prototype.getIdKey = function() { DemoUser.prototype.getIdKey = function() {
...@@ -158,14 +172,15 @@ DemoUser.prototype.getOneTimeKey = function() { ...@@ -158,14 +172,15 @@ DemoUser.prototype.getOneTimeKey = function() {
/** /**
* retrieve, or initiate, a one-to-one session to a given peer * 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 self = this;
var peerId = peer.getIdKey();
if (this.peerSessions[peerId]) { if (this.peerSessions[peerId]) {
callback(this.peerSessions[peerId]); callback(this.peerSessions[peerId]);
return; return;
} }
var peer = this.peers[peerId];
this.addTask("get peer keys", function(done) { this.addTask("get peer keys", function(done) {
key = peer.getOneTimeKey(); key = peer.getOneTimeKey();
done(key); done(key);
...@@ -182,9 +197,9 @@ DemoUser.prototype.getPeerSession = function(peer, callback) { ...@@ -182,9 +197,9 @@ DemoUser.prototype.getPeerSession = function(peer, callback) {
/** /**
* encrypt a one-to-one message and prepare it for sending to a peer * 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; var self = this;
this.getPeerSession(peer, function(session) { this.getPeerSession(peerId, function(session) {
self.addTask("encrypt one-to-one message", function(done) { self.addTask("encrypt one-to-one message", function(done) {
var encrypted = session.encrypt(message); var encrypted = session.encrypt(message);
var packet = { var packet = {
...@@ -194,7 +209,7 @@ DemoUser.prototype.sendToPeer = function(peer, message, callback) { ...@@ -194,7 +209,7 @@ DemoUser.prototype.sendToPeer = function(peer, message, callback) {
var json = JSON.stringify(packet); var json = JSON.stringify(packet);
var el = buttonAndTextElement("send", json, function(ev) { var el = buttonAndTextElement("send", json, function(ev) {
peer.receiveOneToOne(json); self.peers[peerId].receiveOneToOne(json);
}); });
self.cipherOutputDiv.appendChild(el); self.cipherOutputDiv.appendChild(el);
done(); done();
...@@ -285,8 +300,10 @@ DemoUser.prototype.getGroupSession = function() { ...@@ -285,8 +300,10 @@ DemoUser.prototype.getGroupSession = function() {
}; };
var jsonmsg = JSON.stringify(keymsg); var jsonmsg = JSON.stringify(keymsg);
for (var i = 0; i < this.peers.length; i++) { for (var peer in this.peers) {
var peer = this.peers[i]; if (!this.peers.hasOwnProperty(peer)) {
continue;
}
this.sendToPeer(peer, jsonmsg); this.sendToPeer(peer, jsonmsg);
} }
...@@ -376,9 +393,11 @@ DemoUser.prototype.encrypt = function(message) { ...@@ -376,9 +393,11 @@ DemoUser.prototype.encrypt = function(message) {
var json = JSON.stringify(packet); var json = JSON.stringify(packet);
var el = buttonAndTextElement("send", json, function(ev) { var el = buttonAndTextElement("send", json, function(ev) {
for (var i = 0; i < self.peers.length; i++) { for (var peer in self.peers) {
var peer = self.peers[i]; if (!self.peers.hasOwnProperty(peer)) {
peer.receiveGroup(json); continue;
}
self.peers[peer].receiveGroup(json);
} }
}); });
self.groupOutputDiv.appendChild(el); self.groupOutputDiv.appendChild(el);
...@@ -413,8 +432,8 @@ function startDemo() { ...@@ -413,8 +432,8 @@ function startDemo() {
initUserDiv(user2, document.getElementById("user2")); initUserDiv(user2, document.getElementById("user2"));
user2.generateKeys(); user2.generateKeys();
user1.addPeer(user2); user1.addPeer(user2.remoteOps);
user2.addPeer(user1); user2.addPeer(user1.remoteOps);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment