From 0c6e3bf38f2c93c6d1deafdc799dc335e73fd0ac Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 23 Feb 2025 20:02:40 +0100 Subject: [PATCH 1/2] feat(frontend): show closed reports button, add some alerts for auth --- Foxnouns.Frontend/src/lib/i18n/locales/en.json | 3 ++- .../src/routes/admin/reports/+page.server.ts | 2 +- .../src/routes/admin/reports/+page.svelte | 13 +++++++++++++ .../src/routes/auth/log-in/+page.server.ts | 6 +++--- .../src/routes/auth/log-in/+page.svelte | 2 ++ .../src/routes/auth/welcome/+page.server.ts | 2 +- .../src/routes/settings/+layout.server.ts | 2 +- 7 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Foxnouns.Frontend/src/lib/i18n/locales/en.json b/Foxnouns.Frontend/src/lib/i18n/locales/en.json index 55696dd..700b8e2 100644 --- a/Foxnouns.Frontend/src/lib/i18n/locales/en.json +++ b/Foxnouns.Frontend/src/lib/i18n/locales/en.json @@ -321,6 +321,7 @@ "required": "Required" }, "alert": { - "auth-method-remove-success": "Successfully unlinked account!" + "auth-method-remove-success": "Successfully unlinked account!", + "auth-required": "You must log in to access this page." } } diff --git a/Foxnouns.Frontend/src/routes/admin/reports/+page.server.ts b/Foxnouns.Frontend/src/routes/admin/reports/+page.server.ts index a88121e..c2149c1 100644 --- a/Foxnouns.Frontend/src/routes/admin/reports/+page.server.ts +++ b/Foxnouns.Frontend/src/routes/admin/reports/+page.server.ts @@ -19,5 +19,5 @@ export const load = async ({ url, fetch, cookies }) => { fetch, cookies, }); - return { reports, url: url.toString(), byReporter, byTarget, before, after }; + return { reports, url: url.toString(), includeClosed, byReporter, byTarget, before, after }; }; diff --git a/Foxnouns.Frontend/src/routes/admin/reports/+page.svelte b/Foxnouns.Frontend/src/routes/admin/reports/+page.svelte index 3b76ce7..5021846 100644 --- a/Foxnouns.Frontend/src/routes/admin/reports/+page.svelte +++ b/Foxnouns.Frontend/src/routes/admin/reports/+page.svelte @@ -18,6 +18,14 @@ return url.toString(); }; + const addClosed = () => { + const url = new URL(data.url); + if (!data.includeClosed) url.searchParams.set("include-closed", "true"); + else url.searchParams.delete("include-closed"); + + return url.toString(); + }; + const addTarget = (id: string | null) => { const url = new URL(data.url); if (id) url.searchParams.set("by-target", id); @@ -56,6 +64,11 @@ {#if data.byReporter}
  • Filtering by reporter (clear)
  • {/if} + {#if data.includeClosed} +
  • Showing all reports (only show open reports)
  • + {:else} +
  • Showing open reports (show all reports)
  • + {/if} {#if data.before} diff --git a/Foxnouns.Frontend/src/routes/auth/log-in/+page.server.ts b/Foxnouns.Frontend/src/routes/auth/log-in/+page.server.ts index 9c6bf25..579ea67 100644 --- a/Foxnouns.Frontend/src/routes/auth/log-in/+page.server.ts +++ b/Foxnouns.Frontend/src/routes/auth/log-in/+page.server.ts @@ -2,15 +2,15 @@ import { isRedirect, redirect } from "@sveltejs/kit"; import { apiRequest } from "$api"; import type { AuthResponse, AuthUrls } from "$api/models/auth"; -import { setToken } from "$lib"; +import { alertKey, setToken } from "$lib"; import ApiError, { ErrorCode } from "$api/error"; -export const load = async ({ fetch, parent }) => { +export const load = async ({ fetch, parent, url }) => { const parentData = await parent(); if (parentData.meUser) redirect(303, `/@${parentData.meUser.username}`); const urls = await apiRequest("POST", "/auth/urls", { fetch, isInternal: true }); - return { urls }; + return { urls, alertKey: alertKey(url) }; }; export const actions = { diff --git a/Foxnouns.Frontend/src/routes/auth/log-in/+page.svelte b/Foxnouns.Frontend/src/routes/auth/log-in/+page.svelte index c6c47a9..ee4d040 100644 --- a/Foxnouns.Frontend/src/routes/auth/log-in/+page.svelte +++ b/Foxnouns.Frontend/src/routes/auth/log-in/+page.svelte @@ -3,6 +3,7 @@ import { t } from "$lib/i18n"; import { enhance } from "$app/forms"; import ErrorAlert from "$components/ErrorAlert.svelte"; + import UrlAlert from "$components/URLAlert.svelte"; type Props = { data: PageData; form: ActionData }; let { data, form }: Props = $props(); @@ -13,6 +14,7 @@
    +
    {#if form?.error} diff --git a/Foxnouns.Frontend/src/routes/auth/welcome/+page.server.ts b/Foxnouns.Frontend/src/routes/auth/welcome/+page.server.ts index 88baf97..4b76df2 100644 --- a/Foxnouns.Frontend/src/routes/auth/welcome/+page.server.ts +++ b/Foxnouns.Frontend/src/routes/auth/welcome/+page.server.ts @@ -2,5 +2,5 @@ import { redirect } from "@sveltejs/kit"; export const load = async ({ parent }) => { const { meUser } = await parent(); - if (!meUser) redirect(303, "/auth/log-in"); + if (!meUser) redirect(303, "/auth/log-in?alert=auth-required"); }; diff --git a/Foxnouns.Frontend/src/routes/settings/+layout.server.ts b/Foxnouns.Frontend/src/routes/settings/+layout.server.ts index fe2eaa3..95228f0 100644 --- a/Foxnouns.Frontend/src/routes/settings/+layout.server.ts +++ b/Foxnouns.Frontend/src/routes/settings/+layout.server.ts @@ -2,7 +2,7 @@ import { redirect } from "@sveltejs/kit"; export const load = async ({ parent }) => { const data = await parent(); - if (!data.meUser) redirect(303, "/auth/log-in"); + if (!data.meUser) redirect(303, "/auth/log-in?alert=auth-required"); return { user: data.meUser!, token: data.token! }; }; From c8e4078b35f7be2c1d3a57a85b26d4a2b8b2c9cf Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 23 Feb 2025 21:42:01 +0100 Subject: [PATCH 2/2] fix: show 404 page if /api/v2/meta/page/{page} can't find a file --- Foxnouns.Backend/Controllers/MetaController.cs | 11 +++++++++-- Foxnouns.Backend/ExpectedError.cs | 1 + Foxnouns.Frontend/src/hooks.server.ts | 8 ++------ Foxnouns.Frontend/src/lib/api/error.ts | 1 + Foxnouns.Frontend/src/lib/errorCodes.ts | 2 ++ Foxnouns.Frontend/src/lib/i18n/locales/en.json | 3 ++- 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Foxnouns.Backend/Controllers/MetaController.cs b/Foxnouns.Backend/Controllers/MetaController.cs index cf86d55..1f00a7a 100644 --- a/Foxnouns.Backend/Controllers/MetaController.cs +++ b/Foxnouns.Backend/Controllers/MetaController.cs @@ -58,8 +58,15 @@ public partial class MetaController(Config config) : ApiControllerBase } string path = Path.Join(Directory.GetCurrentDirectory(), "static-pages", $"{page}.md"); - string text = await System.IO.File.ReadAllTextAsync(path, ct); - return Ok(text); + try + { + string text = await System.IO.File.ReadAllTextAsync(path, ct); + return Ok(text); + } + catch (FileNotFoundException) + { + throw new ApiError.NotFound("Page not found", code: ErrorCode.PageNotFound); + } } [HttpGet("/api/v2/coffee")] diff --git a/Foxnouns.Backend/ExpectedError.cs b/Foxnouns.Backend/ExpectedError.cs index 6a704e2..647688b 100644 --- a/Foxnouns.Backend/ExpectedError.cs +++ b/Foxnouns.Backend/ExpectedError.cs @@ -164,6 +164,7 @@ public enum ErrorCode GenericApiError, UserNotFound, MemberNotFound, + PageNotFound, AccountAlreadyLinked, LastAuthMethod, InvalidReportTarget, diff --git a/Foxnouns.Frontend/src/hooks.server.ts b/Foxnouns.Frontend/src/hooks.server.ts index c67d258..35a0048 100644 --- a/Foxnouns.Frontend/src/hooks.server.ts +++ b/Foxnouns.Frontend/src/hooks.server.ts @@ -21,12 +21,8 @@ Sentry.init({ }); export const handleError: HandleServerError = async ({ error, status, message }) => { - // as far as i know, sentry IDs are just UUIDs with the dashes removed. use those here as well - let id = crypto.randomUUID().replaceAll("-", ""); - if (error instanceof ApiError) { return { - error_id: id, status: error.raw?.status || status, message: error.raw?.message || "Unknown error", code: error.code, @@ -34,11 +30,11 @@ export const handleError: HandleServerError = async ({ error, status, message }) } if (status >= 400 && status <= 499) { - return { error_id: id, status, message, code: ErrorCode.GenericApiError }; + return { status, message, code: ErrorCode.GenericApiError }; } // client errors and backend API errors just clog up sentry, so we don't send those. - id = Sentry.captureException(error, { + const id = Sentry.captureException(error, { mechanism: { type: "sveltekit", handled: false, diff --git a/Foxnouns.Frontend/src/lib/api/error.ts b/Foxnouns.Frontend/src/lib/api/error.ts index 0b05d69..e893a86 100644 --- a/Foxnouns.Frontend/src/lib/api/error.ts +++ b/Foxnouns.Frontend/src/lib/api/error.ts @@ -43,6 +43,7 @@ export enum ErrorCode { MemberNotFound = "MEMBER_NOT_FOUND", AccountAlreadyLinked = "ACCOUNT_ALREADY_LINKED", LastAuthMethod = "LAST_AUTH_METHOD", + PageNotFound = "PAGE_NOT_FOUND", // This code isn't actually returned by the API Non204Response = "(non 204 response)", } diff --git a/Foxnouns.Frontend/src/lib/errorCodes.ts b/Foxnouns.Frontend/src/lib/errorCodes.ts index b97b71b..8b8ef44 100644 --- a/Foxnouns.Frontend/src/lib/errorCodes.ts +++ b/Foxnouns.Frontend/src/lib/errorCodes.ts @@ -29,6 +29,8 @@ export default function errorDescription(t: TranslateFn, code: ErrorCode): strin return t("error.account-already-linked"); case ErrorCode.LastAuthMethod: return t("error.last-auth-method"); + case ErrorCode.PageNotFound: + return t("error.page-not-found"); case ErrorCode.Non204Response: return t("error.generic-error"); } diff --git a/Foxnouns.Frontend/src/lib/i18n/locales/en.json b/Foxnouns.Frontend/src/lib/i18n/locales/en.json index 700b8e2..0c2f958 100644 --- a/Foxnouns.Frontend/src/lib/i18n/locales/en.json +++ b/Foxnouns.Frontend/src/lib/i18n/locales/en.json @@ -120,7 +120,8 @@ "400-description": "Something went wrong with your request. This error should never land you on this page, so it's probably a bug.", "500-description": "Something went wrong on the server. Please try again later.", "unknown-status-description": "Something went wrong, but we're not sure what. Please try again.", - "error-id": "If you report this error to the developers, please give them this ID:" + "error-id": "If you report this error to the developers, please give them this ID:", + "page-not-found": "No page exists at this URL." }, "settings": { "general-information-tab": "General information",