Browse Source

Implemented support to request and accept contacts when in a call with that contact.

pull/48/head
Simon Eisenmann 12 years ago
parent
commit
bd2bb78994
  1. 10
      static/js/controllers/chatroomcontroller.js
  2. 100
      static/js/directives/chat.js
  3. 4
      static/js/directives/contactrequest.js

10
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) { $scope.showmessage = function(from, timestamp, message, nodes) {
var userid = $scope.$parent.$parent.id; var sessonid = $scope.$parent.$parent.id;
// Prepare message to display. // Prepare message to display.
var s = []; var s = [];
if (message) { if (message) {
s.push(message); s.push(message);
$scope.$emit("incoming", message, from, userid); $scope.$emit("incoming", message, from, sessonid);
} }
var is_new_message = lastSender !== from; var is_new_message = lastSender !== from;
var is_self = from === userid; var is_self = from === sessonid;
var extra_css = ""; var extra_css = "";
var title = null; var title = null;
@ -374,7 +374,7 @@ define(['underscore', 'moment', 'text!partials/fileinfo.html', 'text!partials/co
$scope.$on("received", function(event, from, data) { $scope.$on("received", function(event, from, data) {
var userid = $scope.$parent.$parent.id; var sessionid = $scope.$parent.$parent.id;
var mid = data.Mid || null; var mid = data.Mid || null;
switch (data.Type) { switch (data.Type) {
@ -395,7 +395,7 @@ define(['underscore', 'moment', 'text!partials/fileinfo.html', 'text!partials/co
// Definitions. // Definitions.
var message = null; var message = null;
var nodes = null; var nodes = null;
var fromself = from === userid; var fromself = from === sessionid;
var noop = false; var noop = false;
var element = null; var element = null;
var subscope; var subscope;

100
static/js/directives/chat.js

@ -20,7 +20,7 @@
*/ */
define(['underscore', 'text!partials/chat.html', 'text!partials/chatroom.html'], function(_, templateChat, templateChatroom) { 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 displayName = safeDisplayName;
var group_chat_id = ""; 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) { $scope.$parent.$on("requestcontact", function(event, id, options) {
if (id !== group_chat_id) { 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, { var subscope = $scope.showRoom(id, {
title: translation._("Chat with") title: translation._("Chat with")
}, options); }, options);
$timeout(function() { subscope.sendChatServer(id, "Contact request", {
subscope.sendChat(id, "Contact request", { ContactRequest: {
ContactRequest: { Id: randomGen.random({hex: true})
Id: randomGen.random({hex: true}) }
} });
});
}, 0);
} }
}); });
@ -222,47 +232,59 @@ define(['underscore', 'text!partials/chat.html', 'text!partials/chatroom.html'],
subscope.sendChat = function(to, message, status, mid, noloop) { subscope.sendChat = function(to, message, status, mid, noloop) {
//console.log("send chat", to, scope.peer); //console.log("send chat", to, scope.peer);
var peercall = mediaStream.webrtc.findTargetCall(to); var peercall = mediaStream.webrtc.findTargetCall(to);
if (message && !mid) {
mid = randomGen.random({
hex: true
});
}
if (peercall && peercall.peerconnection.datachannelReady) { if (peercall && peercall.peerconnection.datachannelReady) {
subscope.p2p(true); subscope.p2p(true);
// Send out stuff through data channel. // Send out stuff through data channel.
_.delay(function() { return subscope.sendChatPeer2Peer(peercall, to, message, status, mid, noloop);
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);
} else { } else {
subscope.p2p(false); subscope.p2p(false);
_.delay(function() { return subscope.sendChatServer(to, message, status, mid, noloop);
mediaStream.api.send2("sendChat", function(type, data) { }
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) { if (!noloop) {
//console.log("looped to self", type, data);
mediaStream.api.received({ mediaStream.api.received({
Type: data.Type, Type: data.Type,
Data: data, Data: data,
From: mediaStream.api.id, From: mediaStream.api.id,
To: to To: peercall.id
}); });
} }
})(to, message, status, mid); return peercall.peerconnection.send(data);
}, 100); }
})(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; return mid;
}; };
subscope.p2p = function(state) { subscope.p2p = function(state) {
@ -312,8 +334,8 @@ define(['underscore', 'text!partials/chat.html', 'text!partials/chatroom.html'],
} }
safeApply(subscope); safeApply(subscope);
}); });
subscope.$on("incoming", function(event, message, from, userid) { subscope.$on("incoming", function(event, message, from, sessionid) {
if (from !== userid) { if (from !== sessionid) {
subscope.pending++; subscope.pending++;
scope.$emit("chatincoming", subscope.id); scope.$emit("chatincoming", subscope.id);
} }
@ -321,7 +343,7 @@ define(['underscore', 'text!partials/chat.html', 'text!partials/chatroom.html'],
var room = event.targetScope.id; var room = event.targetScope.id;
// Make sure we are not in group chat or the message is from ourselves // Make sure we are not in group chat or the message is from ourselves
// before we beep and shout. // before we beep and shout.
if (!subscope.isgroupchat && from !== userid) { if (!subscope.isgroupchat && from !== sessionid) {
playSound.play("message1"); playSound.play("message1");
desktopNotify.notify(translation._("Message from ") + displayName(from), message); desktopNotify.notify(translation._("Message from ") + displayName(from), message);
} }

4
static/js/directives/contactrequest.js

@ -31,14 +31,14 @@ define(['jquery', 'underscore'], function($, _) {
}; };
$scope.doReject = function() { $scope.doReject = function() {
$scope.state = "rejected"; $scope.state = "rejected";
$scope.doContact(false); $scope.doContact(false);
}; };
$scope.doContact = function(success) { $scope.doContact = function(success) {
var r = $scope.request; var r = $scope.request;
r.Success = !!success; r.Success = !!success;
$scope.sendChat($scope.id, "Contact request answer", { $scope.sendChatServer($scope.id, "Contact request answer", {
ContactRequest: r ContactRequest: r
}); });
}; };

Loading…
Cancel
Save