aboutsummaryrefslogtreecommitdiff
path: root/src/routes/api/badges/+server.ts
blob: a4d4ba936ec02f3bb6bed4c400c6045038926b62 (plain) (blame)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { safeUserIdentity } from "$lib/Data/AniList/identity";
import { decodeAuthCookieOrNull } from "$lib/Effect/authCookie";
import { decodeRequestJsonOrThrow } from "$lib/Effect/requestBody";
import {
	removeAllUserBadges,
	removeUserBadge,
	updateUserBadge,
	getUserBadges,
	addUserBadge,
	type Badge,
	type BadgeInput,
	migrateCategory,
	setShadowHidden,
	setShadowHiddenBadge,
	incrementClickCount,
} from "$lib/Database/SB/User/badges";
import { Schema } from "effect";
import { appOrigin, appOriginHeaders } from "$lib/Utility/appOrigin";
import privilegedUser from "$lib/Utility/privilegedUser";

const unauthorised = () => new Response("Unauthorised", { status: 401 });
const importedBadgeSchema = Schema.Record(Schema.String, Schema.Unknown);

const badges = async (id: number) =>
	Response.json(await getUserBadges(id), {
		headers: appOriginHeaders(),
	});

export const GET = async ({ url }) => {
	return await badges(Number(url.searchParams.get("id") || 0));
};

export const DELETE = async ({ url, cookies }) => {
	const userCookie = cookies.get("user");

	if (!userCookie) return unauthorised();

	const user = decodeAuthCookieOrNull(userCookie);

	if (!user) return unauthorised();

	const identity = await safeUserIdentity(user);

	if (!identity) return unauthorised();

	if ((url.searchParams.get("prune") || 0) === "true") {
		await removeAllUserBadges(identity.id);
	} else {
		await removeUserBadge(identity.id, Number(url.searchParams.get("id")));
	}

	return await badges(identity.id);
};

export const PUT = async ({ cookies, url, request }) => {
	if (url.searchParams.get("incrementClickCount") || undefined) {
		if (request.headers.get("origin") !== appOrigin()) return unauthorised();

		await incrementClickCount(
			Number(url.searchParams.get("incrementClickCount")),
		);

		return new Response("Incremented", { status: 200 });
	}

	const userCookie = cookies.get("user");

	if (!userCookie) return unauthorised();

	const user = decodeAuthCookieOrNull(userCookie);

	if (!user) return unauthorised();

	const identity = await safeUserIdentity(user);

	if (!identity) return unauthorised();
	const authorised = privilegedUser(identity.id);

	if (url.searchParams.get("shadowHide"))
		setShadowHidden(Number(url.searchParams.get("shadowHide")), authorised);

	if (url.searchParams.get("import") || undefined) {
		const importedBadges = await decodeRequestJsonOrThrow(
			request,
			Schema.Array(importedBadgeSchema),
		);

		await Promise.all(
			importedBadges.map(
				async (badge) =>
					await addUserBadge(identity.id, badge as unknown as BadgeInput),
			),
		);

		return await badges(identity.id);
	} else if (url.searchParams.get("migrate") || undefined) {
		await migrateCategory(
			identity.id,
			url.searchParams.get("original") || "",
			url.searchParams.get("new") || "",
		);

		return await badges(identity.id);
	}

	if (url.searchParams.get("hide") || undefined) {
		const allBadges = await getUserBadges(identity.id);

		await Promise.all(
			allBadges
				.filter(
					(badge) =>
						badge.category === (url.searchParams.get("category") || ""),
				)
				.map(async (badge) => {
					await updateUserBadge(identity.id, badge.id as number, {
						...badge,
						hidden:
							allBadges
								.filter(
									(badge) =>
										badge.category === (url.searchParams.get("category") || ""),
								)
								.filter((badge) => badge.hidden).length >
							allBadges.filter(
								(badge) =>
									badge.category === (url.searchParams.get("category") || ""),
							).length /
								2
								? false
								: true,
					});
				}),
		);

		return await badges(identity.id);
	}

	if (url.searchParams.get("shadowHideBadge") || undefined) {
		if (!authorised) return unauthorised();

		await setShadowHiddenBadge(
			Number(url.searchParams.get("shadowHideBadge")),
			url.searchParams.get("status") === "true" ? false : true,
		);

		return await badges(Number(url.searchParams.get("id")));
	}

	const badge = {
		post: url.searchParams.get("post") || undefined,
		image: url.searchParams.get("image") || undefined,
		description: url.searchParams.get("description") || null,
		time: url.searchParams.get("time") || undefined,
		category: url.searchParams.get("category") || null,
		hidden: url.searchParams.get("hidden") || false,
		source: url.searchParams.get("source") || null,
		designer: url.searchParams.get("designer") || null,
	};

	if (
		(await getUserBadges(identity.id)).find(
			(badge) => Number(badge.id) === Number(url.searchParams.get("update")),
		)
	) {
		await updateUserBadge(
			identity.id,
			Number(url.searchParams.get("update")),
			badge as Badge,
		);
	} else {
		await addUserBadge(identity.id, badge as Badge);
	}

	return await badges(identity.id);
};