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); } 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) { team.sendEvent = function (q, data, sender) {
ctx.emit(q, data, team.clients.filter(function (cId) { ctx.emit(q, data, team.clients.filter(function (cId) {
return cId !== sender; return cId !== sender;
@ -307,6 +324,12 @@ define([
} }
roster = _roster; 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 you're allowed to edit the roster, try to update your data
if (!rosterData.edit) { return; } if (!rosterData.edit) { return; }
var data = {}; var data = {};
@ -319,6 +342,19 @@ define([
console.error(err); 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 () { }).nThen(function () {
onReady(ctx, id, lm, roster, keys, null, cb); onReady(ctx, id, lm, roster, keys, null, cb);
}); });
@ -457,6 +493,7 @@ define([
ctx.onReadyHandlers[id] = []; ctx.onReadyHandlers[id] = [];
openChannel(ctx, team, id, function (obj) { openChannel(ctx, team, id, function (obj) {
if (!(obj && obj.error)) { console.debug('Team joined:' + id); } if (!(obj && obj.error)) { console.debug('Team joined:' + id); }
ctx.updateMetadata();
cb(obj); 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 removeUser = function (ctx, data, cId, cb) {
var teamId = data.teamId; var teamId = data.teamId;
if (!teamId) { return void cb({error: 'EINVAL'}); } if (!teamId) { return void cb({error: 'EINVAL'}); }

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

Loading…
Cancel
Save