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 { 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<Event[]>([]); // 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 (
<>

View File

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

View File

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

View File

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

View File

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