From 5c9ccb3ab759526e84079f1c49abb87c995c7021 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 18 Mar 2022 16:59:58 -0500 Subject: Add customization page --- src/_admins.json | 3 + src/_backgrounds.json | 1 + src/hooks.ts | 5 +- src/lib/APITypes.d.ts | 1 + src/lib/Tooltip.svelte | 6 +- src/routes/loggedin.ts | 3 +- src/routes/profile.svelte | 79 --------------- src/routes/profile/index.svelte | 218 ++++++++++++++++++++++++++++++++++++++++ src/routes/profile/update.ts | 136 +++++++++++++++++++++++++ src/routes/verify.svelte | 8 +- src/routes/verify.ts | 33 +----- 11 files changed, 375 insertions(+), 118 deletions(-) create mode 100644 src/_admins.json create mode 100644 src/_backgrounds.json delete mode 100644 src/routes/profile.svelte create mode 100644 src/routes/profile/index.svelte create mode 100644 src/routes/profile/update.ts (limited to 'src') diff --git a/src/_admins.json b/src/_admins.json new file mode 100644 index 0000000..b264374 --- /dev/null +++ b/src/_admins.json @@ -0,0 +1,3 @@ +[ + "6536bfed869548fd83a1ecd24cf2a0fd" +] \ No newline at end of file diff --git a/src/_backgrounds.json b/src/_backgrounds.json new file mode 100644 index 0000000..e3737b5 --- /dev/null +++ b/src/_backgrounds.json @@ -0,0 +1 @@ +["1.jpg","10.jpg","11.jpg","12.jpg","13.jpg","14.jpg","15.jpg","16.jpg","17.jpg","18.jpg","19.jpg","2.jpg","20.jpg","21.jpg","22.jpg","23.jpg","24.jpg","25.jpg","26.jpg","27.jpg","28.jpg","29.jpg","3.jpg","30.jpg","31.jpg","32.jpg","33.jpg","34.jpg","35.jpg","36.jpg","37.jpg","38.jpg","39.jpg","4.jpg","40.jpg","41.jpg","42.jpg","43.jpg","44.jpg","45.jpg","46.jpg","47.jpg","48.jpg","49.jpg","5.jpg","50.jpg","51.jpg","52.jpg","53.jpg","54.jpg","55.jpg","56.jpg","57.jpg","58.jpg","59.jpg","6.jpg","60.jpg","61.jpg","62.jpg","63.jpg","64.jpg","65.jpg","66.jpg","67.jpg","68.jpg","69.jpg","7.jpg","70.jpg","71.jpg","8.jpg","9.jpg"] \ No newline at end of file diff --git a/src/hooks.ts b/src/hooks.ts index 5b3464c..ac53658 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -9,6 +9,7 @@ export const handle: Handle = async ({ event, resolve }) => { const cookies = cookie.parse(event.request.headers.get('cookie') || '') // event.locals.userid = cookies.userid || uuid(); + console.log(cookies) event.locals.sid = cookies.sid const response = await resolve(event) @@ -40,7 +41,7 @@ export const externalFetch: ExternalFetch = async (request) => { request.headers.set('key', SKYBLOCK_STATS_API_KEY) } - console.log('request', request.url) + const response = await fetch(request) - return fetch(request) + return response } \ No newline at end of file diff --git a/src/lib/APITypes.d.ts b/src/lib/APITypes.d.ts index b0deee0..328f4cd 100644 --- a/src/lib/APITypes.d.ts +++ b/src/lib/APITypes.d.ts @@ -97,6 +97,7 @@ export interface CleanBasicProfile { export interface AccountCustomization { backgroundUrl?: string pack?: string + blurBackground?: boolean emoji?: string } diff --git a/src/lib/Tooltip.svelte b/src/lib/Tooltip.svelte index b354d42..14fa90d 100644 --- a/src/lib/Tooltip.svelte +++ b/src/lib/Tooltip.svelte @@ -4,9 +4,12 @@ A tooltip that works without requiring JavaScript to be enabled. When you hover or click on the element, it shows the content in a box above the element. --> + - + No tooltip! @@ -47,7 +50,6 @@ width: max-content; text-align: center; - max-width: 10em; cursor: auto; box-shadow: 0 0 1em 0.5em #0002; pointer-events: none; diff --git a/src/routes/loggedin.ts b/src/routes/loggedin.ts index 9eadf67..32dd6eb 100644 --- a/src/routes/loggedin.ts +++ b/src/routes/loggedin.ts @@ -20,12 +20,13 @@ export const get: RequestHandler = async ({ url }) => { return res.json() }) console.log(response) + if (response.ok) { return { status: 303, headers: { location: '/verify', - 'Set-Cookie': `sid=${response.session_id}; Max-Age=31536000000; Path=/; HttpOnly; SameSite=Strict` + 'Set-Cookie': `sid=${response.session_id}; Max-Age=31536000000; Path=/; HttpOnly` } } } diff --git a/src/routes/profile.svelte b/src/routes/profile.svelte deleted file mode 100644 index c88b8fe..0000000 --- a/src/routes/profile.svelte +++ /dev/null @@ -1,79 +0,0 @@ - - - - - -
- -
- {#if session && session._id} - - {/if} -

Customize Profile

- - {#if player && player.player} - View profile - {:else} -

No linked Minecraft account

- {/if} -

- Pack: -

-
- - diff --git a/src/routes/profile/index.svelte b/src/routes/profile/index.svelte new file mode 100644 index 0000000..5fc937f --- /dev/null +++ b/src/routes/profile/index.svelte @@ -0,0 +1,218 @@ + + + + + +
+ +
+ {#if session && session._id} + + {/if} +

Customize Profile

+ + +

+ {#if error} + {error} + {:else if loading} + Loading... + {/if} +

+ + View profile +

+ + +

+

+ + +

+ {#if isDonator} + + + + +

Windows: win+.

+

Mobile: Emoji keyboard

+
+ +
+ {/if} +

Background

+
+ {#each backgroundNames as thisBackgroundName} + (backgroundName = thisBackgroundName)} + /> + {/each} +
+
+ + diff --git a/src/routes/profile/update.ts b/src/routes/profile/update.ts new file mode 100644 index 0000000..b906810 --- /dev/null +++ b/src/routes/profile/update.ts @@ -0,0 +1,136 @@ +import { API_URL } from '$lib/api' +import type { AccountSchema, SessionSchema } from '$lib/APITypes' +import type { RequestHandler } from '@sveltejs/kit' +import backgroundFileNames from '../../_backgrounds.json' +import donators from '../../_donators.json' +import admins from '../../_admins.json' +import type { JSONValue } from '@sveltejs/kit/types/internal' + +const emojiRegex = /^(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])$/ + +function isValidEmoji(emoji: string) { + const match = emojiRegex.exec(emoji) + return match && match[0] === emoji && match.index === 0 +} + +console.log(isValidEmoji('😎')) +console.log(isValidEmoji('😎')) +console.log(isValidEmoji('😎')) +console.log(isValidEmoji('😎')) + +export const patch: RequestHandler = async ({ request, locals }) => { + console.log('updating profile...') + if (locals.sid === undefined) { + return { + body: { ok: false, error: 'You are not logged in.' }, + status: 401, + } + } + if (!process.env.SKYBLOCK_STATS_API_KEY) { + return { + body: { ok: false, error: 'The SKYBLOCK_STATS_API_KEY environment variable is not set.' }, + status: 500, + } + } + const data = await request.json() + + console.log('sending request to get session') + const sessionResponse: { session: SessionSchema | null, account: AccountSchema | null } = await fetch(`${API_URL}accounts/session`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + uuid: locals.sid, + }), + }).then(r => r.json()) + if (!sessionResponse.session || !sessionResponse.account?.minecraftUuid) + return { + body: { ok: false, error: 'Invalid session.' }, + status: 401, + } + + const backgroundName = data.backgroundName + const pack = data.pack + const blurBackground = data.blurBackground + const emoji = data.emoji + + const isDonator = donators.find(d => d.uuid === sessionResponse.account?.minecraftUuid) !== undefined + console.log(sessionResponse.account?.minecraftUuid) + const isAdmin = admins.includes(sessionResponse.account?.minecraftUuid) + + if (typeof backgroundName !== 'undefined' && typeof backgroundName !== 'string') { + return { + body: { ok: false, error: 'Invalid background.' }, + status: 400, + } + } + if (typeof pack !== 'string') { + return { + body: { ok: false, error: 'Invalid pack.' }, + status: 400, + } + } + if (typeof blurBackground !== 'boolean') { + return { + body: { ok: false, error: 'Invalid blurBackground.' }, + status: 400, + } + } + console.log('emoji', emoji) + if (typeof emoji !== 'undefined' && typeof emoji !== 'string') { + console.log('a') + return { + body: { ok: false, error: 'Invalid emoji.' }, + status: 400, + } + } + + // prevent people from putting non-existent backgrounds + if (backgroundName && !backgroundFileNames.includes(backgroundName)) + return { + body: { ok: false, error: 'Invalid background.' }, + status: 400, + } + const backgroundUrl = backgroundName ? `/backgrounds/${backgroundName}` : undefined + + console.log('emoji?', emoji) + if (emoji) { + if (!isDonator && !isAdmin) + return { + body: { ok: false, error: 'You are not allowed to use emojis.' }, + status: 401, + } + if (!isValidEmoji(emoji)) + return { + body: { ok: false, error: 'Invalid emoji.' }, + status: 400, + } + } + + const updatedAccount: AccountSchema = { + discordId: sessionResponse.account.discordId, + customization: { + backgroundUrl, + pack, + blurBackground, + emoji, + }, + } + + const response = await fetch(`${API_URL}accounts/update`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + key: process.env.SKYBLOCK_STATS_API_KEY + }, + body: JSON.stringify(updatedAccount), + }).then(r => r.json()) + + console.log(response) + + + return { + body: { ok: true } as JSONValue, + } +} \ No newline at end of file diff --git a/src/routes/verify.svelte b/src/routes/verify.svelte index e900a5f..9675779 100644 --- a/src/routes/verify.svelte +++ b/src/routes/verify.svelte @@ -1,6 +1,7 @@