Realtime updates when the roster changes

pull/1/head
yflory 5 years ago
parent 3cc8b5eb83
commit 5d18dcc8ba

@ -137,6 +137,23 @@ define([
if (cId) { team.clients.push(cId); }
roster.on('change', function () {
var state = roster.getState();
var me = Util.find(ctx, ['store', 'proxy', 'curvePublic']);
if (!state.members[me]) {
lm.stop();
roster.stop();
proxy = {};
delete ctx.teams[id];
delete ctx.store.proxy.teams[id];
ctx.emit('LEAVE_TEAM', id, team.clients);
ctx.updateMetadata();
return;
}
ctx.emit('ROSTER_CHANGE', id, team.clients);
});
team.sendEvent = function (q, data, sender) {
ctx.emit(q, data, team.clients.filter(function (cId) {
return cId !== sender;
@ -307,6 +324,12 @@ define([
}
roster = _roster;
// If we've been kicked, don't try to update our data, we'll close everything
// in the next nThen part
var state = roster.getState();
var me = Util.find(ctx, ['store', 'proxy', 'curvePublic']);
if (!state.members[me]) { return; }
// If you're allowed to edit the roster, try to update your data
if (!rosterData.edit) { return; }
var data = {};
@ -319,6 +342,19 @@ define([
console.error(err);
});
}));
}).nThen(function (waitFor) {
// Make sure we have not been kicked from the roster
var state = roster.getState();
var me = Util.find(ctx, ['store', 'proxy', 'curvePublic']);
if (!state.members[me]) {
lm.stop();
roster.stop();
lm.proxy = {};
delete ctx.store.proxy.teams[id];
ctx.updateMetadata();
cb({error: 'EFORBIDDEN'});
waitFor.abort();
}
}).nThen(function () {
onReady(ctx, id, lm, roster, keys, null, cb);
});
@ -457,6 +493,7 @@ define([
ctx.onReadyHandlers[id] = [];
openChannel(ctx, team, id, function (obj) {
if (!(obj && obj.error)) { console.debug('Team joined:' + id); }
ctx.updateMetadata();
cb(obj);
});
};
@ -549,8 +586,6 @@ define([
});
};
// XXX Listen for changes to the roster pad to know if you've been removed
// XXX onReady, if you've been removed, leave the team
var removeUser = function (ctx, data, cId, cb) {
var teamId = data.teamId;
if (!teamId) { return void cb({error: 'EINVAL'}); }

@ -369,7 +369,7 @@ define([
};
var ROLES = ['MEMBER', 'ADMIN', 'OWNER'];
var describeUser = function (data, icon) {
var describeUser = function (common, data, icon) {
APP.module.execCommand('DESCRIBE_USER', {
teamId: APP.team,
curvePublic: data.curvePublic,
@ -379,7 +379,7 @@ define([
$(icon).show();
return void UI.alert(Messages.error);
}
redrawRoster();
redrawRoster(common);
});
};
var makeMember = function (common, data, me) {
@ -403,7 +403,7 @@ define([
$(promote).click(function () {
data.role = 'ADMIN';
$(promote).hide();
describeUser(data, promote);
describeUser(common, data, promote);
});
$actions.append(promote);
}
@ -416,7 +416,7 @@ define([
$(demote).click(function () {
data.role = ROLES[theirRole - 1] || 'MEMBER';
$(demote).hide();
describeUser(data, demote);
describeUser(common, data, demote);
});
$actions.append(demote);
}
@ -435,7 +435,7 @@ define([
$(remove).show();
return void UI.alert(Messages.error);
}
redrawRoster();
redrawRoster(common);
});
});
$actions.append(remove);
@ -449,7 +449,7 @@ define([
];
var div = h('div.cp-team-roster-member', content);
if (data.profile) {
$(div).click(function (e) {
$(div).dblclick(function (e) {
e.preventDefault();
e.stopPropagation();
common.openURL('/profile/#' + data.profile);
@ -463,16 +463,19 @@ define([
var userData = metadataMgr.getUserData();
var me = roster[userData.curvePublic] || {};
var owner = Object.keys(roster).filter(function (k) {
if (roster[k].pending) { return; }
return roster[k].role === "OWNER";
}).map(function (k) {
return makeMember(common, roster[k], me);
});
var admins = Object.keys(roster).filter(function (k) {
if (roster[k].pending) { return; }
return roster[k].role === "ADMIN";
}).map(function (k) {
return makeMember(common, roster[k], me);
});
var members = Object.keys(roster).filter(function (k) {
if (roster[k].pending) { return; }
return roster[k].role === "MEMBER" || !roster[k].role;
}).map(function (k) {
return makeMember(common, roster[k], me);
@ -637,16 +640,6 @@ define([
});
}, true);
var onEvent = function (obj) {
var ev = obj.ev;
var data = obj.data;
if (ev === 'PEWPEW') {
data = data;
// Do something
return;
}
};
var main = function () {
var common;
var readOnly;
@ -712,6 +705,21 @@ define([
}
// Load the Team module
var onEvent = function (obj) {
var ev = obj.ev;
var data = obj.data;
if (ev === 'LEAVE_TEAM') {
$('div.cp-team-cat-back').click();
return;
}
if (ev === 'ROSTER_CHANGE') {
if (Number(APP.team) === Number(data)) {
redrawRoster(common);
}
return;
}
};
APP.module = common.makeUniversal('team', {
onEvent: onEvent
});
@ -719,6 +727,13 @@ define([
$('body').css('display', '');
loadMain(common);
metadataMgr.onChange(function () {
var $div = $('div.cp-team-list');
if (!$div.length) { return; }
refreshList(common, function (content) {
$div.empty().append(content);
});
});
var onDisconnect = function (noAlert) {
setEditable(false);

Loading…
Cancel
Save