API: reload content on server restart

This commit is contained in:
yflory 2023-01-24 16:58:23 +01:00 committed by Wolfgang Ginolas
parent 368dd79883
commit 2be39c3749
8 changed files with 108 additions and 35 deletions

View file

@ -64,6 +64,11 @@ module.exports.create = function (Env, cb) {
}); });
} }
if (metadata && metadata.selfdestruct && metadata.selfdestruct !== Env.id) {
HK.expireChannel(Env, channelName);
return void cb('ESELFDESTRUCT');
}
if (Env.selfDestructTo && Env.selfDestructTo[channelName]) { if (Env.selfDestructTo && Env.selfDestructTo[channelName]) {
clearTimeout(Env.selfDestructTo[channelName]); clearTimeout(Env.selfDestructTo[channelName]);
} }

View file

@ -120,7 +120,7 @@ var CHECKPOINT_PATTERN = /^cp\|(([A-Za-z0-9+\/=]+)\|)?/;
/* expireChannel is here to clean up channels that should have been removed /* expireChannel is here to clean up channels that should have been removed
but for some reason are still present but for some reason are still present
*/ */
const expireChannel = function (Env, channel) { const expireChannel = HK.expireChannel = function (Env, channel) {
return void Env.store.archiveChannel(channel, function (err) { return void Env.store.archiveChannel(channel, function (err) {
Env.Log.info("ARCHIVAL_CHANNEL_BY_HISTORY_KEEPER_EXPIRATION", { Env.Log.info("ARCHIVAL_CHANNEL_BY_HISTORY_KEEPER_EXPIRATION", {
channelId: channel, channelId: channel,
@ -571,6 +571,10 @@ const handleRPC = function (Env, Server, seq, userId, parsed) {
if the provided metadata has an expire time then we also create a task to expire it. if the provided metadata has an expire time then we also create a task to expire it.
*/ */
const handleFirstMessage = function (Env, channelName, metadata) { const handleFirstMessage = function (Env, channelName, metadata) {
if (metadata.selfdestruct) {
// Set the selfdestruct flag to history keeper ID to handle server crash.
metadata.selfdestruct = Env.id;
}
Env.store.writeMetadata(channelName, JSON.stringify(metadata), function (err) { Env.store.writeMetadata(channelName, JSON.stringify(metadata), function (err) {
if (err) { if (err) {
// FIXME tell the user that there was a channel error? // FIXME tell the user that there was a channel error?

View file

@ -459,7 +459,8 @@ define([
} }
} }
common.getSframeChannel().on('EV_VERSION_TIME', function (time) { var sframeChan = common.getSframeChannel();
sframeChan.on('EV_VERSION_TIME', function (time) {
if (!versionHashEl) { return; } if (!versionHashEl) { return; }
var vTime = time; var vTime = time;
var vTimeStr = vTime ? new Date(vTime).toLocaleString() var vTimeStr = vTime ? new Date(vTime).toLocaleString()
@ -468,6 +469,11 @@ define([
versionHashEl.innerText = vTxt; versionHashEl.innerText = vTxt;
versionHashEl = undefined; versionHashEl = undefined;
}); });
sframeChan.on('EV_INTEGRATION_NEEDSAVE', function (format) {
// XXX
// check priv.initialState.name (Blob) to get the format
sframeChan.event('Q_INTEGRATION_SAVE', {});
});
}; };
var noCache = false; // Prevent reload loops var noCache = false; // Prevent reload loops

View file

@ -29,6 +29,7 @@ define([
useCreationScreen: !isIntegration, useCreationScreen: !isIntegration,
messaging: true, messaging: true,
integration: isIntegration, integration: isIntegration,
integrationUtils: integration.utils,
initialState: integration.initialState || undefined initialState: integration.initialState || undefined
}); });
}); });

View file

@ -31,6 +31,7 @@ define([], function () {
var versionHash = conf.versionHash; var versionHash = conf.versionHash;
var validateKey = metadata.validateKey; var validateKey = metadata.validateKey;
var onConnect = conf.onConnect || function () { }; var onConnect = conf.onConnect || function () { };
var onError = conf.onError || function () { };
var lastTime; // Time of last patch (if versioned link); var lastTime; // Time of last patch (if versioned link);
conf = undefined; conf = undefined;
@ -143,6 +144,7 @@ define([], function () {
}); });
padRpc.onErrorEvent.reg(function (err) { padRpc.onErrorEvent.reg(function (err) {
onError(err);
sframeChan.event('EV_RT_ERROR', err); sframeChan.event('EV_RT_ERROR', err);
}); });

View file

@ -1926,6 +1926,19 @@ define([
} }
}); });
var integrationSave = function () {};
if (cfg.integration) {
// TODO
sframeChan.on('Q_INTEGRATION_SAVE', function (obj, cb) {
if (cfg.integrationUtils && cfg.integrationUtils.save) {
cfg.integrationUtils.save(obj, cb);
}
});
integrationSave = function () {
sframeChan.event('EV_INTEGRATION_NEEDSAVE');
};
}
if (cfg.messaging) { if (cfg.messaging) {
sframeChan.on('Q_CHAT_OPENPADCHAT', function (data, cb) { sframeChan.on('Q_CHAT_OPENPADCHAT', function (data, cb) {
Cryptpad.universal.execCommand({ Cryptpad.universal.execCommand({
@ -2016,6 +2029,11 @@ define([
validateKey: secret.keys.validateKey validateKey: secret.keys.validateKey
}; };
} }
if (cfg.integration) {
rtConfig.metadata = rtConfig.metadata || {};
rtConfig.metadata.selfdestruct = true;
}
var cpNfCfg = { var cpNfCfg = {
sframeChan: sframeChan, sframeChan: sframeChan,
@ -2036,6 +2054,13 @@ define([
} }
if (readOnly || cfg.noHash) { return; } if (readOnly || cfg.noHash) { return; }
replaceHash(Utils.Hash.getEditHashFromKeys(secret)); replaceHash(Utils.Hash.getEditHashFromKeys(secret));
},
onError: function (err) {
if (!cfg.integration) { return; }
console.error(err);
if (cfg.integrationUtils && cfg.integrationUtils.reload) {
cfg.integrationUtils.reload();
}
} }
}; };
@ -2089,9 +2114,7 @@ define([
metadata: {} metadata: {}
}; };
if (cfg.integration) { if (cfg.integration) { rtConfig.metadata.selfdestruct = true; }
rtConfig.metadata.selfdestruct = true;
}
if (data.team) { if (data.team) {
Cryptpad.initialTeam = data.team.id; Cryptpad.initialTeam = data.team.id;

View file

@ -69,14 +69,13 @@
}; };
}; };
var makeIframe = function () {} // placeholder
var start = function (config, chan) { var start = function (config, chan) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
setTimeout(function () { setTimeout(function () {
var key = config.document.key; var key = config.document.key;
var blob;
chan.on('SAVE', function (data) {
config.events.onSave(data);
});
var getBlob = function (cb) { var getBlob = function (cb) {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
@ -97,7 +96,7 @@
xhr.send(); xhr.send();
}; };
var start = function (blob) { var start = function () {
chan.send('START', { chan.send('START', {
key: key, key: key,
application: config.documentType, application: config.documentType,
@ -106,26 +105,45 @@
if (obj && obj.error) { reject(obj.error); return console.error(obj.error); } if (obj && obj.error) { reject(obj.error); return console.error(obj.error); }
console.log('OUTER START SUCCESS'); console.log('OUTER START SUCCESS');
resolve({}); resolve({});
resolve = function () {};
reject = function () {};
}); });
}; };
var onKeyValidated = function () { var onKeyValidated = function () {
getBlob(function (err, blob) { if (config.document.blob) { // This is a reload
blob = config.document.blob;
return start();
}
getBlob(function (err, _blob) {
if (err) { reject(err); return console.error(err); } if (err) { reject(err); return console.error(err); }
blob.name = `document.${config.document.fileType}`; _blob.name = `document.${config.document.fileType}`;
start(blob); blob = _blob;
start();
}); });
}; };
chan.send('GET_SESSION', { var getSession = function (cb) {
key: key chan.send('GET_SESSION', {
}, function (obj) { key: key
if (obj && obj.error) { reject(obj.error); return console.error(obj.error); } }, function (obj) {
if (obj.key !== key) { if (obj && obj.error) { reject(obj.error); return console.error(obj.error); }
key = obj.key; if (obj.key !== key) {
config.events.onNewKey(key); key = obj.key;
} config.events.onNewKey(key);
onKeyValidated(); }
cb();
});
};
getSession(onKeyValidated);
chan.on('SAVE', function (data) {
config.events.onSave(data);
});
chan.on('RELOAD', function (data) {
config.document.blob = blob;
document.getElementById('cryptpad-editor').remove();
makeIframe(config);
}); });
}); });
@ -188,20 +206,22 @@
return reject('Invalid arg: cryptpadURL'); return reject('Invalid arg: cryptpadURL');
} }
var iframe = document.createElement('iframe'); makeIframe = function (config) {
iframe.setAttribute('id', 'cryptpad-editor'); var iframe = document.createElement('iframe');
iframe.setAttribute("src", url); iframe.setAttribute('id', 'cryptpad-editor');
container.appendChild(iframe); iframe.setAttribute("src", url);
container.appendChild(iframe);
var onMsg = function (msg) { var onMsg = function (msg) {
var data = typeof(msg.data) === "string" ? JSON.parse(msg.data) : msg.data; var data = typeof(msg.data) === "string" ? JSON.parse(msg.data) : msg.data;
if (!data || data.q !== 'INTEGRATION_READY') { return; } if (!data || data.q !== 'INTEGRATION_READY') { return; }
window.removeEventListener('message', onMsg); window.removeEventListener('message', onMsg);
var chan = makeChan(iframe, parsed.origin); var chan = makeChan(iframe, parsed.origin);
start(config, chan).then(resolve).catch(reject); start(config, chan).then(resolve).catch(reject);
};
window.addEventListener('message', onMsg);
}; };
window.addEventListener('message', onMsg); makeIframe(config);
}); });
}); });
}; };

View file

@ -110,6 +110,14 @@ define([
}); });
}); });
var save = function (data, cb) {
};
var reload = function (data, cb) {
chan.send('RELOAD', data);
console.error(data);
};
chan.on('START', function (data) { chan.on('START', function (data) {
console.warn('INNER START', data); console.warn('INNER START', data);
var href = Hash.hashToHref(data.key, data.application); var href = Hash.hashToHref(data.key, data.application);
@ -118,7 +126,11 @@ define([
pathname: `/${data.application}/`, pathname: `/${data.application}/`,
hash: data.key, hash: data.key,
href: href, href: href,
initialState: isNew ? data.document : undefined initialState: data.document,
utils: {
save: save,
reload: reload
}
}; };
require(['/common/sframe-app-outer.js'], function () { require(['/common/sframe-app-outer.js'], function () {
console.warn('SAO REQUIRED'); console.warn('SAO REQUIRED');