Browse Source

Added correct support for room changes with contacts.

pull/48/head
Simon Eisenmann 11 years ago
parent
commit
9928d71412
  1. 4
      static/js/services/buddydata.js
  2. 78
      static/js/services/buddylist.js
  3. 52
      static/js/services/buddysession.js

4
static/js/services/buddydata.js

@ -63,6 +63,7 @@ define(['underscore'], function(underscore) { @@ -63,6 +63,7 @@ define(['underscore'], function(underscore) {
},
get: function(id, createInParent, afterCreateCallback, userid) {
if (scopes.hasOwnProperty(id)) {
//console.log("found id scope", id);
return scopes[id];
} else if (!createInParent && pushed.hasOwnProperty(id)) {
return pushed[id].scope;
@ -73,9 +74,11 @@ define(['underscore'], function(underscore) { @@ -73,9 +74,11 @@ define(['underscore'], function(underscore) {
if (createInParent) {
scopes[id] = scope;
}
//console.log("found userid scope", userid);
return scope;
}
if (createInParent) {
//console.log("creating scope", id, userid);
// If we have a parent we can create a new scope.
scope = scopes[id] = createInParent.$new();
if (userid) {
@ -117,7 +120,6 @@ define(['underscore'], function(underscore) { @@ -117,7 +120,6 @@ define(['underscore'], function(underscore) {
del: function(id, hard) {
var scope = scopes[id];
if (scope) {
scope.$destroy();
if (!hard) {
brain[id] = scope;
}

78
static/js/services/buddylist.js

@ -109,6 +109,12 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! @@ -109,6 +109,12 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text!
};
BuddyTree.prototype.keys = function() {
return _.keys(this.data);
};
BuddyTree.prototype.clear = function() {
this.tree.clear();
@ -202,7 +208,7 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! @@ -202,7 +208,7 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text!
scope.element = null;
scope.contact = null;
scope.display = {};
scope.session = buddySession.create(data);
var session = scope.session = buddySession.create(data);
scope.doDefault = function() {
var id = scope.session.Id;
@ -221,14 +227,19 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! @@ -221,14 +227,19 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text!
}
};
scope.$on("$destroy", function() {
//console.log("destroyed");
scope.element = null;
scope.killed = true;
});
//console.log("on buddy scope", session.Userid, session);
};
Buddylist.prototype.onBuddySessionUserid = function(scope, sourceSession) {
//console.log("session with userid", sourceSession);
var userid = sourceSession.Userid;
/*
if (userid === scope.userid) {
@ -239,24 +250,29 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! @@ -239,24 +250,29 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text!
if (!targetScope) {
// No scope for this userid yet - set us.
buddyData.set(userid, scope);
//console.log("set scope with userid", sourceSession);
return;
}
if (targetScope === scope) {
var session = targetScope.session;
if (sourceSession === session) {
// No action.
//console.log("source session same as target");
return;
}
// Merge sessions.
targetScope.session.merge(sourceSession);
session.merge(sourceSession);
// Cleanup old from tree and DOM.
var id = sourceSession.Id;
this.tree.remove(id);
if (targetScope !== scope) {
if (scope.element) {
this.lefts[id] = scope.element;
scope.element = null;
}
//console.log("destroying", id, scope.element);
scope.$destroy();
}
buddyData.set(id, targetScope);
delete this.actionElements[id];
}
};
@ -456,10 +472,11 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! @@ -456,10 +472,11 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text!
this.updateDisplay(id, scope, sessionData, "status");
}
scope.$apply();
return scope;
};
Buddylist.prototype.onJoined = function(data) {
Buddylist.prototype.onJoined = function(data, noApply) {
//console.log("Joined", data);
var id = data.Id;
@ -468,17 +485,21 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! @@ -468,17 +485,21 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text!
}, this), data.Userid);
// Update session.
buddyCount++;
var sessionData = scope.session.update(id, data);
var sessionData = scope.session.update(id, data, _.bind(function(session) {
//console.log("Session is now authenticated", session);
this.onBuddySessionUserid(scope, session);
}, this));
if (sessionData && sessionData.Status) {
this.setDisplay(id, scope, sessionData, "joined");
} else {
} else if (!noApply) {
scope.$apply();
}
return scope;
};
Buddylist.prototype.onLeft = function(data, force) {
Buddylist.prototype.onLeft = function(data, force, noApply) {
//console.log("Left", data);
var id = data.Id;
@ -492,6 +513,7 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! @@ -492,6 +513,7 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text!
}
// Remove current id from tree.
this.tree.remove(id);
buddyData.del(id);
// Remove session.
var session = scope.session;
if ((session.remove(id) && scope.contact === null) || force) {
@ -500,32 +522,58 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! @@ -500,32 +522,58 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text!
this.lefts[id] = scope.element;
this.playSoundLeft = true;
}
buddyData.del(id);
if (session.Userid) {
buddyData.del(session.Userid);
buddyData.del(session.Userid, true);
}
delete this.actionElements[id];
scope.$destroy();
} else {
// Update display stuff if session left. This can
// Update display stuff if a session is left. This can
// return no session in case when we got this as contact.
var sessionData = session.get();
if (sessionData) {
this.updateDisplay(sessionData.Id, scope, sessionData, "status");
} else if (scope.contact) {
// Use it with userid as id in tree.
this.tree.add(session.Userid, scope);
}
if (!noApply) {
scope.$apply();
}
}
return scope;
};
Buddylist.prototype.onClosed = function() {
//console.log("Closed");
console.log("Closed");
this.queue = [];
var data = {};
var sessions = buddySession.sessions();
for (var id in sessions) {
if (sessions.hasOwnProperty(id)) {
console.log("close id", id);
data.Id = id;
this.onLeft(data, false, true);
}
}
console.log("buddyCount after close", buddyCount, this.tree.keys());
/*
this.$element.empty();
buddyCount = 0;
buddyData.clear();
this.tree.clear();
this.actionElements = {};
this.queue = [];
*/
};
@ -595,6 +643,10 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text! @@ -595,6 +643,10 @@ define(['underscore', 'modernizr', 'avltree', 'text!partials/buddy.html', 'text!
buddy.addClass("hovered");
} else {
var scope = buddyData.get(id);
if (!scope) {
console.warn("No scope for buddy", id);
return;
}
var template = buddyActions;
//if (scope.status.autoCalls && _.indexOf(scope.status.autoCalls, "conference") !== -1) {
// template = buddyActionsForAudioMixer;

52
static/js/services/buddysession.js

@ -23,12 +23,27 @@ define(["underscore"], function(_) { @@ -23,12 +23,27 @@ define(["underscore"], function(_) {
// buddySession
return [function() {
var sessions = {};
var serials = 0;
var BuddySession = function(data) {
this.serial = serials++;
this.sessions = {};
this.count = 0;
//console.log("creating session", this.serial, this);
var id = data.Id;
if (data.Id) {
this.add(data.Id, data);
this.Userid = data.Userid || null;
var userid = data.Userid || null;
if (id === userid) {
// Add as default with userid.
this.use(userid, data);
} else {
// Add as session.
var sessionData = this.add(id, data);
if (userid) {
this.auth(userid, sessionData);
}
}
} else {
this.set({});
}
@ -40,12 +55,14 @@ define(["underscore"], function(_) { @@ -40,12 +55,14 @@ define(["underscore"], function(_) {
this.use(id, data);
}
this.count++;
sessions[id] = this;
return data;
};
BuddySession.prototype.rm = function(id) {
delete this.sessions[id];
this.count--;
delete sessions[id];
};
BuddySession.prototype.get = function(id) {
@ -61,7 +78,7 @@ define(["underscore"], function(_) { @@ -61,7 +78,7 @@ define(["underscore"], function(_) {
} else {
this.Id = null;
}
console.log("Use session as default", id, data);
console.log("Use session as default", id, data, this);
};
BuddySession.prototype.remove = function(id, onEmptyCallback) {
@ -95,18 +112,16 @@ define(["underscore"], function(_) { @@ -95,18 +112,16 @@ define(["underscore"], function(_) {
BuddySession.prototype.update = function(id, data, onUseridCallback) {
var sessionData = this.sessions[id];
var sessionData = this.get(id);
if (!sessionData) {
sessionData = this.add(id, data);
}
if (data.Userid && !this.Userid) {
this.Userid = data.Userid;
console.log("Session now has a user id", this.Id, data.Userid);
if (onUseridCallback) {
onUseridCallback(this);
}
var userid = data.Userid;
if (userid) {
this.auth(userid, sessionData, onUseridCallback);
}
if (data.Rev) {
sessionData.Rev = data.Rev;
}
@ -123,6 +138,20 @@ define(["underscore"], function(_) { @@ -123,6 +138,20 @@ define(["underscore"], function(_) {
};
BuddySession.prototype.auth = function(userid, sessionData, onUseridCallback) {
if (!this.Userid) {
this.Userid = userid;
console.log("Session now has a user id", this.Id, userid);
}
// Trigger callback if defined and not triggered before.
if (onUseridCallback && !sessionData.auth) {
onUseridCallback(this);
sessionData.auth = true;
}
};
BuddySession.prototype.merge = function(otherSession) {
if (!this.Userid) {
console.error("Refusing to merge into session as we have no userid", this, otherSession);
@ -144,6 +173,9 @@ define(["underscore"], function(_) { @@ -144,6 +173,9 @@ define(["underscore"], function(_) {
return {
create: function(data) {
return new BuddySession(data);
},
sessions: function() {
return sessions;
}
};

Loading…
Cancel
Save