refactor: comprehensive codebase review — security, performance, quality, UX

Security:
- Fix NUT protocol command injection (validate names against safe regex)
- Enable Jinja2 autoescape=True to prevent HTML injection via external data
- Add WebhookProviderConfig validation model

Performance:
- Shared aiohttp.ClientSession singleton (replaces 40+ per-request sessions)
- Fix 4 N+1 queries with batch IN loads (poller, scheduler, memory, broadcast)
- asyncio.gather for Gitea commands and notification dispatcher
- Add DB indexes on NotificationTrackerState.tracker_id, CommandTrackerListener
- LRU cache for compiled Jinja2 templates
- Daily EventLog cleanup job (90-day retention)
- 30s HTTP timeout on all external calls
- GROUP BY for target type counts (replaces 7 sequential queries)

Code quality:
- Extract get_owned_entity() helper (replaces 11 duplicate functions)
- Extract slot_helpers.py (load_slots, save_slots, render_template_preview)
- Extract command_utils.py (tracker lookup, last event, collection IDs)
- Extract http_session.py (shared session lifecycle)
- Provider connection validation dedup (3x → 1 helper)
- Command dispatch tables replacing if/elif chains
- Album+links fetch helper (fetch_albums_with_links)
- Provider dispatch polymorphism (list_provider_collections)
- Immutable _enrich_assets (no longer mutates in-place)
- Fix _format_assets return type + handler unpacking

Frontend:
- Fix 18+ hardcoded English strings → t() with new i18n keys (en + ru)
- Mobile "More" nav panel with provider filter and search
- Shared Button.svelte component (4 variants, 2 sizes)
- Shared ErrorBanner.svelte component (8 pages updated)
- SvelteKit goto() replacing window.location.href
- Dashboard grid fixed for 4 cards, paginator opacity consistency

Functionality:
- max_instances=1 on scheduler jobs (prevents duplicate events)
- Webhook provider in watcher (prevents error spam)
- Fix stale SQLModel reference in poller
- Gitea get_repo() direct API call
This commit is contained in:
2026-03-28 13:22:26 +03:00
parent 616b221c92
commit b803d004e1
65 changed files with 1934 additions and 1498 deletions
+17 -18
View File
@@ -5,8 +5,11 @@
import Card from '$lib/components/Card.svelte';
import IconPicker from '$lib/components/IconPicker.svelte';
import IconGridSelect from '$lib/components/IconGridSelect.svelte';
import { goto } from '$app/navigation';
import { providerTypeItems } from '$lib/grid-items';
import { getDescriptor, buildProviderFormDefaults } from '$lib/providers';
import Button from '$lib/components/Button.svelte';
import ErrorBanner from '$lib/components/ErrorBanner.svelte';
let form = $state(buildProviderFormDefaults());
let error = $state('');
@@ -16,7 +19,7 @@
async function testAndSave() {
const desc = descriptor;
if (!desc) { error = 'Select a provider type'; return; }
if (!desc) { error = t('providers.selectType'); return; }
const { config, error: buildError } = desc.buildConfig(form, false);
if (buildError) { error = t(buildError); snackError(error); return; }
@@ -32,22 +35,22 @@
if (!result.ok) {
await api(`/providers/${provider.id}`, { method: 'DELETE' }).catch(() => {});
createdId = null;
error = result.message || 'Connection test failed';
error = result.message || t('providers.testFailed');
snackError(error);
} else {
snackSuccess(t('snack.providerSaved'));
window.location.href = '/providers';
goto('/providers');
}
} catch (e: any) {
if (createdId) await api(`/providers/${createdId}`, { method: 'DELETE' }).catch(() => {});
error = e.message || 'Test failed'; snackError(error);
error = e.message || t('providers.testFailed'); snackError(error);
}
finally { testing = false; }
}
async function saveWithoutTest() {
const desc = descriptor;
if (!desc) { error = 'Select a provider type'; return; }
if (!desc) { error = t('providers.selectType'); return; }
const { config, error: buildError } = desc.buildConfig(form, false);
if (buildError) { error = t(buildError); snackError(error); return; }
@@ -58,8 +61,8 @@
body: JSON.stringify({ type: form.type, name: form.name || desc.defaultName, icon: form.icon, config }),
});
snackSuccess(t('snack.providerSaved'));
window.location.href = '/providers';
} catch (e: any) { error = e.message || 'Save failed'; snackError(error); }
goto('/providers');
} catch (e: any) { error = e.message || t('common.saveFailed'); snackError(error); }
finally { saving = false; }
}
</script>
@@ -112,22 +115,18 @@
</div>
{/each}
{#if error}
<p class="text-sm text-[var(--color-error-fg)]">{error}</p>
{/if}
<ErrorBanner message={error} />
<div class="flex gap-3 pt-2">
<button onclick={testAndSave} disabled={testing || saving}
class="px-4 py-2 bg-[var(--color-primary)] text-[var(--color-primary-foreground)] rounded-md text-sm font-medium hover:opacity-90 disabled:opacity-50">
<Button onclick={testAndSave} disabled={testing || saving}>
{testing ? t('providers.connecting') : t('providers.testAndSave')}
</button>
<button onclick={saveWithoutTest} disabled={testing || saving}
class="px-4 py-2 bg-[var(--color-muted)] text-[var(--color-foreground)] rounded-md text-sm font-medium hover:opacity-80 disabled:opacity-50">
</Button>
<Button variant="secondary" onclick={saveWithoutTest} disabled={testing || saving}>
{saving ? t('common.loading') : t('providers.saveWithoutTest')}
</button>
<a href="/providers" class="px-4 py-2 bg-[var(--color-muted)] text-[var(--color-muted-foreground)] rounded-md text-sm font-medium hover:opacity-80">
</Button>
<Button variant="secondary" href="/providers">
{t('common.cancel')}
</a>
</Button>
</div>
</div>
</Card>