chat: Move .etherpad import/export handling to chat.js

This commit is contained in:
Richard Hansen 2022-05-06 02:45:28 -04:00
parent bc06ef87bb
commit 310a371234
4 changed files with 34 additions and 2 deletions

View file

@ -24,7 +24,9 @@
"clientVars": "ep_etherpad-lite/node/chat",
"eejsBlock_mySettings": "ep_etherpad-lite/node/chat",
"eejsBlock_stickyContainer": "ep_etherpad-lite/node/chat",
"exportEtherpad": "ep_etherpad-lite/node/chat",
"handleMessage": "ep_etherpad-lite/node/chat",
"importEtherpad": "ep_etherpad-lite/node/chat",
"padCheck": "ep_etherpad-lite/node/chat",
"padCopy": "ep_etherpad-lite/node/chat",
"padLoad": "ep_etherpad-lite/node/chat",

View file

@ -2,6 +2,7 @@
const ChatMessage = require('../static/js/ChatMessage');
const CustomError = require('./utils/customError');
const Stream = require('./utils/Stream');
const api = require('./db/API');
const assert = require('assert').strict;
const authorManager = require('./db/AuthorManager');
@ -106,6 +107,21 @@ exports.eejsBlock_stickyContainer = (hookName, context) => {
/* eslint-enable max-len */
};
exports.exportEtherpad = async (hookName, {pad, data, dstPadId}) => {
const ops = (function* () {
const {chatHead = -1} = pad;
data[`pad:${dstPadId}`].chatHead = chatHead;
for (let i = 0; i <= chatHead; ++i) {
yield (async () => {
const v = await pad.db.get(`pad:${pad.id}:chat:${i}`);
if (v == null) return;
data[`pad:${dstPadId}:chat:${i}`] = v;
})();
}
})();
for (const op of new Stream(ops).batch(100).buffer(99)) await op;
};
exports.handleMessage = async (hookName, {message, sessionInfo, socket}) => {
const {authorId, padId, readOnly} = sessionInfo;
if (message.type !== 'COLLABROOM' || readOnly) return;
@ -140,6 +156,19 @@ exports.handleMessage = async (hookName, {message, sessionInfo, socket}) => {
return null; // Important! Returning null (not undefined!) stops further processing.
};
exports.importEtherpad = async (hookName, {pad, data, srcPadId}) => {
const ops = (function* () {
const {chatHead = -1} = data[`pad:${srcPadId}`];
pad.chatHead = chatHead;
for (let i = 0; i <= chatHead; ++i) {
const v = data[`pad:${srcPadId}:chat:${i}`];
if (v == null) continue;
yield pad.db.set(`pad:${pad.id}:chat:${i}`, v);
}
})();
for (const op of new Stream(ops).batch(100).buffer(99)) await op;
};
exports.padCheck = async (hookName, {pad}) => {
assert(pad.chatHead != null);
assert(Number.isInteger(pad.chatHead));

View file

@ -50,7 +50,6 @@ exports.getPadRaw = async (padId, readOnlyId) => {
})()];
}
for (let i = 0; i <= pad.head; ++i) yield [`${dstPfx}:revs:${i}`, pad.getRevision(i)];
for (let i = 0; i <= pad.chatHead; ++i) yield [`${dstPfx}:chat:${i}`, pad.getChatMessage(i)];
for (const gen of pluginRecords) yield* gen;
})();
const data = {[dstPfx]: pad};

View file

@ -74,7 +74,9 @@ exports.setPadRaw = async (padId, r, authorId = '') => {
return;
}
value.padIDs = {[padId]: 1};
} else if (padKeyPrefixes.includes(prefix)) {
} else if (padKeyPrefixes.includes(prefix) &&
// Chat message handling was moved to the importEtherpad hook.
(keyParts[0] !== 'pad' || keyParts[2] !== 'chat')) {
checkOriginalPadId(id);
if (prefix === 'pad' && keyParts.length === 2) {
const pool = new AttributePool().fromJsonable(value.pool);