mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
this is a LOT, i should've commited in stages but oh well, from what i can remember:
- paste interactions (reactions)
- comments
- comment interactions (reactions)
- rich text editor
- move all report apis into report controller
- rename size to sizeStored
- rename trueSize to originalSize
- fixed compression on paste compression
- implemented compression threshold
- use longs instead of ints where possible in the entities
- improve record view + remove redundant check for existing view
103 lines
3.0 KiB
TypeScript
103 lines
3.0 KiB
TypeScript
import type { PageServerLoad } from './$types';
|
|
import { error } from '@sveltejs/kit';
|
|
import { apiUrl } from '$lib/server/api';
|
|
import { getServerToken } from '$lib/utils/auth';
|
|
import type { User } from '$lib/types/user';
|
|
import type { Paste } from '$lib/types/paste';
|
|
import type { Pagination } from '$lib/types/pagination';
|
|
import type { Report, ReportListResponse } from '$lib/types/report';
|
|
|
|
function decodeJwtRoles(token?: string | null): number {
|
|
try {
|
|
if (!token) return 0;
|
|
const payload = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString());
|
|
const roles = payload?.role;
|
|
if (typeof roles === 'number') return roles;
|
|
if (typeof roles === 'string') {
|
|
const parsed = parseInt(roles, 10);
|
|
return isNaN(parsed) ? 0 : parsed;
|
|
}
|
|
return 0;
|
|
} catch {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
function decodeJwtUuid(token?: string | null): string | null {
|
|
try {
|
|
if (!token) return null;
|
|
const payload = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString());
|
|
const uuid = payload?.uuid;
|
|
return typeof uuid === 'string' ? uuid : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export const load: PageServerLoad = async ({ params, fetch, cookies, url, parent, request }) => {
|
|
const token = getServerToken(cookies);
|
|
const { username } = params;
|
|
const { options } = await parent();
|
|
const jwtUuid = decodeJwtUuid(token);
|
|
const asGuest = url.searchParams.get('guest') === '1';
|
|
const headers = new Headers(request.headers);
|
|
if (token && !asGuest) {
|
|
headers.set('Authorization', `Bearer ${token}`);
|
|
}
|
|
|
|
const res = await fetch(`${apiUrl}/api/user/${username}`, {
|
|
headers: headers
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const { message } = await res.json().catch(() => ({}));
|
|
throw error(res.status, message ?? 'Request failed');
|
|
}
|
|
|
|
const { user, pastes, pagination, reports, reportsPagination } = (await res.json()) as {
|
|
user: User;
|
|
pastes: Paste[];
|
|
pagination: Pagination;
|
|
reports?: Report[];
|
|
reportsPagination?: Pagination;
|
|
};
|
|
|
|
const isOwner = jwtUuid ? user.uuid === jwtUuid : false;
|
|
let reportsTarget: ReportListResponse | null = null;
|
|
let reportsSubmitted: ReportListResponse | null = null;
|
|
const roles = token ? decodeJwtRoles(token) : 0;
|
|
const canModerate = (roles & 4) !== 0 || (roles & 2) !== 0;
|
|
|
|
if (isOwner && reports && reportsPagination) {
|
|
reportsSubmitted = {
|
|
reports,
|
|
pagination: reportsPagination
|
|
};
|
|
}
|
|
|
|
if (token && !asGuest && canModerate && (!reportsSubmitted || !isOwner)) {
|
|
headers.set('Authorization', `Bearer ${token}`);
|
|
const submittedRes = await fetch(`${apiUrl}/api/report/users/${user.uuid}/reports/submitted`, {
|
|
headers: headers
|
|
});
|
|
if (submittedRes.ok) reportsSubmitted = await submittedRes.json();
|
|
}
|
|
|
|
if (token && !asGuest && !isOwner && canModerate) {
|
|
headers.set('Authorization', `Bearer ${token}`);
|
|
const r = await fetch(`${apiUrl}/api/report/users/${user.uuid}/reports`, {
|
|
headers: headers
|
|
});
|
|
if (r.ok) reportsTarget = await r.json();
|
|
}
|
|
|
|
return {
|
|
user,
|
|
pastes: { pastes, pagination },
|
|
reportsTarget,
|
|
reportsSubmitted,
|
|
url: url.href,
|
|
authOptions: options
|
|
};
|
|
};
|