fix echo suppression

closes #1
This commit is contained in:
HF 2022-06-23 17:25:21 +02:00
parent f4739ad500
commit d8be801b3b

View File

@ -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);