mirror of
https://github.com/Yonle/bostr.git
synced 2025-09-28 13:56:20 +02:00
bouncer: too many filter validations.
Signed-off-by: Yonle <yonle@lecturify.net>
This commit is contained in:
53
bouncer.js
53
bouncer.js
@@ -167,6 +167,26 @@ function bc(msg, ws) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function validateFilter(event, filters) {
|
||||||
|
for (const filter of filters) {
|
||||||
|
if (filter === {}) return true;
|
||||||
|
// if filter.since > receivedEvent.created_at, skip
|
||||||
|
// if receivedEvent.created_at > filter.until, skip
|
||||||
|
if (Array.isArray(filter?.ids) && !filter.ids.includes(event.id)) continue;
|
||||||
|
if (Array.isArray(filter?.authors) && !filter.authors.includes(event.pubkey)) continue;
|
||||||
|
if (Array.isArray(filter?.kinds) && !filter.kinds.includes(event.kind)) continue;
|
||||||
|
if (filter?.since > event.created_at) continue;
|
||||||
|
if (event.created_at > filter?.until) continue;
|
||||||
|
|
||||||
|
const NotInSearchQuery = "search" in filter && !event?.content?.toLowerCase().includes(filter.search.toLowerCase());
|
||||||
|
if (NotInSearchQuery) continue;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// WS - Sessions
|
// WS - Sessions
|
||||||
function newConn(addr, client, reconn_t = 0) {
|
function newConn(addr, client, reconn_t = 0) {
|
||||||
if (client.readyState !== 1) return;
|
if (client.readyState !== 1) return;
|
||||||
@@ -208,17 +228,8 @@ function newConn(addr, client, reconn_t = 0) {
|
|||||||
data[1] = client.subalias.get(data[1]);
|
data[1] = client.subalias.get(data[1]);
|
||||||
if (client.pause_subs.has(data[1])) return;
|
if (client.pause_subs.has(data[1])) return;
|
||||||
|
|
||||||
const cFilter = {...client.subs.get(data[1])};
|
const filters = client.subs.get(data[1]);
|
||||||
// if filter.since > receivedEvent.created_at, skip
|
if (!validateFilter(data[2], filters)) return;
|
||||||
// if receivedEvent.created_at > filter.until, skip
|
|
||||||
if (Array.isArray(cFilter?.ids) && !cFilter.ids.includes(data[2].id)) return;
|
|
||||||
if (Array.isArray(cFilter?.authors) && !cFilter.authors.includes(data[2].pubkey)) return;
|
|
||||||
if (Array.isArray(cFilter?.kinds) && !cFilter.kinds.includes(data[2].kind)) return;
|
|
||||||
if (cFilter?.since > data[2].created_at) return;
|
|
||||||
if (data[2].created_at > cFilter?.until) return;
|
|
||||||
|
|
||||||
const NotInSearchQuery = "search" in cFilter && !data[2]?.content?.toLowerCase().includes(cFilter.search.toLowerCase());
|
|
||||||
if (NotInSearchQuery) return;
|
|
||||||
if (client.events.get(data[1]).has(data[2]?.id)) return; // No need to transmit once it has been transmitted before.
|
if (client.events.get(data[1]).has(data[2]?.id)) return; // No need to transmit once it has been transmitted before.
|
||||||
|
|
||||||
if (!client.pause_subs.has(data[1])) {
|
if (!client.pause_subs.has(data[1])) {
|
||||||
@@ -230,14 +241,18 @@ function newConn(addr, client, reconn_t = 0) {
|
|||||||
// If it's at the limit, Send EOSE to client and delete pendingEOSE of subID
|
// If it's at the limit, Send EOSE to client and delete pendingEOSE of subID
|
||||||
|
|
||||||
// Skip if EOSE has been omitted
|
// Skip if EOSE has been omitted
|
||||||
if (!client.pendingEOSE.has(data[1]) || !(cFilter?.limit || cFilter?.ids?.length) || client.pause_subs.has(data[1])) return;
|
if (!client.pendingEOSE.has(data[1])) return;
|
||||||
if (client.events.get(data[1]).size >= (cFilter?.ids?.length || cFilter?.limit)) {
|
for (const filter of filters) {
|
||||||
// Once reached to <filter.limit>, send EOSE to client.
|
if (!(filter?.limit || filter?.ids?.length) || client.pause_subs.has(data[1])) return;
|
||||||
client.send(JSON.stringify(["EOSE", data[1]]));
|
if (client.events.get(data[1]).size >= (filter?.ids?.length || filter?.limit)) {
|
||||||
if (pause_on_limit) {
|
// Once reached to <filter.limit>, send EOSE to client.
|
||||||
client.pause_subs.add(data[1]);
|
client.send(JSON.stringify(["EOSE", data[1]]));
|
||||||
} else {
|
if (pause_on_limit) {
|
||||||
client.pendingEOSE.delete(data[1]);
|
client.pause_subs.add(data[1]);
|
||||||
|
} else {
|
||||||
|
client.pendingEOSE.delete(data[1]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
Reference in New Issue
Block a user