From e61d25664bcc91742d54d57502c2bb0aeda7612e Mon Sep 17 00:00:00 2001 From: Kieran Date: Thu, 5 Jan 2023 19:20:48 +0000 Subject: [PATCH] Cleanup subscription spam --- src/feed/Subscription.js | 7 ------- src/feed/TimelineFeed.js | 8 ++++---- src/state/Login.js | 24 ++++++++++++++++++++---- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/feed/Subscription.js b/src/feed/Subscription.js index 2d091937..b1bb523a 100644 --- a/src/feed/Subscription.js +++ b/src/feed/Subscription.js @@ -3,12 +3,6 @@ import { System } from ".."; import { Subscriptions } from "../nostr/Subscriptions"; function notesReducer(state, ev) { - if (ev.reset === true) { - return { - notes: [] - } - } - if (state.notes.some(a => a.id === ev.id)) { return state; } @@ -37,7 +31,6 @@ export default function useSubscription(sub, opt) { useEffect(() => { if (sub) { - dispatch({ reset: true }); sub.OnEvent = (e) => { dispatch(e); }; diff --git a/src/feed/TimelineFeed.js b/src/feed/TimelineFeed.js index 208460b1..9c96f5d0 100644 --- a/src/feed/TimelineFeed.js +++ b/src/feed/TimelineFeed.js @@ -4,6 +4,7 @@ import { Subscriptions } from "../nostr/Subscriptions"; import useSubscription from "./Subscription"; export default function useTimelineFeed(pubKeys, global = false) { + const subTab = global ? "global" : "follows"; const sub = useMemo(() => { if (!Array.isArray(pubKeys)) { pubKeys = [pubKeys]; @@ -14,7 +15,7 @@ export default function useTimelineFeed(pubKeys, global = false) { } let sub = new Subscriptions(); - sub.Id = `timeline:${sub.Id}`; + sub.Id = `timeline:${subTab}`; sub.Authors = new Set(global ? [] : pubKeys); sub.Kinds.add(EventKind.TextNote); sub.Limit = 20; @@ -25,11 +26,10 @@ export default function useTimelineFeed(pubKeys, global = false) { const main = useSubscription(sub, { leaveOpen: true }); const subNext = useMemo(() => { - return null; // spamming subscriptions - + return null; // TODO: spam if (main.notes.length > 0) { let sub = new Subscriptions(); - sub.Id = `timeline-related:${sub.Id}`; + sub.Id = `timeline-related:${subTab}`; sub.Kinds.add(EventKind.Reaction); sub.Kinds.add(EventKind.Deletion); sub.ETags = new Set(main.notes.map(a => a.id)); diff --git a/src/state/Login.js b/src/state/Login.js index 0308fcbe..cd4df895 100644 --- a/src/state/Login.js +++ b/src/state/Login.js @@ -92,7 +92,19 @@ const LoginSlice = createSlice({ state.relays = Object.fromEntries(filtered); }, setFollows: (state, action) => { - state.follows = action.payload; + let existing = new Set(state.follows); + let update = Array.isArray(action.payload) ? action.payload : [action.payload]; + + let changes = false; + for (let pk of update) { + if (!existing.has(pk)) { + existing.add(pk); + changes = true; + } + } + if (changes) { + state.follows = Array.from(existing); + } }, addNotifications: (state, action) => { let n = action.payload; @@ -100,14 +112,18 @@ const LoginSlice = createSlice({ n = [n]; } + let didChange = false; for (let x of n) { if (!state.notifications.some(a => a.id === x.id)) { state.notifications.push(x); + didChange = true; } } - state.notifications = [ - ...state.notifications - ]; + if (didChange) { + state.notifications = [ + ...state.notifications + ]; + } }, logout: (state) => { window.localStorage.removeItem(PrivateKeyItem);