This commit is contained in:
Keep Creating Online
2025-01-08 13:26:00 -05:00
parent ee8eaf45d5
commit 1f4db031c9
16 changed files with 554 additions and 578 deletions

View File

@@ -63,32 +63,26 @@ class bitvidApp {
/**
* Initializes the application by setting up the Nostr client and loading videos.
*/
// app.js
async init() {
try {
// Hide the video player sections initially
this.playerSection.classList.add('hidden');
this.playerModal.classList.add('hidden');
// Initialize Nostr client
// Hide and reset player states
this.playerSection.style.display = 'none';
this.playerModal.style.display = 'none';
this.currentMagnetUri = null;
// Initialize Nostr and check login
await nostrClient.init();
this.log('Nostr client initialized.');
// Check if user is already logged in
const savedPubKey = localStorage.getItem('userPubKey');
if (savedPubKey) {
this.login(savedPubKey, false);
}
// Setup event listeners
this.setupEventListeners();
this.log('Event listeners set up.');
// Load videos
await this.loadVideos();
this.log('Videos loaded.');
} catch (error) {
this.log('Failed to initialize app:', error);
this.showError('Failed to connect to Nostr relay. Please try again later.');
console.error('Init failed:', error);
this.showError('Failed to connect to Nostr relay');
}
}

View File

@@ -48,59 +48,44 @@ class NostrClient {
* Initializes the Nostr client by connecting to relays.
*/
async init() {
if (isDevMode) console.log('Connecting to relays...');
try {
if (isDevMode) console.log('Connecting to relays...');
this.pool = new window.NostrTools.SimplePool();
const testFilter = { kinds: [0], limit: 1 };
const connections = this.relays.map(async url => {
try {
return new Promise((resolve) => {
const sub = this.pool.sub([url], [testFilter]);
let timeout = setTimeout(() => {
sub.unsub();
if (isDevMode) console.log(`Connection timeout for ${url}`);
resolve({ url, success: false });
}, 5000);
sub.on('event', () => {
clearTimeout(timeout);
sub.unsub();
if (isDevMode) console.log(`Received event from ${url}`);
resolve({ url, success: true });
});
sub.on('eose', () => {
clearTimeout(timeout);
sub.unsub();
if (isDevMode) console.log(`EOSE from ${url}`);
resolve({ url, success: true });
});
});
} catch (err) {
if (isDevMode) console.error(`Failed to connect to relay: ${url}`, err.message);
return { url, success: false };
}
});
const results = await Promise.all(connections);
const results = await this.connectToRelays();
const successfulRelays = results.filter(r => r.success).map(r => r.url);
if (successfulRelays.length === 0) {
throw new Error('No relays could be connected.');
}
if (isDevMode) {
console.log(`Connected to ${successfulRelays.length} relay(s):`, successfulRelays);
}
if (successfulRelays.length === 0) throw new Error('No relays connected');
if (isDevMode) console.log(`Connected to ${successfulRelays.length} relay(s)`);
} catch (err) {
console.error('Failed to initialize Nostr client:', err.message);
console.error('Nostr init failed:', err);
throw err;
}
}
// Helper method to handle relay connections
async connectToRelays() {
return Promise.all(this.relays.map(url =>
new Promise(resolve => {
const sub = this.pool.sub([url], [{ kinds: [0], limit: 1 }]);
const timeout = setTimeout(() => {
sub.unsub();
resolve({ url, success: false });
}, 5000);
const succeed = () => {
clearTimeout(timeout);
sub.unsub();
resolve({ url, success: true });
};
sub.on('event', succeed);
sub.on('eose', succeed);
})
));
}
/**
* Logs in the user using a Nostr extension or by entering an NSEC key.
*/