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:
@@ -1,18 +1,21 @@
|
||||
import './components'
|
||||
import './bootstrap'
|
||||
import "./components"
|
||||
import {Alpine, Livewire} from '../../vendor/livewire/livewire/dist/livewire.esm';
|
||||
import {NDKNip07Signer} from "@nostr-dev-kit/ndk";
|
||||
import NDKCacheAdapterDexie from "@nostr-dev-kit/ndk-cache-dexie";
|
||||
import nostrStart from "./nostr/nostrStart";
|
||||
|
||||
import Alpine from 'alpinejs'
|
||||
import collapse from '@alpinejs/collapse'
|
||||
import intersect from '@alpinejs/intersect'
|
||||
import focus from '@alpinejs/focus'
|
||||
import NDK, { NDKNip07Signer, NDKEvent } from "@nostr-dev-kit/ndk"
|
||||
Alpine.store('ndk', {
|
||||
// nostr ndk
|
||||
ndk: null,
|
||||
// signer
|
||||
nip07signer: new NDKNip07Signer(),
|
||||
// dexie cache adapter
|
||||
dexieAdapter: new NDKCacheAdapterDexie({dbName: 'einundzwanzigNostrDB', expirationTime: 60 * 60 * 24 * 7}),
|
||||
// current nostr user
|
||||
user: null,
|
||||
// hours ago
|
||||
explicitRelayUrls: [],
|
||||
});
|
||||
Alpine.data('nostrStart', nostrStart);
|
||||
|
||||
window.Alpine = Alpine
|
||||
window.NDK = NDK
|
||||
window.NDKNip07Signer = NDKNip07Signer
|
||||
window.NDKEvent = NDKEvent
|
||||
|
||||
Alpine.plugin(collapse)
|
||||
Alpine.plugin(intersect)
|
||||
Alpine.plugin(focus)
|
||||
Alpine.start()
|
||||
Livewire.start();
|
||||
|
||||
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