Skip to content

Commit

Permalink
Fetch first profile from any relay.
Browse files Browse the repository at this point in the history
  • Loading branch information
chmac committed Aug 11, 2024
1 parent e77ce2e commit 93789ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
20 changes: 20 additions & 0 deletions src/nostr/relays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ export const _initRelays = async ({
return relayPool;
};

export const getFirstEventFromAnyRelay = (filter: nostrify.NostrFilter) => {
return new Promise<nostrify.NostrEvent | void>((resolve, reject) => {
_initRelays().then(async (relays) => {
const subscription = relays.req([filter]);
for await (const message of subscription) {
const [messageType, , event] = message;
if (messageType === "EVENT") {
return resolve(event);
}
if (messageType === "EOSE") {
return resolve();
}
if (messageType === "CLOSED") {
return resolve();
}
}
});
});
};

export const _publish = async (event: NostrEvent): Promise<void> => {
const relayPool = await _initRelays();
return relayPool.event(event);
Expand Down
9 changes: 4 additions & 5 deletions src/nostr/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
TRUSTED_VALIDATION_PUBKEYS,
} from "../constants";
import { Kind30398Event, MetadataEvent } from "../types";
import { _initRelays } from "./relays";
import { _initRelays, getFirstEventFromAnyRelay } from "./relays";
import {
doesStringPassSanitisation,
getProfileFromEvent,
Expand Down Expand Up @@ -126,10 +126,9 @@ function fetchProfileFactory(pubKey) {
return;
}

const relayPool = await _initRelays();
const events = (await relayPool.query([profileFilter])) as MetadataEvent[];
if (events.length > 0) {
onProfileEvent(events[0]);
const event = await getFirstEventFromAnyRelay(profileFilter);
if (typeof event !== "undefined") {
onProfileEvent(event as MetadataEvent);
}
};
}
Expand Down

0 comments on commit 93789ee

Please sign in to comment.