Skip to content

Commit

Permalink
chat: Move .etherpad import/export handling to chat.js
Browse files Browse the repository at this point in the history
  • Loading branch information
rhansen committed May 6, 2022
1 parent 4719629 commit a7d6d8d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/ep.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions src/node/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -107,6 +108,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;
Expand Down Expand Up @@ -141,6 +157,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(Number.isInteger(pad.chatHead));
assert(pad.chatHead >= -1);
Expand Down
1 change: 0 additions & 1 deletion src/node/utils/ExportEtherpad.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 3 additions & 1 deletion src/node/utils/ImportEtherpad.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a7d6d8d

Please sign in to comment.