diff --git a/src/lib/i18n/en.json b/src/lib/i18n/en.json index 1d7ed7d2..a11c4071 100644 --- a/src/lib/i18n/en.json +++ b/src/lib/i18n/en.json @@ -716,7 +716,8 @@ "settingsImport": "Successfully imported settings", "settingsImportWarning": "The imported settings don't seem valid. Are you sure you want to import this?", "userLoading": "Still loading your user data...", - "lemmyDonate": "Your account's instance runs Lemmy, and the developers are requesting donations. They are able to develop Lemmy as an open source platform, free of tracking and ads, thanks to the generosity of its users.\n\nAnnually, they ask you to consider donating to support their work, and allow them to continue maintaining and improving Lemmy.\n\n[Donate](https://join-lemmy.org/donate)\n\n*Note: this is a donation to Lemmy, not Photon.*" + "lemmyDonate": "Your account's instance runs Lemmy, and the developers are requesting donations. They are able to develop Lemmy as an open source platform, free of tracking and ads, thanks to the generosity of its users.\n\nAnnually, they ask you to consider donating to support their work, and allow them to continue maintaining and improving Lemmy.\n\n[Donate](https://join-lemmy.org/donate)\n\n*Note: this is a donation to Lemmy, not Photon.*", + "successDiagnostic": "Thank you for submitting this information!" }, "settings": { "title": "Settings", @@ -727,6 +728,14 @@ "export": "Export", "import": "Import", "reset": "Reset", + "usageInfo": { + "title": "Send usage info", + "body": "You can help the development of Photon by sending information about how you use the app. This is entirely opt-in, and nothing is shared when you don't explicitly ask to.\n\nThe data under 'payload' will be sent to `https://{{hostname}}/api/diagnostic` via `POST`, and such data will be handled by the owner(s) of **{{hostname}}**.", + "profiles": "Profile count and instances (no usernames)", + "settings": "Your chosen photon settings", + "device": "Info about your device", + "payload": "Payload" + }, "navigation": { "title": "Navigation", "autoHide": { diff --git a/src/routes/api/diagnostic/+server.ts b/src/routes/api/diagnostic/+server.ts new file mode 100644 index 00000000..9bb994ef --- /dev/null +++ b/src/routes/api/diagnostic/+server.ts @@ -0,0 +1,26 @@ +import type { RequestHandler } from '@sveltejs/kit' +import { env as envPrivate } from '$env/dynamic/private' + +// this is a very rudimentary 'security' system. +export const POST: RequestHandler = async ({ request, getClientAddress }) => { + const body = await request.json() + + const payload = { + profiles: body.profiles, + settings: body.settings, + device: body.device, + } + + await fetch(envPrivate.PRIVATE_DIAGNOSTICS_URL, { + body: JSON.stringify(payload), + method: 'POST', + headers: { + Authorization: envPrivate.PRIVATE_DIAGNOSTIC_KEY, + 'Content-Type': 'application/json', + // so that any upstream diagnostic server can handle abuse + 'x-forwarded-for': getClientAddress(), + }, + }) + + return new Response(JSON.stringify({ success: true, storedData: payload })) +} diff --git a/src/routes/settings/+page.svelte b/src/routes/settings/+page.svelte index 94f7a1a7..54896d1c 100644 --- a/src/routes/settings/+page.svelte +++ b/src/routes/settings/+page.svelte @@ -36,6 +36,7 @@ Bars2, Bars3, BarsArrowDown, + Beaker, BookOpen, BugAnt, Calendar, @@ -70,9 +71,63 @@ import Section from './Section.svelte' import Setting from './Setting.svelte' import ToggleSetting from './ToggleSetting.svelte' + import { page } from '$app/state' + import Markdown from '$lib/components/markdown/Markdown.svelte' + let importing = $state(false) let importText = $state('') + let diagnostic = $state({ + open: false, + profile: false, + settings: false, + device: false, + }) + + let diagPayload = $derived(calculatePayload(diagnostic)) + + function calculatePayload(args: { + profile: boolean + settings: boolean + device: boolean + }) { + // eslint-disable-next-line + let payload: Record = {} + + if (args.profile) { + payload.profiles = { + count: profile.meta.profiles.length, + instances: [profile.meta.profiles.map(i => i.instance)], + } + } + if (args.settings) { + payload.settings = $state.snapshot(settings) + } + if (args.device) { + payload.device = { + userAgent: navigator.userAgent, + windowSize: { + width: window.innerWidth, + height: window.innerHeight, + ratio: window.devicePixelRatio, + }, + language: navigator.language, + platform: navigator.platform, + } + } + + return payload + } + + async function submitDiagnostics() { + if (Object.keys(diagPayload).length == 0) return + + await fetch('/api/diagnostic', { + method: 'POST', + body: JSON.stringify(diagPayload), + }) + } + let localeMap: Map< string, { @@ -133,6 +188,40 @@ {/if} +{#if diagnostic.open} + +
{ + e.preventDefault() + submitDiagnostics() + diagnostic.open = false + toast({ content: $t('toast.successDiagnostic'), type: 'success' }) + }} + class="space-y-3" + > + +
+ + {$t('settings.usageInfo.profiles')} + + + {$t('settings.usageInfo.settings')} + + + {$t('settings.usageInfo.device')} + +
+      {JSON.stringify(diagPayload, undefined, 2)}
+    
+ + +
+{/if} +
{$t('settings.title')} {#snippet extended()} @@ -205,6 +294,19 @@ +{#if env.PUBLIC_DIAGNOSTICS.toLowerCase() == 'true'} + +{/if} +