chat: Move message checking to padCheck hook

This commit is contained in:
Richard Hansen 2022-04-08 03:56:26 -04:00
parent cdea600acc
commit 42c22ce28d
3 changed files with 19 additions and 17 deletions

View file

@ -25,6 +25,7 @@
"eejsBlock_mySettings": "ep_etherpad-lite/node/chat",
"eejsBlock_stickyContainer": "ep_etherpad-lite/node/chat",
"handleMessage": "ep_etherpad-lite/node/chat",
"padCheck": "ep_etherpad-lite/node/chat",
"socketio": "ep_etherpad-lite/node/chat"
}
},

View file

@ -2,6 +2,7 @@
const ChatMessage = require('../static/js/ChatMessage');
const api = require('./db/API');
const assert = require('assert').strict;
const authorManager = require('./db/AuthorManager');
const hooks = require('../static/js/pluginfw/hooks.js');
const pad = require('./db/Pad');
@ -138,6 +139,23 @@ exports.handleMessage = async (hookName, {message, sessionInfo, socket}) => {
return null; // Important! Returning null (not undefined!) stops further processing.
};
exports.padCheck = async (hookName, {pad}) => {
assert(pad.chatHead != null);
assert(Number.isInteger(pad.chatHead));
assert(pad.chatHead >= -1);
const chats = Stream.range(0, pad.chatHead).map(async (c) => {
try {
const msg = await getChatMessage(pad, c);
assert(msg != null);
assert(msg instanceof ChatMessage);
} catch (err) {
err.message = `(pad ${pad.id} chat message ${c}) ${err.message}`;
throw err;
}
});
for (const p of chats.batch(100).buffer(99)) await p;
};
exports.socketio = (hookName, {io}) => {
socketio = io;
};

View file

@ -690,23 +690,6 @@ class Pad {
assert.deepEqual(this.atext, atext);
assert.deepEqual(this.getAllAuthors().sort(), [...authorIds].sort());
assert(this.chatHead != null);
assert(Number.isInteger(this.chatHead));
assert(this.chatHead >= -1);
const chats = Stream.range(0, this.chatHead + 1)
.map(async (c) => {
try {
const msg = await chat.getChatMessage(this, c);
assert(msg != null);
assert(msg instanceof ChatMessage);
} catch (err) {
err.message = `(pad ${this.id} chat message ${c}) ${err.message}`;
throw err;
}
})
.batch(100).buffer(99);
for (const p of chats) await p;
await hooks.aCallAll('padCheck', {pad: this});
}
}