From 42c22ce28d7bc9f9c93c9a3d05ea9045092992ed Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 8 Apr 2022 03:56:26 -0400 Subject: [PATCH] chat: Move message checking to `padCheck` hook --- src/ep.json | 1 + src/node/chat.js | 18 ++++++++++++++++++ src/node/db/Pad.js | 17 ----------------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/ep.json b/src/ep.json index 10ac09e08..566be503c 100644 --- a/src/ep.json +++ b/src/ep.json @@ -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" } }, diff --git a/src/node/chat.js b/src/node/chat.js index 3420df644..f805f6306 100644 --- a/src/node/chat.js +++ b/src/node/chat.js @@ -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; }; diff --git a/src/node/db/Pad.js b/src/node/db/Pad.js index 4a18ddcff..ec7cef6c7 100644 --- a/src/node/db/Pad.js +++ b/src/node/db/Pad.js @@ -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}); } }