feat: reports; improved+simplifed modals; extractError funcction;

refactor: change jwt claims to lowercase; use response dtos for sensitive data
fix: nullify user on token clears; change appsettings.dev View_Internal_API_Key
This commit is contained in:
2026-01-14 19:41:52 -05:00
parent 1da88dba4e
commit 67447d51ae
39 changed files with 2291 additions and 454 deletions
@@ -0,0 +1,43 @@
import type { PageServerLoad } from './$types';
import { error } from '@sveltejs/kit';
import { env } from '$env/dynamic/private';
import { getServerToken } from '$lib/utils/auth';
import type { Report } from '$lib/types/report';
const API_URL = env.VITE_API_URL ?? 'http://localhost:5050';
function decodeJwtRoles(token?: string | null): number[] {
try {
if (!token) return [];
const payload = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString());
const roles = payload?.role;
if (Array.isArray(roles)) {
return roles.map((r: string) => parseInt(r, 10)).filter((r: number) => !isNaN(r));
}
return [];
} catch {
return [];
}
}
export const load: PageServerLoad = async ({ fetch, cookies, params, url }) => {
const token = getServerToken(cookies);
if (!token) throw error(401, 'Login required');
const roles = decodeJwtRoles(token);
const canEdit = roles.includes(1) || roles.includes(255);
const res = await fetch(`${API_URL}/api/report/${params.reportId}`, {
headers: { Authorization: `Bearer ${token}` }
});
if (!res.ok) {
const { message } = await res.json().catch(() => ({}));
throw error(res.status, message ?? 'Request failed');
}
const report = (await res.json()) as Report;
const optionsRes = await fetch(`${API_URL}/api/report/options`);
const options = optionsRes.ok ? await optionsRes.json() : { types: [], statuses: [] };
return { report, url: url.href, canEdit, options };
};
@@ -0,0 +1,165 @@
<script lang="ts">
import type { PageData } from './$types';
import { resolve } from '$app/paths';
import { reportStatusLabels, reportTargetLabels, reportTypeLabels } from '$lib/types/report';
import { tooltip } from '$lib/utils/misc';
import { getToken } from '$lib/utils/auth';
export let data: PageData;
const report = data.report;
let saving = false;
let saveError = '';
const statusOptions = data.options?.statuses ?? [];
const typeOptions = data.options?.types ?? [];
let statusValue = reportStatusLabels[report.status] ?? '';
let typeValue = reportTypeLabels[report.type] ?? '';
async function updateReport() {
saveError = '';
saving = true;
const token = getToken();
const res = await fetch(`/api/report/${report.reportID}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
...(token ? { Authorization: `Bearer ${token}` } : {})
},
body: JSON.stringify({ status: statusValue, type: typeValue })
});
if (!res.ok) {
const { message } = await res.json().catch(() => ({}));
saveError = message ?? 'Failed to update report.';
saving = false;
return;
}
const updated = await res.json();
report.status = updated.status ?? report.status;
report.type = updated.type ?? report.type;
report.updatedAt = updated.updatedAt ?? report.updatedAt;
report.description = updated.description ?? report.description;
saving = false;
}
</script>
<svelte:head>
<title>Report #{report.reportID} - Sharpbin</title>
</svelte:head>
<main
class="flex min-h-[calc(100vh-60px)] w-full flex-col items-center justify-center pt-5 pb-5 text-white"
>
<div class="mx-auto w-[95%] max-w-4xl rounded border-2 border-neutral-800 bg-neutral-900 p-6">
<h1 class="mb-6 text-center text-3xl font-bold">Report #{report.reportID}</h1>
<div class="space-y-4">
<div class="flex flex-wrap items-center gap-3 text-neutral-300">
<span class="rounded bg-neutral-800 px-3 py-1">
{reportTypeLabels[report.type] ?? 'Other'}
</span>
<span class="rounded bg-neutral-800 px-3 py-1">
{reportStatusLabels[report.status] ?? 'Open'}
</span>
<span class="rounded bg-neutral-800 px-3 py-1">
{reportTargetLabels[report.targetType] ?? 'Unknown'}
</span>
</div>
<div class="space-y-2 text-sm text-neutral-400">
<div>
Reporter:
{#if report.reporterUsername}
<a
class="text-neutral-200 hover:text-white hover:underline"
href={resolve(`/user/${report.reporterUsername}`)}
use:tooltip={report.reporterUUID}
>
{report.reporterDisplayName || report.reporterUsername}
</a>
{:else}
<span>{report.reporterUUID}</span>
{/if}
</div>
{#if report.pasteId}
<div>
Target Paste:
<a
class="text-neutral-200 hover:text-white hover:underline"
href={resolve(`/${report.pasteId}`)}
>
{report.pasteTitle !== '' ? report.pasteTitle : report.pasteId}
</a>
</div>
{/if}
{#if report.userUUID}
<div>
Target User:
{#if report.targetUsername}
<a
class="text-neutral-200 hover:text-white hover:underline"
href={resolve(`/user/${report.targetUsername}`)}
use:tooltip={report.userUUID}
>
{report.targetDisplayName || report.targetUsername}
</a>
{:else}
<span>{report.userUUID}</span>
{/if}
</div>
{/if}
<div>Created: {new Date(report.createdAt * 1000).toLocaleString()}</div>
{#if report.updatedAt}
<div>Updated: {new Date(report.updatedAt * 1000).toLocaleString()}</div>
{/if}
</div>
{#if data.canEdit}
<div class="mt-4 space-y-3">
<div class="grid grid-cols-1 gap-3 md:grid-cols-2">
<div class="flex flex-col gap-2">
<label class="text-sm text-neutral-400" for="report-update-type">Type</label>
<select
id="report-update-type"
class="rounded border border-neutral-700 bg-neutral-800 p-2"
bind:value={typeValue}
>
{#each typeOptions as t (t)}
<option value={t}>{t}</option>
{/each}
</select>
</div>
<div class="flex flex-col gap-2">
<label class="text-sm text-neutral-400" for="report-update-status">Status</label>
<select
id="report-update-status"
class="rounded border border-neutral-700 bg-neutral-800 p-2"
bind:value={statusValue}
>
{#each statusOptions as s (s)}
<option value={s}>{s}</option>
{/each}
</select>
</div>
</div>
<div class="flex items-center gap-3">
<button
class="rounded bg-neutral-700 px-4 py-2 text-sm font-medium transition-colors hover:bg-neutral-600 disabled:opacity-60"
on:click={updateReport}
disabled={saving}
>
{saving ? 'Saving...' : 'Update Report'}
</button>
{#if saveError}
<span class="text-sm text-red-300">{saveError}</span>
{/if}
</div>
</div>
{/if}
{#if report.description}
<div class="rounded border border-neutral-700 bg-neutral-800 p-4 text-neutral-200">
{report.description}
</div>
{/if}
</div>
</div>
</main>