Skip to content

Commit

Permalink
check if posts get accepted and validated
Browse files Browse the repository at this point in the history
  • Loading branch information
shuesken committed Aug 11, 2024
1 parent 2540a57 commit 3fb1586
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ export const HITCHMAPS_AUTHOR_PUBLIC_KEY =
export const TRUSTROOTS_NPUB_PUT_URL = "https://www.trustroots.org/api/users";
export const TRUSTROOTS_NIP5_URL =
"https://www.trustroots.org/.well-known/nostr.json";

export const POST_VALIDATION_TIMEOUT_SECONDS = 5;
export const POST_ACCEPTANCE_TIMEOUT_SECONDS = 5;
9 changes: 6 additions & 3 deletions src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {
import { hasPrivateKey } from "./nostr/keys";
import { createNote } from "./nostr/notes";
import { _initRelays } from "./nostr/relays";
import { getMetadataEvent, subscribe } from "./nostr/subscribe";
import {
getKind30398Events,
getMetadataEvent,
subscribe,
} from "./nostr/subscribe";
import { startUserOnboarding } from "./onboarding";
import { Note, NostrEvent, Kind30398Event, MetadataEvent } from "./types";
import {
Expand Down Expand Up @@ -105,9 +109,9 @@ const circleMarker: CircleMarkerOptions = {
(L.control as any).sidepanel(PANEL_CONTAINER_ID, { hasTabs: true }).addTo(map);

// GEOCHAT HACKS
const allKind30398Events = new NSet();

async function updateGeochat() {
const allKind30398Events = await getKind30398Events();
const geochatNotes = document.getElementById(
"geochat-notes"
) as HTMLUListElement;
Expand Down Expand Up @@ -259,7 +263,6 @@ function generateChatContentFromNotes(
}

function addNoteToMap(event: Kind30398Event) {
allKind30398Events.add(event);
const plusCode =
getTagFirstValueFromEvent({
event,
Expand Down
48 changes: 44 additions & 4 deletions src/nostr/notes.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { Event } from "nostr-tools";
import { Event, getPublicKey } from "nostr-tools";
import {
CONTENT_MAXIMUM_LENGTH,
CONTENT_MINIMUM_LENGTH,
LABEL_NAMESPACE_TAG,
MAP_NOTE_KIND,
MAP_NOTE_REPOST_KIND,
OPEN_LOCATION_CODE_NAMESPACE_TAG,
PLUS_CODE_TAG_KEY,
POST_ACCEPTANCE_TIMEOUT_SECONDS,
POST_VALIDATION_TIMEOUT_SECONDS,
TRUSTED_VALIDATION_PUBKEYS,
} from "../constants";
import { UnsignedEvent } from "../types";
import { getPrivateKey } from "./keys";
import { _publish } from "./relays";
import { signEventWithPrivateKey } from "./utils";
import { getTagFirstValueFromEvent, signEventWithPrivateKey } from "./utils";
import { getKind30398Events } from "./subscribe";
import { alert, promiseWithTimeout } from "../utils";

type CreateNoteParams = {
/** The content of the note to publish on the map */
Expand Down Expand Up @@ -52,8 +58,42 @@ export const createNote = async ({
unsignedEvent,
privateKey: key,
});
_publish(signedEvent);
// TODO - How do we wait for the SEEN here?

try {
await promiseWithTimeout(
_publish(signedEvent),
POST_ACCEPTANCE_TIMEOUT_SECONDS * 1000
);
} catch (e) {
alert(
`Your post was not accepted by our relay pool. Please choose different relays or try again later.`
);
console.error(e);
}

setTimeout(async () => {
console.log(`Verifying that event ${signedEvent.id} got validated.`);
const allKind30398Events = await getKind30398Events();
const publicKey = getPublicKey(key);
const events = await allKind30398Events.query([
{
kinds: [MAP_NOTE_REPOST_KIND],
authors: TRUSTED_VALIDATION_PUBKEYS,
"#p": [publicKey],
},
]);
const match = events.find(
(event) =>
getTagFirstValueFromEvent({ event, tag: "e" }) == signedEvent.id
);
if (!match) {
alert(
`Your post was published successfully, but not validated within ${POST_VALIDATION_TIMEOUT_SECONDS} seconds. Hopefully it will be soon!`
);
} else {
console.log(`Event ${signedEvent.id} got validated.`);
}
}, POST_VALIDATION_TIMEOUT_SECONDS * 1000);
};

globalThis.createNote = createNote;
9 changes: 8 additions & 1 deletion src/nostr/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
const fetchProfileQueue = newQueue(2);

const metadataEvents = new nostrify.NCache({ max: 1000 });
const kind30398Events = new nostrify.NCache({ max: 1000 });

export async function getMetadataEvent(pubkey) {
const events = await metadataEvents.query([{ authors: [pubkey] }]);
Expand All @@ -32,6 +33,10 @@ export async function getMetadataEvent(pubkey) {
}
}

export async function getKind30398Events() {
return kind30398Events;
}

type SubscribeParams = {
/** The maximum number of notes to fetch
* @default 200
Expand Down Expand Up @@ -75,6 +80,8 @@ export const subscribe = async ({
return;
}

kind30398Events.add(event);

onEventReceived(event);
const pubKey = getPublicKeyFromEvent({ event });
fetchProfileQueue.add(fetchProfileFactory(pubKey));
Expand Down Expand Up @@ -132,7 +139,7 @@ async function backgroundNoteEventsFetching(onEventReceived) {
}

const onProfileEvent = (event: MetadataEvent) => {
console.log("#zD1Iau got profile event", event);
// console.log("#zD1Iau got profile event", event);

const profile = getProfileFromEvent({ event });
const publicKey = getPublicKeyFromEvent({ event });
Expand Down
14 changes: 14 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,17 @@ export const prompt = async ({
return keyResult.value;
}
};

export function promiseWithTimeout(promise: Promise<any>, timeout: number) {
console.log("stargin with timeout");
return new Promise((resolve, reject) => {
setTimeout(async () => {
reject(`Timed out after ${timeout}ms.`);
}, timeout);
console.log("timeout started");
promise.then((r) => {
console.log("promise resolved");
resolve(r);
});
});
}

0 comments on commit 3fb1586

Please sign in to comment.