From 0fde0ed7d4b92a8e894b6fd233ee44c0f2991d67 Mon Sep 17 00:00:00 2001 From: smolgrrr Date: Sat, 16 Sep 2023 02:58:12 +1000 Subject: [PATCH] due for a clean-up soon --- client/src/components/Home.tsx | 18 ++++++--------- .../src/components/PostCard/NewThreadCard.tsx | 5 ++-- client/src/components/PostCard/PostCard.tsx | 4 ++-- client/src/utils/relays.tsx | 6 ++--- client/src/utils/subscriptions.ts | 23 ++++++++++++++----- 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/client/src/components/Home.tsx b/client/src/components/Home.tsx index 5520523..0967d8d 100644 --- a/client/src/components/Home.tsx +++ b/client/src/components/Home.tsx @@ -6,21 +6,13 @@ import { relayInit, Event } from 'nostr-tools'; import { subGlobalFeed, simpleSub24hFeed } from '../utils/subscriptions'; import { uniqBy } from '../utils/utils'; -const relay = relayInit('wss://nostr.lu.ke'); - -type EventRelayMap = { - [eventId: string]: string[]; -}; -const eventRelayMap: EventRelayMap = {}; // eventId: [relay1, relay2] - - const Home = () => { const [events, setEvents] = useState([]); // Initialize state // Define your callback function for subGlobalFeed const onEvent = (event: Event, relay: string) => { setEvents((prevEvents) => [...prevEvents, event]); - console.log(event.id); + console.log(event.id + ' ' + event.kind + ' ' + event.tags); }; useEffect(() => { @@ -34,8 +26,12 @@ const Home = () => { }, []); // Empty dependency array means this useEffect runs once when the component mounts const uniqEvents = events.length > 0 ? uniqBy(events, "id") : []; - // const filteredEvents = uniqEvents.filter(event => getPow(event.id) > 5); - const sortedEvents = uniqEvents.sort((a, b) => (b.created_at as any) - (a.created_at as any)); + const filteredEvents1 = uniqEvents.filter(event => getPow(event.id) > 3); + const filteredEvents2 = filteredEvents1.filter(event => event.kind == 1); + const filteredEvents3 = filteredEvents2.filter(event => + !event.tags.some(tag => tag[0] === 'p') + ); + const sortedEvents = filteredEvents3.sort((a, b) => (b.created_at as any) - (a.created_at as any)); return ( <> diff --git a/client/src/components/PostCard/NewThreadCard.tsx b/client/src/components/PostCard/NewThreadCard.tsx index 646b0e5..910a68b 100644 --- a/client/src/components/PostCard/NewThreadCard.tsx +++ b/client/src/components/PostCard/NewThreadCard.tsx @@ -3,8 +3,9 @@ import { ArrowUpTrayIcon, CpuChipIcon } from '@heroicons/react/24/outline'; import { useState } from 'react'; import { Event, generatePrivateKey, getPublicKey, finishEvent, relayInit} from 'nostr-tools'; import { minePow } from '../../utils/mine'; +import { publish } from '../../utils/relays'; -const difficulty = 10 +const difficulty = 20 const NewThreadCard: React.FC = () => { const [comment, setComment] = useState(""); @@ -23,7 +24,7 @@ const NewThreadCard: React.FC = () => { }, difficulty); const signedEvent = finishEvent(event, sk); - // await publish(signedEvent); + await publish(signedEvent); console.log(signedEvent.id); } catch (error) { diff --git a/client/src/components/PostCard/PostCard.tsx b/client/src/components/PostCard/PostCard.tsx index aac8fb1..0c1e482 100644 --- a/client/src/components/PostCard/PostCard.tsx +++ b/client/src/components/PostCard/PostCard.tsx @@ -80,14 +80,14 @@ const PostCard = ({ event}: { event: Event }) => {
{comment}
- {file !== "" && ( + {/* {file !== "" && (
- )} + )} */} diff --git a/client/src/utils/relays.tsx b/client/src/utils/relays.tsx index 35ae5ff..6bb9703 100644 --- a/client/src/utils/relays.tsx +++ b/client/src/utils/relays.tsx @@ -86,15 +86,13 @@ type PublishCallback = ( ) => void; -export const publish = (event: Event, cb: PublishCallback) => { +export const publish = (event: Event) => { relayMap.forEach(async (relay, url) => { try { await relay.publish(event); - console.info(`${relay.url} has accepted our event`); - cb(relay.url); + console.info(`${relay.url} has accepted our event: ${event.id}`); } catch (reason) { console.error(`failed to publish to ${relay.url}: ${reason}`); - cb(relay.url, reason as string); } }); }; diff --git a/client/src/utils/subscriptions.ts b/client/src/utils/subscriptions.ts index 1ab2f55..49255f0 100644 --- a/client/src/utils/subscriptions.ts +++ b/client/src/utils/subscriptions.ts @@ -1,5 +1,6 @@ import {sub, subOnce, unsubAll} from './relays'; import { Event } from 'nostr-tools'; +import { getPow } from './mine'; type SubCallback = ( event: Event, @@ -13,8 +14,7 @@ export const subGlobalFeed = (onEvent: SubCallback) => { const now = Math.floor(Date.now() * 0.001); const pubkeys = new Set(); const notes = new Set(); - const prefix = Math.floor(10 / 4); // 4 bits in each '0' character - + const prefix = Math.floor(16 / 4); // 4 bits in each '0' character sub({ // get past events cb: (evt, relay) => { pubkeys.add(evt.pubkey); @@ -22,17 +22,27 @@ export const subGlobalFeed = (onEvent: SubCallback) => { onEvent(evt, relay); }, filter: { + ...(prefix && {ids: ['0'.repeat(prefix)]}), kinds: [1], since: Math.floor((Date.now() * 0.001) - (24 * 60 * 60)), - limit: 10, + limit: 100, }, unsub: true }); + // New Callback to only add events that pass the PoW requirement + const powFilteredCallback = (evt: Event, relay: string) => { + if (getPow(evt.id) > 2) { // Replace '5' with your actual PoW requirement + pubkeys.add(evt.pubkey); + notes.add(evt.id); + onEvent(evt, relay); + } + }; + setTimeout(() => { // get profile info sub({ - cb: onEvent, + cb: powFilteredCallback, filter: { authors: Array.from(pubkeys), kinds: [0], @@ -54,7 +64,7 @@ export const subGlobalFeed = (onEvent: SubCallback) => { ) { return; } - subOnce({ // get profil data + subOnce({ // get profile data relay, cb: onEvent, filter: { @@ -65,7 +75,8 @@ export const subGlobalFeed = (onEvent: SubCallback) => { }); }, filter: { - kinds: [0, 1], + ...(prefix && {ids: ['0'.repeat(prefix)]}), + kinds: [1], since: now, }, });