From bd2bb7899462cff05f8e7f146392a037d0c35215 Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Wed, 28 May 2014 16:30:24 +0200 Subject: [PATCH] Implemented support to request and accept contacts when in a call with that contact. --- static/js/controllers/chatroomcontroller.js | 10 +- static/js/directives/chat.js | 100 ++++++++++++-------- static/js/directives/contactrequest.js | 4 +- 3 files changed, 68 insertions(+), 46 deletions(-) diff --git a/static/js/controllers/chatroomcontroller.js b/static/js/controllers/chatroomcontroller.js index 7b88dce7..0b814c2e 100644 --- a/static/js/controllers/chatroomcontroller.js +++ b/static/js/controllers/chatroomcontroller.js @@ -286,17 +286,17 @@ define(['underscore', 'moment', 'text!partials/fileinfo.html', 'text!partials/co $scope.showmessage = function(from, timestamp, message, nodes) { - var userid = $scope.$parent.$parent.id; + var sessonid = $scope.$parent.$parent.id; // Prepare message to display. var s = []; if (message) { s.push(message); - $scope.$emit("incoming", message, from, userid); + $scope.$emit("incoming", message, from, sessonid); } var is_new_message = lastSender !== from; - var is_self = from === userid; + var is_self = from === sessonid; var extra_css = ""; var title = null; @@ -374,7 +374,7 @@ define(['underscore', 'moment', 'text!partials/fileinfo.html', 'text!partials/co $scope.$on("received", function(event, from, data) { - var userid = $scope.$parent.$parent.id; + var sessionid = $scope.$parent.$parent.id; var mid = data.Mid || null; switch (data.Type) { @@ -395,7 +395,7 @@ define(['underscore', 'moment', 'text!partials/fileinfo.html', 'text!partials/co // Definitions. var message = null; var nodes = null; - var fromself = from === userid; + var fromself = from === sessionid; var noop = false; var element = null; var subscope; diff --git a/static/js/directives/chat.js b/static/js/directives/chat.js index 503132a7..c58916c1 100644 --- a/static/js/directives/chat.js +++ b/static/js/directives/chat.js @@ -20,7 +20,7 @@ */ define(['underscore', 'text!partials/chat.html', 'text!partials/chatroom.html'], function(_, templateChat, templateChatroom) { - return ["$compile", "safeDisplayName", "mediaStream", "safeApply", "desktopNotify", "translation", "playSound", "fileUpload", "randomGen", "buddyData", "$timeout", function($compile, safeDisplayName, mediaStream, safeApply, desktopNotify, translation, playSound, fileUpload, randomGen, buddyData, $timeout) { + return ["$compile", "safeDisplayName", "mediaStream", "safeApply", "desktopNotify", "translation", "playSound", "fileUpload", "randomGen", "buddyData", "appData", "$timeout", function($compile, safeDisplayName, mediaStream, safeApply, desktopNotify, translation, playSound, fileUpload, randomGen, buddyData, appData, $timeout) { var displayName = safeDisplayName; var group_chat_id = ""; @@ -141,16 +141,26 @@ define(['underscore', 'text!partials/chat.html', 'text!partials/chatroom.html'], $scope.$parent.$on("requestcontact", function(event, id, options) { if (id !== group_chat_id) { + // Make sure the contact id is valid. + var ad = appData.get(); + if (!ad.userid) { + // Unable to add contacts as we have no own userid. + console.log("You need to log in to add contacts."); + return; + } + var bd = buddyData.get(id); + if (!bd || !bd.session || !bd.session.Userid || ad.userid === bd.session.Userid) { + console.log("This contact cannot be added."); + return; + } var subscope = $scope.showRoom(id, { title: translation._("Chat with") }, options); - $timeout(function() { - subscope.sendChat(id, "Contact request", { - ContactRequest: { - Id: randomGen.random({hex: true}) - } - }); - }, 0); + subscope.sendChatServer(id, "Contact request", { + ContactRequest: { + Id: randomGen.random({hex: true}) + } + }); } }); @@ -222,47 +232,59 @@ define(['underscore', 'text!partials/chat.html', 'text!partials/chatroom.html'], subscope.sendChat = function(to, message, status, mid, noloop) { //console.log("send chat", to, scope.peer); var peercall = mediaStream.webrtc.findTargetCall(to); - if (message && !mid) { - mid = randomGen.random({ - hex: true - }); - } if (peercall && peercall.peerconnection.datachannelReady) { subscope.p2p(true); // Send out stuff through data channel. - _.delay(function() { - mediaStream.api.apply("sendChat", { - send: function(type, data) { - // We also send to self, to display our own stuff. - if (!noloop) { - mediaStream.api.received({ - Type: data.Type, - Data: data, - From: mediaStream.api.id, - To: peercall.id - }); - } - return peercall.peerconnection.send(data); - } - })(to, message, status, mid); - }, 100); - + return subscope.sendChatPeer2Peer(peercall, to, message, status, mid, noloop); } else { subscope.p2p(false); - _.delay(function() { - mediaStream.api.send2("sendChat", function(type, data) { + return subscope.sendChatServer(to, message, status, mid, noloop); + } + return mid; + }; + subscope.sendChatPeer2Peer = function(peercall, to, message, status, mid, noloop) { + if (message && !mid) { + mid = randomGen.random({ + hex: true + }); + } + _.delay(function() { + mediaStream.api.apply("sendChat", { + send: function(type, data) { + // We also send to self, to display our own stuff. if (!noloop) { - //console.log("looped to self", type, data); mediaStream.api.received({ Type: data.Type, Data: data, From: mediaStream.api.id, - To: to + To: peercall.id }); } - })(to, message, status, mid); - }, 100); + return peercall.peerconnection.send(data); + } + })(to, message, status, mid); + }, 100); + return mid; + }; + subscope.sendChatServer = function(to, message, status, mid, noloop) { + if (message && !mid) { + mid = randomGen.random({ + hex: true + }); } + _.delay(function() { + mediaStream.api.send2("sendChat", function(type, data) { + if (!noloop) { + //console.log("looped to self", type, data); + mediaStream.api.received({ + Type: data.Type, + Data: data, + From: mediaStream.api.id, + To: to + }); + } + })(to, message, status, mid); + }, 100); return mid; }; subscope.p2p = function(state) { @@ -312,8 +334,8 @@ define(['underscore', 'text!partials/chat.html', 'text!partials/chatroom.html'], } safeApply(subscope); }); - subscope.$on("incoming", function(event, message, from, userid) { - if (from !== userid) { + subscope.$on("incoming", function(event, message, from, sessionid) { + if (from !== sessionid) { subscope.pending++; scope.$emit("chatincoming", subscope.id); } @@ -321,7 +343,7 @@ define(['underscore', 'text!partials/chat.html', 'text!partials/chatroom.html'], var room = event.targetScope.id; // Make sure we are not in group chat or the message is from ourselves // before we beep and shout. - if (!subscope.isgroupchat && from !== userid) { + if (!subscope.isgroupchat && from !== sessionid) { playSound.play("message1"); desktopNotify.notify(translation._("Message from ") + displayName(from), message); } diff --git a/static/js/directives/contactrequest.js b/static/js/directives/contactrequest.js index 57ade37e..89c3fd08 100644 --- a/static/js/directives/contactrequest.js +++ b/static/js/directives/contactrequest.js @@ -31,14 +31,14 @@ define(['jquery', 'underscore'], function($, _) { }; $scope.doReject = function() { - $scope.state = "rejected"; + $scope.state = "rejected"; $scope.doContact(false); }; $scope.doContact = function(success) { var r = $scope.request; r.Success = !!success; - $scope.sendChat($scope.id, "Contact request answer", { + $scope.sendChatServer($scope.id, "Contact request answer", { ContactRequest: r }); };