Cleanup subscription spam

This commit is contained in:
Kieran 2023-01-05 19:20:48 +00:00
parent 989cbe991e
commit e61d25664b
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
3 changed files with 24 additions and 15 deletions

View File

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

View File

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

View File

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