aboutsummaryrefslogtreecommitdiff
path: root/src/routes/profile/update.ts
blob: 168cba8dccffff479851379a310c5b206a3d8a16 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { fetchApi } 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'
import env from '$lib/env'

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: RequestHandler = async ({ request, locals, platform }) => {
	if (locals.sid === undefined) {
		return {
			body: { ok: false, error: 'You are not logged in.' },
			status: 401,
		}
	}
	const key = env(platform).SKYBLOCK_STATS_API_KEY
	if (!key) {
		return {
			body: { ok: false, error: 'The SKYBLOCK_STATS_API_KEY environment variable is not set.' },
			status: 500,
		}
	}
	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)
		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
	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,
		}
	}
	if (typeof emoji !== 'undefined' && typeof emoji !== 'string') {
		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

	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 fetchApi(`accounts/update`, fetch, {
		method: 'POST',
		headers: {
			'Content-Type': 'application/json',
			key: key
		},
		body: JSON.stringify(updatedAccount),
	}).then(r => r.json())


	return {
		body: { ok: true } as JSONValue,
	}
}