From d50fb327b3afdc82f5f33f95952f6318860b545c Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Fri, 6 Jun 2014 11:33:31 +0200 Subject: [PATCH] Implemented contact add and remove with aggregated buddies. --- static/js/directives/buddylist.js | 12 +++++- static/js/services/buddylist.js | 61 +++++++++++++++++++++++------- static/js/services/buddysession.js | 23 ++++++++--- static/js/services/contactdata.js | 5 ++- static/js/services/contacts.js | 11 ++++++ static/partials/buddy.html | 2 +- 6 files changed, 91 insertions(+), 23 deletions(-) diff --git a/static/js/directives/buddylist.js b/static/js/directives/buddylist.js index e7540529..b7b23542 100644 --- a/static/js/directives/buddylist.js +++ b/static/js/directives/buddylist.js @@ -46,7 +46,7 @@ define(['underscore', 'text!partials/buddylist.html'], function(_, template) { }; - $scope.doContact = function(id) { + $scope.doContactRequest = function(id) { //console.log("doContact", id); $scope.$emit("requestcontact", id, { @@ -55,6 +55,12 @@ define(['underscore', 'text!partials/buddylist.html'], function(_, template) { }; + $scope.doContactRemove = function(userid) { + + contacts.remove(userid); + + }; + $scope.doAudioConference = function(id) { $scope.updateAutoAccept(id); @@ -85,6 +91,7 @@ define(['underscore', 'text!partials/buddylist.html'], function(_, template) { var onLeft = _.bind(buddylist.onLeft, buddylist); var onStatus = _.bind(buddylist.onStatus, buddylist); var onContactAdded = _.bind(buddylist.onContactAdded, buddylist); + var onContactRemoved = _.bind(buddylist.onContactRemoved, buddylist); mediaStream.api.e.on("received.userleftorjoined", function(event, dataType, data) { if (dataType === "Left") { onLeft(data); @@ -117,6 +124,9 @@ define(['underscore', 'text!partials/buddylist.html'], function(_, template) { contacts.e.on("contactadded", function(event, data) { onContactAdded(data); }); + contacts.e.on("contactremoved", function(event, data) { + onContactRemoved(data); + }); }]; diff --git a/static/js/services/buddylist.js b/static/js/services/buddylist.js index 8fe5745b..2d0cebdb 100644 --- a/static/js/services/buddylist.js +++ b/static/js/services/buddylist.js @@ -200,19 +200,25 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! Buddylist.prototype.onBuddyScopeCreated = function(scope, data) { scope.element = null; + scope.contact = null; scope.display = {}; scope.session = buddySession.create(data); scope.doDefault = function() { var id = scope.session.Id; - if (scope.status.isMixer) { - return scope.doAudioConference(id); - } + //if (scope.status.isMixer) { + // return scope.doAudioConference(id); + //} return scope.doCall(id); }; scope.doDefaultContact = function() { - var id = scope.session.Id; - return scope.doContact(id); + var contact = scope.contact; + if (contact) { + return scope.doContactRemove(contact.Userid); + } else { + var id = scope.session.Id; + return scope.doContactRequest(id); + } }; scope.$on("$destroy", function() { scope.element = null; @@ -472,7 +478,7 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! }; - Buddylist.prototype.onLeft = function(data) { + Buddylist.prototype.onLeft = function(data, force) { //console.log("Left", data); var id = data.Id; @@ -488,7 +494,7 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! this.tree.remove(id); // Remove session. var session = scope.session; - if (session.remove(id)) { + if ((session.remove(id) && scope.contact === null) || force) { // No session left. Cleanup. if (scope.element) { this.lefts[id] = scope.element; @@ -500,9 +506,12 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! } delete this.actionElements[id]; } else { - // Update display stuff if session left. + // Update display stuff if session left. This can + // return no session in case when we got this as contact. var sessionData = session.get(); - this.updateDisplay(sessionData.Id, scope, sessionData, "status"); + if (sessionData) { + this.updateDisplay(sessionData.Id, scope, sessionData, "status"); + } scope.$apply(); } @@ -528,13 +537,37 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! var scope = buddyData.get(userid); if (scope) { scope.contact = contact; + var sessionData = scope.session.get(); + if (sessionData) { + if (contact.Status === null && sessionData.Status) { + // Update contact status with session.Status + contact.Status = _.extend({}, sessionData.Status); + console.log("Injected status into contact", contact); + } + this.updateDisplay(sessionData.Id, scope, contact, "status"); + } + } else { + // TODO(longsleep): Implement rendering of contacts without scope. + console.log("No scope for contact", userid); } }; - Buddylist.prototype.onContactRemoved = function(data) { + Buddylist.prototype.onContactRemoved = function(contact) { - console.log("onContactRemoved", data); + console.log("onContactRemoved", contact); + var userid = contact.Userid; + + var scope = buddyData.get(userid); + if (scope) { + scope.contact = null; + // Remove with left when no session for this userid. + var sessionData = scope.session.get(); + if (!sessionData) { + // Force left. + this.onLeft({Id: userid}, true); + } + } }; @@ -563,9 +596,9 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! } else { var scope = buddyData.get(id); var template = buddyActions; - if (scope.status.autoCalls && _.indexOf(scope.status.autoCalls, "conference") !== -1) { - template = buddyActionsForAudioMixer; - } + //if (scope.status.autoCalls && _.indexOf(scope.status.autoCalls, "conference") !== -1) { + // template = buddyActionsForAudioMixer; + //} //console.log("scope", scope, id); template(scope, _.bind(function(clonedElement, $scope) { actionElements[id] = clonedElement; diff --git a/static/js/services/buddysession.js b/static/js/services/buddysession.js index 97a43d3d..4e085902 100644 --- a/static/js/services/buddysession.js +++ b/static/js/services/buddysession.js @@ -28,7 +28,6 @@ define(["underscore"], function(_) { this.count = 0; if (data.Id) { this.add(data.Id, data); - this.use(data); this.Userid = data.Userid || null; } else { this.set({}); @@ -37,6 +36,9 @@ define(["underscore"], function(_) { BuddySession.prototype.add = function(id, data) { this.sessions[id] = data; + if (this.count === 0) { + this.use(id, data); + } this.count++; return data; }; @@ -53,9 +55,13 @@ define(["underscore"], function(_) { return this.sessions[id]; }; - BuddySession.prototype.use = function(data) { - this.Id = data.Id || null; - this.Ua = data.Ua || ""; + BuddySession.prototype.use = function(id, data) { + if (id) { + this.Id = id; + } else { + this.Id = null; + } + console.log("Use session as default", id, data); }; BuddySession.prototype.remove = function(id, onEmptyCallback) { @@ -71,10 +77,15 @@ define(["underscore"], function(_) { } } if (sessionData) { - //console.log("remove session", sessionData); - this.use(sessionData); + this.use(sessionData.Id, sessionData); } else { console.log("Last session removed", sessions); + if (this.Userid) { + console.log("Using userid as session id"); + this.use(this.Userid); + } else { + this.use(null); + } return true; } } diff --git a/static/js/services/contactdata.js b/static/js/services/contactdata.js index 2becce40..0ab3f0dd 100644 --- a/static/js/services/contactdata.js +++ b/static/js/services/contactdata.js @@ -44,7 +44,7 @@ define(['underscore', 'jquery'], function(underscore, $) { Id: "contact-"+id, Userid: userid, Token: token, - Status: $.extend({}, status) + Status: null } // TODO(longsleep): Trigger this to somewhere. return contact; @@ -56,6 +56,9 @@ define(['underscore', 'jquery'], function(underscore, $) { } return null; }, + remove: function(userid) { + delete users[userid]; + }, getById: function(id) { if (id.indexOf("contact-") === 0) { id = id.substr(8) diff --git a/static/js/services/contacts.js b/static/js/services/contacts.js index 94714d72..cc89d90c 100644 --- a/static/js/services/contacts.js +++ b/static/js/services/contacts.js @@ -34,6 +34,17 @@ define(['underscore', 'jquery'], function(underscore, $) { }; + Contacts.prototype.remove = function(userid) { + + var contact = contactData.get(userid); + //console.log("contacts remove", userid, contact); + if (contact) { + contactData.remove(userid); + this.e.triggerHandler("contactremoved", contact); + } + + }; + return new Contacts(); }]; diff --git a/static/partials/buddy.html b/static/partials/buddy.html index 2002cb7a..d0075eea 100644 --- a/static/partials/buddy.html +++ b/static/partials/buddy.html @@ -1,5 +1,5 @@
{{session.Id|displayName}}
-
({{session.count}}) {{display.subLine}}
+
({{session.count}}) {{display.subLine}}