mirror of
https://github.com/Einundzwanzig-Podcast/einundzwanzig-portal.git
synced 2025-12-11 06:46:47 +00:00
load user
This commit is contained in:
7
resources/js/nostr/ndk/excplicitRelays.js
Normal file
7
resources/js/nostr/ndk/excplicitRelays.js
Normal file
@@ -0,0 +1,7 @@
|
||||
export default [
|
||||
'wss://nostr.wine',
|
||||
'wss://nos.lol',
|
||||
'wss://nostr-pub.wellorder.net',
|
||||
'wss://nostr.cercatrova.me',
|
||||
'wss://nostr.mutinywallet.com',
|
||||
];
|
||||
79
resources/js/nostr/ndk/instance.js
Normal file
79
resources/js/nostr/ndk/instance.js
Normal file
@@ -0,0 +1,79 @@
|
||||
import excplicitRelays from "./excplicitRelays.js";
|
||||
import NDK from "@nostr-dev-kit/ndk";
|
||||
|
||||
export const ndkInstance = (Alpine) => ({
|
||||
async init() {
|
||||
try {
|
||||
const urls = excplicitRelays.map((relay) => {
|
||||
if (relay.startsWith('ws')) {
|
||||
return relay.replace('ws', 'http');
|
||||
}
|
||||
if (relay.startsWith('wss')) {
|
||||
return relay.replace('wss', 'https');
|
||||
}
|
||||
});
|
||||
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort('timeout'), 5000);
|
||||
|
||||
const requests = urls.map((url) =>
|
||||
fetch(url, {
|
||||
headers: {Accept: 'application/nostr+json'},
|
||||
signal: controller.signal,
|
||||
})
|
||||
);
|
||||
const responses = await Promise.all(requests);
|
||||
const errors = responses.filter((response) => !response.ok);
|
||||
|
||||
if (errors.length > 0) {
|
||||
throw errors.map((response) => Error(response.statusText));
|
||||
}
|
||||
|
||||
let verifiedRelays = responses.map((res) => {
|
||||
if (res.url.startsWith('http')) {
|
||||
return res.url.replace('http', 'ws');
|
||||
}
|
||||
if (res.url.startsWith('https')) {
|
||||
return res.url.replace('https', 'wss');
|
||||
}
|
||||
});
|
||||
|
||||
// clear timeout
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
console.log('##### verifiedRelays #####', verifiedRelays);
|
||||
Alpine.$store.ndk.explicitRelayUrls = verifiedRelays;
|
||||
|
||||
const instance = new NDK({
|
||||
explicitRelayUrls: Alpine.$store.ndk.explicitRelayUrls,
|
||||
signer: Alpine.$store.ndk.nip07signer,
|
||||
cacheAdapter: Alpine.$store.ndk.dexieAdapter,
|
||||
outboxRelayUrls: ["wss://nostr.einundzwanzig.space",],
|
||||
enableOutboxModel: true,
|
||||
});
|
||||
|
||||
try {
|
||||
await instance.connect(10000);
|
||||
} catch (error) {
|
||||
throw new Error('NDK instance init failed: ', error);
|
||||
}
|
||||
|
||||
// store NDK instance in store
|
||||
Alpine.$store.ndk.ndk = instance;
|
||||
|
||||
// init nip07 signer and fetch profile
|
||||
await Alpine.$store.ndk.nip07signer.user().then(async (user) => {
|
||||
if (!!user.npub) {
|
||||
Alpine.$store.ndk.user = Alpine.$store.ndk.ndk.getUser({
|
||||
npub: user.npub,
|
||||
});
|
||||
await Alpine.$store.ndk.user.fetchProfile();
|
||||
}
|
||||
}).catch((error) => {
|
||||
console.log('##### nip07 signer error #####', error);
|
||||
});
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
19
resources/js/nostr/nostrStart.js
Normal file
19
resources/js/nostr/nostrStart.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import {ndkInstance} from "./ndk/instance.js";
|
||||
|
||||
export default (livewireComponent) => ({
|
||||
|
||||
|
||||
|
||||
async init() {
|
||||
|
||||
await ndkInstance(this).init();
|
||||
|
||||
console.log(this.$store.ndk.user);
|
||||
|
||||
if (this.$store.ndk.user) {
|
||||
this.$wire.setUser(this.$store.ndk.user);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user