diff --git a/SharpbinV3.Client/src/lib/components/Navbar.svelte b/SharpbinV3.Client/src/lib/components/Navbar.svelte index c659173..d89b078 100644 --- a/SharpbinV3.Client/src/lib/components/Navbar.svelte +++ b/SharpbinV3.Client/src/lib/components/Navbar.svelte @@ -1,6 +1,7 @@
@@ -18,15 +22,15 @@ {$user.username} Logout
{:else}
- Login - Register
diff --git a/SharpbinV3.Client/src/lib/consts.ts b/SharpbinV3.Client/src/lib/consts.ts index 2daac53..c6a1648 100644 --- a/SharpbinV3.Client/src/lib/consts.ts +++ b/SharpbinV3.Client/src/lib/consts.ts @@ -72,3 +72,10 @@ export const displayNames: Record = { }; // only include langs that need special display names // e.g. cpp -> C++ or toml -> TOML, langs like java are automatically capitalized + +export const roles = { + 0: 'User', + 1: 'Moderator', + 255: 'Administrator', + 403: 'Banned' +} \ No newline at end of file diff --git a/SharpbinV3.Client/src/lib/utils/auth.ts b/SharpbinV3.Client/src/lib/utils/auth.ts index 1952e6c..e90540e 100644 --- a/SharpbinV3.Client/src/lib/utils/auth.ts +++ b/SharpbinV3.Client/src/lib/utils/auth.ts @@ -45,7 +45,11 @@ export async function refreshTokenIfNeeded() { const data = await res.json(); if (data.token && data.refreshToken) { setTokens(data.token, data.refreshToken); + } else { + clearTokens(); } + } else { + clearTokens(); } } } diff --git a/SharpbinV3.Client/src/lib/utils/misc.ts b/SharpbinV3.Client/src/lib/utils/misc.ts index 4a6a383..5672a6b 100644 --- a/SharpbinV3.Client/src/lib/utils/misc.ts +++ b/SharpbinV3.Client/src/lib/utils/misc.ts @@ -35,3 +35,116 @@ export function dateToRelativeString(date: Date, useSuffix = true): string { if (minutes > 0) return `${minutes} minute${minutes > 1 ? 's' : ''} ${suffix}`; return `${seconds} second${seconds !== 1 ? 's' : ''} ${suffix}`; } + +export function tooltip(node: HTMLElement, text: string) { + let tooltipEl: HTMLDivElement | null; + let caretEl: HTMLDivElement | null; + let showTimeout: ReturnType; + let hideTimeout: ReturnType; + + function createTooltip() { + const el = document.createElement('div'); + el.textContent = text; + el.className = + 'fixed z-50 rounded bg-neutral-800 px-2 py-1 text-sm text-white shadow-lg opacity-0 pointer-events-none transition-opacity duration-150'; + el.style.maxWidth = '90%'; + el.style.wordBreak = 'break-word'; + const lines = text.split('\n'); + if (lines.length > 1) { + el.innerHTML = ''; + lines.forEach((line, index) => { + const lineEl = document.createElement('div'); + lineEl.textContent = line; + el.appendChild(lineEl); + if (index < lines.length - 1) { + const br = document.createElement('br'); + el.appendChild(br); + } + }); + } + const caret = document.createElement('div'); + caret.style.position = 'absolute'; + caret.style.width = '0'; + caret.style.height = '0'; + caret.style.left = '50%'; + caret.style.transform = 'translateX(-50%)'; + caret.style.pointerEvents = 'none'; + caretEl = caret; + el.appendChild(caret); + document.body.appendChild(el); + tooltipEl = el; + } + + function positionTooltip() { + if (!tooltipEl || !caretEl) return; + const rect = node.getBoundingClientRect(); + const tooltipRect = tooltipEl.getBoundingClientRect(); + let top = rect.top - tooltipRect.height - 8 + window.scrollY; + let left = rect.left + rect.width / 2 - tooltipRect.width / 2 + window.scrollX; + let caretOnTop = false; + + if (top < window.scrollY) { + top = rect.bottom + 8 + window.scrollY; + caretOnTop = true; + } + if (left < 0) left = 8; + if (left + tooltipRect.width > window.innerWidth) + left = window.innerWidth - tooltipRect.width - 8; + + tooltipEl.style.top = `${top}px`; + tooltipEl.style.left = `${left}px`; + tooltipEl.style.position = 'absolute'; + + caretEl.style.top = caretOnTop ? '-7px' : ''; + caretEl.style.bottom = caretOnTop ? '' : '-7px'; + caretEl.style.borderLeft = '7px solid transparent'; + caretEl.style.borderRight = '7px solid transparent'; + if (caretOnTop) { + caretEl.style.borderBottom = '7px solid #27272a'; + caretEl.style.borderTop = ''; + } else { + caretEl.style.borderTop = '7px solid #27272a'; + caretEl.style.borderBottom = ''; + } + } + + function mouseOver() { + clearTimeout(hideTimeout); + showTimeout = setTimeout(() => { + createTooltip(); + positionTooltip(); + requestAnimationFrame(() => { + if (tooltipEl) tooltipEl.style.opacity = '1'; + }); + }, 200); + } + + function mouseOut() { + clearTimeout(showTimeout); + if (tooltipEl) { + tooltipEl.style.opacity = '0'; + hideTimeout = setTimeout(() => { + if (tooltipEl) { + tooltipEl.remove(); + tooltipEl = null; + caretEl = null; + } + }, 150); + } + } + + node.addEventListener('mouseover', mouseOver); + node.addEventListener('mouseout', mouseOut); + window.addEventListener('scroll', mouseOut, true); + window.addEventListener('resize', mouseOut, true); + + return { + destroy() { + node.removeEventListener('mouseover', mouseOver); + node.removeEventListener('mouseout', mouseOut); + window.removeEventListener('scroll', mouseOut, true); + window.removeEventListener('resize', mouseOut, true); + if (tooltipEl) tooltipEl.remove(); + } + }; +} \ No newline at end of file diff --git a/SharpbinV3.Client/src/routes/+page.svelte b/SharpbinV3.Client/src/routes/+page.svelte index e5c7335..2744857 100644 --- a/SharpbinV3.Client/src/routes/+page.svelte +++ b/SharpbinV3.Client/src/routes/+page.svelte @@ -141,6 +141,7 @@ spellcheck="false" autocomplete="off" bind:value={title} + maxlength={data.options.maxTitleLength ?? 500} /> import type { PageData } from './$types'; import { FileBox, Eye, User, CalendarDays, CalendarOff, Code } from '@lucide/svelte/icons/index'; - import { formatBytes, extractDateFromUUIDv7, dateToRelativeString } from '$lib/utils/misc'; + import { + formatBytes, + extractDateFromUUIDv7, + dateToRelativeString, + tooltip + } from '$lib/utils/misc'; import { displayNames } from '$lib/consts'; import { fade } from 'svelte/transition'; import { resolve } from '$app/paths'; export let data: PageData; - let fileSizeHovered = false; function addLineNumbers(html: string): string { const match = html.match(/[\s\S]*?([\s\S]*?)<\/code><\/pre>/); @@ -31,26 +35,19 @@ class="max-h-[80vh] w-[95%] max-w-5xl rounded border-2 border-neutral-800 bg-neutral-900 p-4" > {#if data.paste} -

{data.paste.title || 'Untitled Paste'}

+

+ {data.paste.title || 'Untitled Paste'} +

-
(fileSizeHovered = true)} - on:mouseleave={() => (fileSizeHovered = false)} - > +
{formatBytes(data.paste.size)}
- {#if fileSizeHovered} -
- True Size: {formatBytes(data.paste.trueSize)} -
- {/if}
diff --git a/SharpbinV3.Client/src/routes/login/+page.svelte b/SharpbinV3.Client/src/routes/login/+page.svelte index d6a87a1..9e479d8 100644 --- a/SharpbinV3.Client/src/routes/login/+page.svelte +++ b/SharpbinV3.Client/src/routes/login/+page.svelte @@ -17,6 +17,12 @@ setTokens(data.result.token, data.result.refreshToken); startTokenRefreshInterval(); } + const urlParams = new URLSearchParams(window.location.search); + const returnUrl = urlParams.get('return'); + if (returnUrl) { + window.location.href = returnUrl; + return; + } window.location.href = '/'; } else { error = 'Invalid username or password'; diff --git a/SharpbinV3.Client/src/routes/register/+page.svelte b/SharpbinV3.Client/src/routes/register/+page.svelte index 590becc..ae616d7 100644 --- a/SharpbinV3.Client/src/routes/register/+page.svelte +++ b/SharpbinV3.Client/src/routes/register/+page.svelte @@ -49,7 +49,13 @@ startTokenRefreshInterval(); goto(resolve('/')); } else { - goto(resolve('/login')); + const urlParams = new URLSearchParams(window.location.search); + const returnUrl = urlParams.get('return'); + if (returnUrl) { + goto(resolve('/login') + `?return=${encodeURIComponent(returnUrl)}`); + } else { + goto(resolve('/login')); + } } } else { error = 'Registration failed'; diff --git a/SharpbinV3.Client/src/routes/user/[username]/+page.server.ts b/SharpbinV3.Client/src/routes/user/[username]/+page.server.ts index f6269c7..18d8524 100644 --- a/SharpbinV3.Client/src/routes/user/[username]/+page.server.ts +++ b/SharpbinV3.Client/src/routes/user/[username]/+page.server.ts @@ -1,5 +1,6 @@ import type { PageServerLoad } from './$types'; import { getServerToken } from '$lib/utils/auth'; +import type { User } from '$lib/types/user'; export const load: PageServerLoad = async ({ params, fetch, cookies }) => { const { username } = params; @@ -9,6 +10,7 @@ export const load: PageServerLoad = async ({ params, fetch, cookies }) => { Authorization: token ? `Bearer ${token}` : '' } }); - const user = await res.json(); + + const user = await res.json() as User; return { user }; } \ No newline at end of file diff --git a/SharpbinV3.Client/src/routes/user/[username]/+page.svelte b/SharpbinV3.Client/src/routes/user/[username]/+page.svelte index 6ecf29b..04e3650 100644 --- a/SharpbinV3.Client/src/routes/user/[username]/+page.svelte +++ b/SharpbinV3.Client/src/routes/user/[username]/+page.svelte @@ -1,7 +1,85 @@ \ No newline at end of file + export let data: PageData; + + +
+
+

+ {#if data.user.roles.includes(1) || data.user.roles.includes(255)} + {#if Math.max(...data.user.roles.filter((r) => r !== 403)) !== -Infinity} + r !== 403)) as keyof typeof roles + ]} + > + + + {:else} + + + + {/if} + {:else if data.user.roles.includes(403)} + + + + + {:else} + + + + {/if} + {data.user.displayName || data.user.username} +

+ {#if data.user.displayName && data.user.displayName !== data.user.username} +
+ + {data.user.username} +
+ {/if} +
+
+ + + Joined {extractDateFromUUIDv7(data.user.uuid)?.toLocaleDateString() ?? 'Unknown'} + +
+
+ + {data.user.pastes?.length} paste{data.user.pastes?.length !== 1 ? 's' : ''} +
+
+
+ {#if data.user.pastes && data.user.pastes.length > 0} + {#each data.user.pastes as paste} + + + {paste.title || 'Untitled Paste'} + +

+ Created on {extractDateFromUUIDv7(paste.uuid)?.toLocaleDateString() ?? 'Unknown Date'} +

+
+ {/each} + {:else} +

This user has not created any pastes yet.

+ {/if} +
+
+
diff --git a/SharpbinV3/Controllers/UserController.cs b/SharpbinV3/Controllers/UserController.cs index 86b2d60..44514cd 100644 --- a/SharpbinV3/Controllers/UserController.cs +++ b/SharpbinV3/Controllers/UserController.cs @@ -45,9 +45,15 @@ namespace SharpbinV3.Server.Controllers var isAuthenticatedUser = HttpContext.User?.FindFirst("UUID")?.Value == user.UUID.ToString(); var pasteQuery = _db.Pastes.AsNoTracking().Where(p => p.AuthorUUID == user.UUID); if (!isAuthenticatedUser) - pasteQuery = pasteQuery.Where(p => p.Visibility == 0); + { + if (user.Visibility == 1 || user.Visibility == 2) + return NotFound(); + else + pasteQuery = pasteQuery.Where(p => p.Visibility == 0); + } var pastes = await pasteQuery.Select(p => new { + p.ID, p.UUID, p.Title, p.Syntax, @@ -79,6 +85,7 @@ namespace SharpbinV3.Server.Controllers user.Username, user.UUID, user.DisplayName, + user.Roles, Pastes = pastes }; }