Skip to content

Commit

Permalink
log state regularly
Browse files Browse the repository at this point in the history
  • Loading branch information
shuesken committed Aug 11, 2024
1 parent bae5e91 commit c52c3b7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
getTagFirstValueFromEvent,
} from "./nostr/utils";
import { NSet } from "@nostrify/nostrify";
import { logStateToConsole } from "./utils";

const map = L.map("map", {
zoomControl: false,
Expand Down Expand Up @@ -367,10 +368,13 @@ function createPopupHtml(createNoteCallback) {
}

const mapStartup = async () => {
logStateToConsole();
const badge = L.DomUtil.get(BADGE_CONTAINER_ID) as HTMLElement;
L.DomUtil.addClass(badge, "hide");
L.DomUtil.removeClass(badge, "show");
await _initRelays();
subscribe({ onEventReceived: addNoteToMap });
};
mapStartup();

setInterval(logStateToConsole, 30 * 1000); // log state every 30 seconds
9 changes: 8 additions & 1 deletion src/nostr/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getTagFirstValueFromEvent,
isValidPlusCode,
} from "./utils";
import { logStateToConsole, promiseWithTimeout } from "../utils";

const fetchProfileQueue = newQueue(2);

Expand Down Expand Up @@ -94,12 +95,18 @@ export const subscribe = async ({

const relayPool = await _initRelays();

const events = (await relayPool.query([eventsFilter])) as Kind30398Event[];
// better handling here, this will throw an error if a relay is unresponsive
const events = (await promiseWithTimeout(
relayPool.query([eventsFilter]),
5000
)) as Kind30398Event[];

console.log("#4aIfX6 Got stored events", events);

events.forEach(onNoteEvent);

logStateToConsole();

backgroundNoteEventsFetching(onNoteEvent);
};

Expand Down
48 changes: 48 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import Swal from "sweetalert2";
import { TRUSTED_VALIDATION_PUBKEYS } from "./constants";
import { getProfileFromEvent } from "./nostr/utils";
import { getPrivateKey, getRelays } from "./nostr";
import { _initRelays } from "./nostr/relays";
import { getPublicKey } from "nostr-tools";
import { getMetadataEvent } from "./nostr/subscribe";

export const confirmYesNo = async (text: string) => {
const result = await Swal.fire({
Expand Down Expand Up @@ -53,3 +59,45 @@ export function promiseWithTimeout(promise: Promise<any>, timeout: number) {
});
});
}

export async function logStateToConsole() {
const validationLines: string[] = [];
for (const key of TRUSTED_VALIDATION_PUBKEYS) {
const metadataEvent = await getMetadataEvent(key);
if (!metadataEvent) validationLines.push(`No profile: ${key}`);
else {
const profile = getProfileFromEvent({ event: metadataEvent });
const line = `${profile.name} ${
profile.trustrootsUsername
} ${key.substring(0, 10)}…`;
validationLines.push(line);
}
}

const relays = await getRelays();
const relayLine = relays.join(", ");

const privateKey = await getPrivateKey();
const publicKey = getPublicKey(privateKey);
const metadataEvent = await getMetadataEvent(publicKey);
let myProfileLine = "";
if (!metadataEvent) myProfileLine = `No profile: ${publicKey}`;
else {
const profile = getProfileFromEvent({ event: metadataEvent });
const line = `${profile.name} ${
profile.trustrootsUsername
} ${publicKey.substring(0, 10)}…`;
myProfileLine = line;
}
console.debug(`
# TRUSTED VALIDATION PUBKEYS
${validationLines}
# CURRENT RELAYS
${relayLine}
# MY PROFILE
${myProfileLine}
`);
}

0 comments on commit c52c3b7

Please sign in to comment.