Skip to content
Snippets Groups Projects
Verified Commit 07b4144a authored by Ewen Le Bihan's avatar Ewen Le Bihan
Browse files

feat(URLs): redirect /@uid to /users/uid or /groups/uid

parent 2de62bbd
Branches
Tags v0.24.0
No related merge requests found
......@@ -5,6 +5,17 @@
export let white = false;
export let href = '';
function rewriteUrl(url: URL): string {
const segments = url.pathname.split('/').filter(Boolean);
if (['users', 'groups'].includes(segments[0]) && segments.length === 2) {
return new URL(url.pathname.replace(`/${segments[0]}/`, '/@'), url.origin)
.toString()
.replace(/\/$/, '');
}
return url.toString();
}
</script>
<GhostButton
......@@ -12,12 +23,12 @@
on:click={async () => {
try {
await navigator.share({
url: href || $page.url.toString(),
url: href || rewriteUrl($page.url),
title: document.title,
text: document.querySelector('meta[name=description]')?.getAttribute('content') ?? '',
});
} catch {
await navigator.clipboard.writeText(href || $page.url.toString());
await navigator.clipboard.writeText(href || rewriteUrl($page.url));
}
}}
darkShadow={white}
......
/* eslint-disable unicorn/filename-case */
import type { ParamMatcher } from '@sveltejs/kit';
export const match: ParamMatcher = (param) => /^@([\w-])+$/.test(param);
import { loadQuery } from '$lib/zeus';
import { redirect, type RequestHandler } from '@sveltejs/kit';
export const GET: RequestHandler = async ({ params, locals, fetch }) => {
const uid = params.entity!.replace('@', '');
let isGroup = false;
try {
await loadQuery(
{
group: [{ uid }, { __typename: true }],
},
{
fetch,
// eslint-disable-next-line @typescript-eslint/require-await
async parent() {
return {
me: undefined,
mobile: locals.mobile,
token: undefined,
};
},
}
);
isGroup = true;
} catch {}
const error = isGroup ? redirect(301, `/groups/${uid}`) : redirect(301, `/users/${uid}`);
throw error;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment