2024-01-04 17:01:18 +00:00
|
|
|
import { removeUndefined, throwIfOffline } from "@snort/shared";
|
2024-02-22 11:12:26 +00:00
|
|
|
import { mapEventToProfile, NostrEvent, NostrSystem } from "@snort/system";
|
2024-01-04 17:01:18 +00:00
|
|
|
|
2024-02-22 11:12:26 +00:00
|
|
|
import { EventsCache, Relay, RelayMetrics, SystemDb, UserCache, UserFollows, UserRelays } from "@/Cache";
|
2024-01-19 22:01:13 +00:00
|
|
|
import { addEventToFuzzySearch } from "@/Db/FuzzySearch";
|
2024-01-04 13:48:19 +00:00
|
|
|
import { LoginStore } from "@/Utils/Login";
|
2024-01-04 17:01:18 +00:00
|
|
|
import { hasWasm, WasmOptimizer } from "@/Utils/wasm";
|
2024-01-04 13:15:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton nostr system
|
|
|
|
*/
|
|
|
|
export const System = new NostrSystem({
|
2024-02-22 11:12:26 +00:00
|
|
|
relays: UserRelays,
|
|
|
|
events: EventsCache,
|
|
|
|
profiles: UserCache,
|
2024-01-04 13:15:46 +00:00
|
|
|
relayMetrics: RelayMetrics,
|
2024-02-22 11:12:26 +00:00
|
|
|
cachingRelay: Relay,
|
|
|
|
contactLists: UserFollows,
|
2024-01-04 13:15:46 +00:00
|
|
|
optimizer: hasWasm ? WasmOptimizer : undefined,
|
|
|
|
db: SystemDb,
|
2024-02-22 11:12:26 +00:00
|
|
|
buildFollowGraph: true,
|
2024-01-04 13:15:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
System.on("auth", async (c, r, cb) => {
|
|
|
|
const { id } = LoginStore.snapshot();
|
|
|
|
const pub = LoginStore.getPublisher(id);
|
|
|
|
if (pub) {
|
|
|
|
cb(await pub.nip42Auth(c, r));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
System.on("event", (_, ev) => {
|
2024-01-18 21:11:48 +00:00
|
|
|
Relay.event(ev);
|
|
|
|
EventsCache.discover(ev);
|
2024-01-18 22:04:11 +00:00
|
|
|
UserCache.discover(ev);
|
2024-01-19 22:01:13 +00:00
|
|
|
addEventToFuzzySearch(ev);
|
2024-01-09 12:51:33 +00:00
|
|
|
});
|
|
|
|
|
2024-01-04 13:15:46 +00:00
|
|
|
/**
|
|
|
|
* Add profile loader fn
|
|
|
|
*/
|
|
|
|
if (CONFIG.httpCache) {
|
2024-01-09 12:51:33 +00:00
|
|
|
System.profileLoader.loaderFn = async (keys: Array<string>) => {
|
2024-01-04 13:15:46 +00:00
|
|
|
return removeUndefined(await Promise.all(keys.map(a => fetchProfile(a))));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function fetchProfile(key: string) {
|
|
|
|
try {
|
|
|
|
throwIfOffline();
|
|
|
|
const rsp = await fetch(`${CONFIG.httpCache}/profile/${key}`);
|
|
|
|
if (rsp.ok) {
|
|
|
|
const data = (await rsp.json()) as NostrEvent;
|
|
|
|
if (data) {
|
|
|
|
return mapEventToProfile(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2024-01-23 15:36:48 +00:00
|
|
|
}
|