aboutsummaryrefslogtreecommitdiff
path: root/src/routes/profile/update/+server.ts
blob: 7190fb8efc55124299f178832c4a9d7d80a8bad3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { fetchApi } from '$lib/api'
import type { AccountSchema, SessionSchema } from '$lib/APITypes'
import backgroundFileNames from '../../../_backgrounds.json'
import donators from '../../../_donators.json'
import admins from '../../../_admins.json'
import env from '$lib/env'
import type { PageServerLoad } from '../$types'
import { error, json } from '@sveltejs/kit'

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
}


export const PATCH = (async ({ request, locals, platform }) => {
	if (locals.sid === undefined) {
		throw error(401, 'You are not logged in.')
	}
	const key = env(platform).SKYBLOCK_STATS_API_KEY
	if (!key) {
		throw error(500, 'The SKYBLOCK_STATS_API_KEY environment variable is not set.')
	}
	const data = await request.json()

	const sessionResponse: { session: SessionSchema | null, account: AccountSchema | null } = await fetchApi(`accounts/session`, fetch, {
		method: 'POST',
		headers: {
			'Content-Type': 'application/json',
		},
		body: JSON.stringify({
			uuid: locals.sid,
		}),
	}).then(r => r.json())
	if (!sessionResponse.session || !sessionResponse.account?.minecraftUuid)
		throw error(401, 'Invalid session.')

	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
	const isAdmin = admins.includes(sessionResponse.account?.minecraftUuid)

	if (typeof backgroundName !== 'undefined' && typeof backgroundName !== 'string') {
		throw error(400, 'Invalid background.')
	}
	if (typeof pack !== 'string') {
		throw error(400, 'Invalid pack.')
	}
	if (typeof blurBackground !== 'boolean') {
		throw error(400, 'Invalid blurBackground.')
	}
	if (typeof emoji !== 'undefined' && typeof emoji !== 'string') {
		throw error(400, 'Invalid emoji.')
	}

	// prevent people from putting non-existent backgrounds
	if (backgroundName && !backgroundFileNames.includes(backgroundName))
		throw error(400, 'Invalid background.')
	const backgroundUrl = backgroundName ? `/backgrounds/${backgroundName}` : undefined

	if (emoji) {
		if (!isDonator && !isAdmin)
			throw error(401, 'You are not allowed to use emojis.')
		if (!isValidEmoji(emoji))
			throw error(400, 'Invalid emoji.')
	}

	const updatedAccount: AccountSchema = {
		discordId: sessionResponse.account.discordId,
		customization: {
			backgroundUrl,
			pack,
			blurBackground,
			emoji,
		},
	}

	const response = await fetchApi(`accounts/update`, fetch, {
		method: 'POST',
		headers: {
			'Content-Type': 'application/json',
			key: key
		},
		body: JSON.stringify(updatedAccount),
	}).then(r => r.json())
	console.log(response)


	return json({ ok: true })
}) satisfies PageServerLoad