From a767b9d3b0c7990d99983df782100a39c5a021bb Mon Sep 17 00:00:00 2001 From: enki Date: Sun, 11 May 2025 21:56:12 -0700 Subject: [PATCH] Settings Update --- .gitignore | 3 +- web/assets/js/content.js | 172 ++++++++++++++++++++++++++++++++++++++- web/assets/js/main.js | 48 ++++++++++- web/index.html | 32 ++++++++ 4 files changed, 248 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index ec0baab..ac550a5 100644 --- a/.gitignore +++ b/.gitignore @@ -72,4 +72,5 @@ npm-debug.log* # Temporary files *.tmp *.bak -*.swp \ No newline at end of file +*.swp +**/.claude/settings.local.json diff --git a/web/assets/js/content.js b/web/assets/js/content.js index 18fc6bf..447d1d4 100644 --- a/web/assets/js/content.js +++ b/web/assets/js/content.js @@ -52,13 +52,93 @@ document.addEventListener('DOMContentLoaded', () => { checkAuth(); // Load bot choices after a short delay to ensure auth is checked - setTimeout(() => { + setTimeout(async () => { const token = localStorage.getItem('authToken'); if (token) { - loadBotChoices(); + await loadBotChoices(); + // After loading bots, try to get media settings from first bot + loadMediaSettingsFromServer(); } }, 500); + // Function to load media settings from server + async function loadMediaSettingsFromServer() { + try { + // First try to load from localStorage + const savedConfig = localStorage.getItem('mediaConfig'); + if (savedConfig) { + try { + globalMediaConfig = JSON.parse(savedConfig); + console.log('Loaded media config from localStorage:', globalMediaConfig); + } catch (e) { + console.error('Failed to parse saved media config from localStorage'); + } + } + + // Try to fetch bots to get server settings + const token = localStorage.getItem('authToken'); + const botsResponse = await fetch(`${API_ENDPOINT}/api/bots`, { + headers: { 'Authorization': token } + }); + + if (!botsResponse.ok) { + throw new Error('Failed to fetch bots'); + } + + const bots = await botsResponse.json(); + if (bots.length > 0) { + // Get the first bot with media_config + for (const bot of bots) { + if (bot.media_config) { + console.log('Found media config in bot:', bot.id); + + // Update only if server has values + if (bot.media_config.primary_service) { + globalMediaConfig.primaryService = bot.media_config.primary_service; + } + + if (bot.media_config.fallback_service) { + globalMediaConfig.fallbackService = bot.media_config.fallback_service; + } + + // Update URLs based on services + if (bot.media_config.primary_service === 'nip94' && bot.media_config.nip94_server_url) { + globalMediaConfig.primaryURL = bot.media_config.nip94_server_url; + } else if (bot.media_config.primary_service === 'blossom' && bot.media_config.blossom_server_url) { + globalMediaConfig.primaryURL = bot.media_config.blossom_server_url; + } + + // Update fallback URL + if (bot.media_config.fallback_service === 'nip94' && bot.media_config.nip94_server_url) { + globalMediaConfig.fallbackURL = bot.media_config.nip94_server_url; + } else if (bot.media_config.fallback_service === 'blossom' && bot.media_config.blossom_server_url) { + globalMediaConfig.fallbackURL = bot.media_config.blossom_server_url; + } + + break; // Stop after finding first valid config + } + } + + // Update UI with the config (either from localStorage or server) + updateMediaSettingsUI(); + } + } catch (error) { + console.error('Error loading media settings from server:', error); + // Still update UI with whatever we have + updateMediaSettingsUI(); + } + } + + // Function to update the UI with the current globalMediaConfig + function updateMediaSettingsUI() { + if (primaryServer) primaryServer.value = globalMediaConfig.primaryService; + if (primaryServerURL) primaryServerURL.value = globalMediaConfig.primaryURL; + if (fallbackServer) fallbackServer.value = globalMediaConfig.fallbackService || 'none'; + if (fallbackServerURL) fallbackServerURL.value = globalMediaConfig.fallbackURL; + + console.log('Updated media settings UI with:', globalMediaConfig); + } + // Button click handlers if (loadContentBtn) loadContentBtn.addEventListener('click', handleLoadContent); if (uploadButton) uploadButton.addEventListener('click', handleFileUpload); @@ -209,7 +289,7 @@ document.addEventListener('DOMContentLoaded', () => { } // Handle saving media server settings - function handleSaveMediaSettings() { + async function handleSaveMediaSettings() { globalMediaConfig.primaryService = primaryServer.value; globalMediaConfig.primaryURL = primaryServerURL ? primaryServerURL.value.trim() : ''; globalMediaConfig.fallbackService = fallbackServer.value === 'none' ? '' : fallbackServer.value; @@ -218,7 +298,91 @@ document.addEventListener('DOMContentLoaded', () => { // Save to localStorage localStorage.setItem('mediaConfig', JSON.stringify(globalMediaConfig)); - alert('Media server settings saved!'); + // Get the current bot ID if any is selected, we'll update it first + let selectedBotId = null; + if (botSelect && botSelect.value) { + selectedBotId = botSelect.value; + } + + try { + // Get the list of all bots first + const token = localStorage.getItem('authToken'); + const botsResponse = await fetch(`${API_ENDPOINT}/api/bots`, { + headers: { 'Authorization': token } + }); + + if (!botsResponse.ok) { + throw new Error('Failed to fetch bots'); + } + + const bots = await botsResponse.json(); + console.log(`Found ${bots.length} bots to update with media settings`); + + // For each bot, update its media config + for (const bot of bots) { + // If we have a selected bot, prioritize updating it first + if (selectedBotId && bot.id.toString() === selectedBotId) { + await updateBotMediaConfig(bot.id); + } + } + + // Then update all other bots + for (const bot of bots) { + if (!selectedBotId || bot.id.toString() !== selectedBotId) { + await updateBotMediaConfig(bot.id); + } + } + + alert('Media server settings saved to all bots successfully!'); + } catch (error) { + console.error('Failed to update bot media configs:', error); + alert('Media settings saved locally, but failed to update bots: ' + error.message); + } + } + + // Helper function to update bot media config + async function updateBotMediaConfig(botId) { + try { + const token = localStorage.getItem('authToken'); + // Format mapping between localStorage and server + const configPayload = { + media_config: { + primary_service: globalMediaConfig.primaryService, + fallback_service: globalMediaConfig.fallbackService, + nip94_server_url: globalMediaConfig.primaryService === 'nip94' ? globalMediaConfig.primaryURL : '', + blossom_server_url: globalMediaConfig.primaryService === 'blossom' ? globalMediaConfig.primaryURL : '' + } + }; + + // Also set the fallback URL if appropriate + if (globalMediaConfig.fallbackService === 'nip94') { + configPayload.media_config.nip94_server_url = globalMediaConfig.fallbackURL; + } else if (globalMediaConfig.fallbackService === 'blossom') { + configPayload.media_config.blossom_server_url = globalMediaConfig.fallbackURL; + } + + console.log(`Updating bot ${botId} with media config:`, configPayload); + + const configResp = await fetch(`${API_ENDPOINT}/api/bots/${botId}/config`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + 'Authorization': token + }, + body: JSON.stringify(configPayload) + }); + + if (!configResp.ok) { + const errData = await configResp.json().catch(() => ({})); + throw new Error(errData.error || `Failed to update media config for bot ${botId}`); + } + + return true; + } catch (error) { + console.error(`Failed to update bot ${botId}:`, error); + // Don't throw, just log and continue with other bots + return false; + } } // =================================================== diff --git a/web/assets/js/main.js b/web/assets/js/main.js index 973d98b..efcf77b 100644 --- a/web/assets/js/main.js +++ b/web/assets/js/main.js @@ -1304,6 +1304,32 @@ async function checkAuth() { postModeSelect.value = bot.post_config.post_mode || 'kind20'; } } + + // If media_config is present + if (bot.media_config) { + // Set primary server type + const primaryServerSelect = document.getElementById('botSettingsPrimaryServer'); + if (primaryServerSelect) { + primaryServerSelect.value = bot.media_config.primary_service || 'nip94'; + } + + // Set fallback server type + const fallbackServerSelect = document.getElementById('botSettingsFallbackServer'); + if (fallbackServerSelect) { + fallbackServerSelect.value = bot.media_config.fallback_service || ''; + } + + // Set server URLs + const nip94UrlInput = document.getElementById('botSettingsNip94Url'); + if (nip94UrlInput) { + nip94UrlInput.value = bot.media_config.nip94_server_url || ''; + } + + const blossomUrlInput = document.getElementById('botSettingsBlossomUrl'); + if (blossomUrlInput) { + blossomUrlInput.value = bot.media_config.blossom_server_url || ''; + } + } // Show the modal if (typeof bootstrap !== 'undefined' && botSettingsModalEl) { @@ -1424,7 +1450,19 @@ async function checkAuth() { // Log the post mode selected for debugging console.log('Post mode selected:', postMode); - alert('Setting post mode to: ' + postMode); + + // Get media server configuration + const primaryServer = document.getElementById('botSettingsPrimaryServer').value; + const fallbackServer = document.getElementById('botSettingsFallbackServer').value; + const nip94Url = document.getElementById('botSettingsNip94Url').value.trim(); + const blossomUrl = document.getElementById('botSettingsBlossomUrl').value.trim(); + + console.log('Media config:', { + primaryServer, + fallbackServer, + nip94Url, + blossomUrl + }); const configPayload = { post_config: { @@ -1432,6 +1470,12 @@ async function checkAuth() { hashtags: JSON.stringify(hashtagsArr), post_mode: postMode // We do not override 'enabled' here, so it remains as is + }, + media_config: { + primary_service: primaryServer, + fallback_service: fallbackServer, + nip94_server_url: nip94Url, + blossom_server_url: blossomUrl } }; @@ -1455,7 +1499,7 @@ async function checkAuth() { return; } - alert('Bot settings updated!'); + alert('Bot settings and media configuration updated!'); // Hide modal const modalEl = document.getElementById('botSettingsModal'); diff --git a/web/index.html b/web/index.html index 306b250..6e25488 100644 --- a/web/index.html +++ b/web/index.html @@ -517,6 +517,38 @@
Choose how this bot will post images to Nostr
+ + +
+
Media Server Configuration
+
+
+ + +
+ + +
+
+ + +
+
+
+ + +
+
+