Merge branch 'broadcast' into calendar

pull/1/head
yflory 4 years ago
commit 7d6f373ca4

@ -129,13 +129,19 @@ commands.SET_ACCOUNT_RETENTION_TIME = makeIntegerSetter('accountRetentionTime');
var args_isString = function (args) {
return Array.isArray(args) && typeof(args[0]) === "string";
};
// Maintenance: Empty string or an object with a start and end time
var isNumber = function (value) {
return typeof(value) === "number" && !isNaN(value);
};
var args_isMaintenance = function (args) {
return Array.isArray(args) && args[0] && args[0].end && args[0].start; // XXX we could validate that these are numbers && !isNaN
return Array.isArray(args) && args[0] &&
(args[0] === "" || (isNumber(args[0].end) && isNumber(args[0].start)));
};
var makeBroadcastSetter = function (attr) { // XXX could pass extra validation here?
var makeBroadcastSetter = function (attr, validation) {
return function (Env, args) {
if (!args_isString(args) && !args_isMaintenance(args)) {
if ((validation && !validation(args)) || !args_isString(args)) {
throw new Error('INVALID_ARGS');
}
var str = args[0];
@ -154,7 +160,7 @@ commands.SET_SURVEY_URL = makeBroadcastSetter('surveyURL'); // XXX anticipate la
// CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_MAINTENANCE', [{start: +Date, end: +Date}]]], console.log)
// CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_MAINTENANCE', [""]]], console.log)
commands.SET_MAINTENANCE = makeBroadcastSetter('maintenance');
commands.SET_MAINTENANCE = makeBroadcastSetter('maintenance', args_isMaintenance);
var Quota = require("./commands/quota");
var Keys = require("./keys");

@ -970,8 +970,19 @@ define([
var getApi = function (cb) {
return function () {
require(['/api/broadcast?'+ (+new Date())], function (Broadcast) {
// XXX require.s.contexts._ can be used to erase old loaded objects
cb(Broadcast);
setTimeout(function () {
try {
var ctx = require.s.contexts._;
var defined = ctx.defined;
Object.keys(defined).forEach(function (href) {
if (/^\/api\/broadcast\?[0-9]{13}/.test(href)) {
delete defined[href];
return;
}
});
} catch (e) {}
});
});
};
};
@ -1061,7 +1072,7 @@ define([
var form = h('div.cp-admin-broadcast-form');
var $form = $(form).appendTo($div);
var refresh = getApi(function (/* Broadcast */) { // XXX unused argument
var refresh = getApi(function (Broadcast) {
var button = h('button.btn.btn-primary', Messages.admin_broadcastButton);
var $button = $(button);
var removeButton = h('button.btn.btn-danger', Messages.admin_broadcastCancel);
@ -1071,44 +1082,42 @@ define([
var deleted = [];
// Render active message (if there is one)
require(['/api/broadcast?'+ (+new Date())], function (BCast) {
var hash = BCast.lastBroadcastHash || '1'; // Truthy value if no lastKnownHash
common.mailbox.getNotificationsHistory('broadcast', null, hash, function (e, msgs) {
if (e) { return void console.error(e); }
if (!Array.isArray(msgs)) { return; }
if (!msgs.length) {
$active.hide();
var hash = Broadcast.lastBroadcastHash || '1'; // Truthy value if no lastKnownHash
common.mailbox.getNotificationsHistory('broadcast', null, hash, function (e, msgs) {
if (e) { return void console.error(e); }
if (!Array.isArray(msgs)) { return; }
if (!msgs.length) {
$active.hide();
}
msgs.reverse().some(function (data) {
var c = data.content;
var msg = c && c.msg;
if (!msg) { return; }
if (msg.type === "BROADCAST_DELETE") {
deleted.push(Util.find(msg, ['content', 'uid']));
}
msgs.reverse().some(function (data) {
var c = data.content;
var msg = c && c.msg;
if (!msg) { return; }
if (msg.type === "BROADCAST_DELETE") {
deleted.push(Util.find(msg, ['content', 'uid']));
}
if (msg.type !== "BROADCAST_CUSTOM") { return; }
if (deleted.indexOf(msg.uid) !== -1) { return true; }
// We found an active custom message, show it
var el = common.mailbox.createElement(data);
var table = h('table.cp-broadcast-delete');
var $table = $(table);
var uid = Util.find(data, ['content', 'msg', 'uid']);
var time = Util.find(data, ['content', 'msg', 'content', 'time']);
var tr = h('tr', { 'data-uid': uid }, [
h('td', 'ID: '+uid),
h('td', new Date(time || 0).toLocaleString()),
h('td', el),
h('td.delete', removeButton),
]);
$table.append(tr);
$active.append(table);
activeUid = uid;
if (msg.type !== "BROADCAST_CUSTOM") { return; }
if (deleted.indexOf(msg.uid) !== -1) { return true; }
return true;
});
if (!activeUid) { $active.hide(); }
// We found an active custom message, show it
var el = common.mailbox.createElement(data);
var table = h('table.cp-broadcast-delete');
var $table = $(table);
var uid = Util.find(data, ['content', 'msg', 'uid']);
var time = Util.find(data, ['content', 'msg', 'content', 'time']);
var tr = h('tr', { 'data-uid': uid }, [
h('td', 'ID: '+uid),
h('td', new Date(time || 0).toLocaleString()),
h('td', el),
h('td.delete', removeButton),
]);
$table.append(tr);
$active.append(table);
activeUid = uid;
return true;
});
if (!activeUid) { $active.hide(); }
});
// Custom message

@ -162,8 +162,6 @@ define(function() {
// making it much faster to open new tabs.
config.disableWorkers = false;
//config.surveyURL = ""; // XXX remove this?
// Teams are always loaded during the initial loading screen (for the first tab only if
// SharedWorkers are available). Allowing users to be members of multiple teams can
// make them have a very slow loading time. To avoid impacting the user experience

@ -648,6 +648,7 @@ define([
// use uid in /api/broadcast so that all connected users will use the same cached
// version on the server
require(['/api/broadcast?'+uid], function (Broadcast) {
if (!Broadcast) { return; }
broadcast([], 'UNIVERSAL_EVENT', {
type: 'broadcast',
data: {
@ -655,6 +656,18 @@ define([
data: Broadcast.maintenance
}
});
setTimeout(function () {
try {
var ctx = require.s.contexts._;
var defined = ctx.defined;
Object.keys(defined).forEach(function (href) {
if (/^\/api\/broadcast\?[a-z0-9]+/.test(href)) {
delete defined[href];
return;
}
});
} catch (e) {}
});
});
};
Store.onSurveyUpdate = function (uid) {
@ -3178,9 +3191,6 @@ define([
});
};
Store.newVersionReload = function () { // XXX not used anymore?
broadcast([], "NETWORK_RECONNECT");
};
Store.disconnect = function () {
if (self.accountDeletion) { return; }
if (!store.network) { return; }

@ -687,7 +687,6 @@ define([
// Broadcast
//var broadcasts = {}; // XXX defined but never used ?
handlers['BROADCAST_MAINTENANCE'] = function (ctx, box, data, cb) {
var msg = data.msg;
var uid = msg.uid;

@ -1045,6 +1045,7 @@ MessengerUI, Messages) {
]), null, true);
});
var to;
Common.makeUniversal('broadcast', {
onEvent: function (obj) {
var cmd = obj.ev;
@ -1053,13 +1054,25 @@ MessengerUI, Messages) {
if (!data) {
return void $notif.hide();
}
if ((+new Date()) > data.end) {
return void $notif.hide();
}
m = data;
clearTimeout(to);
to = setTimeout(function () {
m = undefined;
$notif.hide();
}, m.end-(+new Date()));
$notif.css('display', '');
}
});
if (m && m.start && m.end) {
$notif.css('display', '');
to = setTimeout(function () {
m = undefined;
$notif.hide();
}, m.end-(+new Date()));
} else {
$notif.hide();
}

Loading…
Cancel
Save