due for a clean-up soon

This commit is contained in:
smolgrrr 2023-09-16 02:58:12 +10:00
parent d31771aa11
commit 0fde0ed7d4
5 changed files with 31 additions and 25 deletions

View File

@ -6,21 +6,13 @@ import { relayInit, Event } from 'nostr-tools';
import { subGlobalFeed, simpleSub24hFeed } from '../utils/subscriptions'; import { subGlobalFeed, simpleSub24hFeed } from '../utils/subscriptions';
import { uniqBy } from '../utils/utils'; 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 Home = () => {
const [events, setEvents] = useState<Event[]>([]); // Initialize state const [events, setEvents] = useState<Event[]>([]); // Initialize state
// Define your callback function for subGlobalFeed // Define your callback function for subGlobalFeed
const onEvent = (event: Event, relay: string) => { const onEvent = (event: Event, relay: string) => {
setEvents((prevEvents) => [...prevEvents, event]); setEvents((prevEvents) => [...prevEvents, event]);
console.log(event.id); console.log(event.id + ' ' + event.kind + ' ' + event.tags);
}; };
useEffect(() => { useEffect(() => {
@ -34,8 +26,12 @@ const Home = () => {
}, []); // Empty dependency array means this useEffect runs once when the component mounts }, []); // Empty dependency array means this useEffect runs once when the component mounts
const uniqEvents = events.length > 0 ? uniqBy(events, "id") : []; const uniqEvents = events.length > 0 ? uniqBy(events, "id") : [];
// const filteredEvents = uniqEvents.filter(event => getPow(event.id) > 5); const filteredEvents1 = uniqEvents.filter(event => getPow(event.id) > 3);
const sortedEvents = uniqEvents.sort((a, b) => (b.created_at as any) - (a.created_at as any)); 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 ( return (
<> <>

View File

@ -3,8 +3,9 @@ import { ArrowUpTrayIcon, CpuChipIcon } from '@heroicons/react/24/outline';
import { useState } from 'react'; import { useState } from 'react';
import { Event, generatePrivateKey, getPublicKey, finishEvent, relayInit} from 'nostr-tools'; import { Event, generatePrivateKey, getPublicKey, finishEvent, relayInit} from 'nostr-tools';
import { minePow } from '../../utils/mine'; import { minePow } from '../../utils/mine';
import { publish } from '../../utils/relays';
const difficulty = 10 const difficulty = 20
const NewThreadCard: React.FC = () => { const NewThreadCard: React.FC = () => {
const [comment, setComment] = useState(""); const [comment, setComment] = useState("");
@ -23,7 +24,7 @@ const NewThreadCard: React.FC = () => {
}, difficulty); }, difficulty);
const signedEvent = finishEvent(event, sk); const signedEvent = finishEvent(event, sk);
// await publish(signedEvent); await publish(signedEvent);
console.log(signedEvent.id); console.log(signedEvent.id);
} catch (error) { } catch (error) {

View File

@ -80,14 +80,14 @@ const PostCard = ({ event}: { event: Event }) => {
<div className="mr-2 flex flex-col break-words"> <div className="mr-2 flex flex-col break-words">
{comment} {comment}
</div> </div>
{file !== "" && ( {/* {file !== "" && (
<div className="file"> <div className="file">
<img <img
src={file} src={file}
loading="lazy" loading="lazy"
/> />
</div> </div>
)} )} */}
</div> </div>
</CardContainer> </CardContainer>
</> </>

View File

@ -86,15 +86,13 @@ type PublishCallback = (
) => void; ) => void;
export const publish = (event: Event, cb: PublishCallback) => { export const publish = (event: Event) => {
relayMap.forEach(async (relay, url) => { relayMap.forEach(async (relay, url) => {
try { try {
await relay.publish(event); await relay.publish(event);
console.info(`${relay.url} has accepted our event`); console.info(`${relay.url} has accepted our event: ${event.id}`);
cb(relay.url);
} catch (reason) { } catch (reason) {
console.error(`failed to publish to ${relay.url}: ${reason}`); console.error(`failed to publish to ${relay.url}: ${reason}`);
cb(relay.url, reason as string);
} }
}); });
}; };

View File

@ -1,5 +1,6 @@
import {sub, subOnce, unsubAll} from './relays'; import {sub, subOnce, unsubAll} from './relays';
import { Event } from 'nostr-tools'; import { Event } from 'nostr-tools';
import { getPow } from './mine';
type SubCallback = ( type SubCallback = (
event: Event, event: Event,
@ -13,8 +14,7 @@ export const subGlobalFeed = (onEvent: SubCallback) => {
const now = Math.floor(Date.now() * 0.001); const now = Math.floor(Date.now() * 0.001);
const pubkeys = new Set<string>(); const pubkeys = new Set<string>();
const notes = new Set<string>(); const notes = new Set<string>();
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 sub({ // get past events
cb: (evt, relay) => { cb: (evt, relay) => {
pubkeys.add(evt.pubkey); pubkeys.add(evt.pubkey);
@ -22,17 +22,27 @@ export const subGlobalFeed = (onEvent: SubCallback) => {
onEvent(evt, relay); onEvent(evt, relay);
}, },
filter: { filter: {
...(prefix && {ids: ['0'.repeat(prefix)]}),
kinds: [1], kinds: [1],
since: Math.floor((Date.now() * 0.001) - (24 * 60 * 60)), since: Math.floor((Date.now() * 0.001) - (24 * 60 * 60)),
limit: 10, limit: 100,
}, },
unsub: true 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(() => { setTimeout(() => {
// get profile info // get profile info
sub({ sub({
cb: onEvent, cb: powFilteredCallback,
filter: { filter: {
authors: Array.from(pubkeys), authors: Array.from(pubkeys),
kinds: [0], kinds: [0],
@ -54,7 +64,7 @@ export const subGlobalFeed = (onEvent: SubCallback) => {
) { ) {
return; return;
} }
subOnce({ // get profil data subOnce({ // get profile data
relay, relay,
cb: onEvent, cb: onEvent,
filter: { filter: {
@ -65,7 +75,8 @@ export const subGlobalFeed = (onEvent: SubCallback) => {
}); });
}, },
filter: { filter: {
kinds: [0, 1], ...(prefix && {ids: ['0'.repeat(prefix)]}),
kinds: [1],
since: now, since: now,
}, },
}); });