From a3de8f1595525b07f6d64041dc58e3542fab5454 Mon Sep 17 00:00:00 2001 From: OFF0 Date: Sun, 8 Jan 2023 16:09:35 +0100 Subject: [PATCH] feed: improve recommend server (kind 2) messages - check that event.content is a valid WSS url, drop otherwise - fix bug that rendered the same server recommendation twice --- src/main.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main.js b/src/main.js index ad7c22d..59062e3 100644 --- a/src/main.js +++ b/src/main.js @@ -500,17 +500,25 @@ const sortEventCreatedAt = (created_at) => ( Math.abs(a - created_at) < Math.abs(b - created_at) ? -1 : 1 ); +function isWssUrl(string) { + try { + return 'wss:' === new URL(string).protocol; + } catch (err) { + return false; + } +} + function handleRecommendServer(evt, relay) { - if (feedDomMap[evt.id]) { + if (feedDomMap[evt.id] || !isWssUrl(evt.content)) { return; } const art = renderRecommendServer(evt, relay); if (textNoteList.length < 2) { feedContainer.append(art); - return; + } else { + const closestTextNotes = textNoteList.sort(sortEventCreatedAt(evt.created_at)); + feedDomMap[closestTextNotes[0].id].after(art); } - const closestTextNotes = textNoteList.sort(sortEventCreatedAt(evt.created_at)); - feedDomMap[closestTextNotes[0].id].after(art); feedDomMap[evt.id] = art; }