From d8be801b3b1eb133af772eb8750a3b4cd37d09b6 Mon Sep 17 00:00:00 2001 From: HF Date: Thu, 23 Jun 2022 17:25:21 +0200 Subject: [PATCH] fix echo suppression closes #1 --- ppfun-bridge/src/ppfunMatrixBridge.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/ppfun-bridge/src/ppfunMatrixBridge.js b/ppfun-bridge/src/ppfunMatrixBridge.js index 17174a7..8cc53ae 100644 --- a/ppfun-bridge/src/ppfunMatrixBridge.js +++ b/ppfun-bridge/src/ppfunMatrixBridge.js @@ -56,12 +56,33 @@ class PPfunMatrixBridge { const arr = Array.from(this.echoSuppression); for (let i = 0; i < arr.length; i += 1) { const [key, value] = arr[i]; - if (curTime - 30000 > value) { + if (curTime - 60000 > value[1]) { this.echoSuppression.delete(key); } } } + addToEchoSuppression(uid, cid) { + const key = `${uid}:${cid}`; + const curTime = Date.now(); + const uidMsgs = this.echoSuppression.get(key); + if (!uidMsgs) { + this.echoSuppression.set(key, [1, curTime]); + return; + } + uidMsgs[0] += 1; + uidMsgs[1] = curTime; + } + + checkEchoSuppression(uid, cid) { + const uidMsgs = this.echoSuppression.get(`${uid}:${cid}`); + if (uidMsgs && uidMsgs[0] > 0) { + uidMsgs[0] -= 1; + return true; + } + return false; + } + recPPfun(name, uid, msg, cid) { const matrixRoom = this.pToMroomMap.get(cid); if (!matrixRoom) { @@ -73,7 +94,7 @@ class PPfunMatrixBridge { return; } const parsedMsg = parseMsg(msg); - this.echoSuppression.set(`${uid}:${cid}`, Date.now()); + this.addToEchoSuppression(uid, cid); console.log(`PPFUN ${name}: ${parsedMsg}`); this.sendMatrix( name, @@ -158,7 +179,7 @@ class PPfunMatrixBridge { && userId.endsWith(this.domain)) ? userId.slice(2 + this.prefix.length, -this.domain.length - 1) : null; - if (this.echoSuppression.delete(`${uid}:${cid}`)) { + if (this.checkEchoSuppression(uid, cid)) { return; } let name = this.idToNameMap.get(userId);