Settings Update

This commit is contained in:
Enki 2025-05-11 21:56:12 -07:00
parent be9cd52ac6
commit a767b9d3b0
4 changed files with 248 additions and 7 deletions

1
.gitignore vendored
View File

@ -73,3 +73,4 @@ npm-debug.log*
*.tmp *.tmp
*.bak *.bak
*.swp *.swp
**/.claude/settings.local.json

View File

@ -52,13 +52,93 @@ document.addEventListener('DOMContentLoaded', () => {
checkAuth(); checkAuth();
// Load bot choices after a short delay to ensure auth is checked // Load bot choices after a short delay to ensure auth is checked
setTimeout(() => { setTimeout(async () => {
const token = localStorage.getItem('authToken'); const token = localStorage.getItem('authToken');
if (token) { if (token) {
loadBotChoices(); await loadBotChoices();
// After loading bots, try to get media settings from first bot
loadMediaSettingsFromServer();
} }
}, 500); }, 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 // Button click handlers
if (loadContentBtn) loadContentBtn.addEventListener('click', handleLoadContent); if (loadContentBtn) loadContentBtn.addEventListener('click', handleLoadContent);
if (uploadButton) uploadButton.addEventListener('click', handleFileUpload); if (uploadButton) uploadButton.addEventListener('click', handleFileUpload);
@ -209,7 +289,7 @@ document.addEventListener('DOMContentLoaded', () => {
} }
// Handle saving media server settings // Handle saving media server settings
function handleSaveMediaSettings() { async function handleSaveMediaSettings() {
globalMediaConfig.primaryService = primaryServer.value; globalMediaConfig.primaryService = primaryServer.value;
globalMediaConfig.primaryURL = primaryServerURL ? primaryServerURL.value.trim() : ''; globalMediaConfig.primaryURL = primaryServerURL ? primaryServerURL.value.trim() : '';
globalMediaConfig.fallbackService = fallbackServer.value === 'none' ? '' : fallbackServer.value; globalMediaConfig.fallbackService = fallbackServer.value === 'none' ? '' : fallbackServer.value;
@ -218,7 +298,91 @@ document.addEventListener('DOMContentLoaded', () => {
// Save to localStorage // Save to localStorage
localStorage.setItem('mediaConfig', JSON.stringify(globalMediaConfig)); 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;
}
} }
// =================================================== // ===================================================

View File

@ -1305,6 +1305,32 @@ async function checkAuth() {
} }
} }
// 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 // Show the modal
if (typeof bootstrap !== 'undefined' && botSettingsModalEl) { if (typeof bootstrap !== 'undefined' && botSettingsModalEl) {
if (!botSettingsModal) { if (!botSettingsModal) {
@ -1424,7 +1450,19 @@ async function checkAuth() {
// Log the post mode selected for debugging // Log the post mode selected for debugging
console.log('Post mode selected:', postMode); 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 = { const configPayload = {
post_config: { post_config: {
@ -1432,6 +1470,12 @@ async function checkAuth() {
hashtags: JSON.stringify(hashtagsArr), hashtags: JSON.stringify(hashtagsArr),
post_mode: postMode post_mode: postMode
// We do not override 'enabled' here, so it remains as is // 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; return;
} }
alert('Bot settings updated!'); alert('Bot settings and media configuration updated!');
// Hide modal // Hide modal
const modalEl = document.getElementById('botSettingsModal'); const modalEl = document.getElementById('botSettingsModal');

View File

@ -517,6 +517,38 @@
</select> </select>
<div class="form-text">Choose how this bot will post images to Nostr</div> <div class="form-text">Choose how this bot will post images to Nostr</div>
</div> </div>
<!-- Media Server Configuration -->
<div class="mb-3">
<h5 class="border-top pt-3">Media Server Configuration</h5>
<div class="row">
<div class="col-md-6 mb-2">
<label for="botSettingsPrimaryServer" class="form-label">Primary Media Server</label>
<select id="botSettingsPrimaryServer" class="form-select mb-2">
<option value="nip94">NIP-94 Server</option>
<option value="blossom">Blossom Server</option>
</select>
<div class="mb-2">
<label for="botSettingsNip94Url" class="form-label">NIP-94 URL</label>
<input type="text" id="botSettingsNip94Url" class="form-control"
placeholder="https://files.sovbit.host">
</div>
<div class="mb-2">
<label for="botSettingsBlossomUrl" class="form-label">Blossom URL</label>
<input type="text" id="botSettingsBlossomUrl" class="form-control"
placeholder="https://cdn.sovbit.host/upload">
</div>
</div>
<div class="col-md-6 mb-2">
<label for="botSettingsFallbackServer" class="form-label">Fallback Server</label>
<select id="botSettingsFallbackServer" class="form-select mb-2">
<option value="">None</option>
<option value="nip94">NIP-94 Server</option>
<option value="blossom">Blossom Server</option>
</select>
</div>
</div>
</div>
</form> </form>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">