rhythia-api 182.0.0 → 183.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/.prettierrc.json +6 -6
  2. package/api/addCollectionMap.ts +82 -82
  3. package/api/approveMap.ts +78 -78
  4. package/api/chartPublicStats.ts +32 -32
  5. package/api/createBeatmap.ts +168 -156
  6. package/api/createBeatmapPage.ts +64 -64
  7. package/api/createClan.ts +81 -81
  8. package/api/createCollection.ts +58 -58
  9. package/api/deleteBeatmapPage.ts +77 -72
  10. package/api/deleteCollection.ts +59 -59
  11. package/api/deleteCollectionMap.ts +71 -71
  12. package/api/editAboutMe.ts +91 -91
  13. package/api/editClan.ts +90 -90
  14. package/api/editCollection.ts +77 -77
  15. package/api/editProfile.ts +123 -123
  16. package/api/getAvatarUploadUrl.ts +85 -85
  17. package/api/getBadgedUsers.ts +56 -56
  18. package/api/getBeatmapComments.ts +57 -57
  19. package/api/getBeatmapPage.ts +106 -106
  20. package/api/getBeatmapPageById.ts +99 -99
  21. package/api/getBeatmapStarRating.ts +53 -53
  22. package/api/getBeatmaps.ts +159 -159
  23. package/api/getClan.ts +77 -77
  24. package/api/getCollection.ts +130 -130
  25. package/api/getCollections.ts +128 -126
  26. package/api/getLeaderboard.ts +136 -136
  27. package/api/getMapUploadUrl.ts +93 -93
  28. package/api/getPassToken.ts +55 -55
  29. package/api/getProfile.ts +146 -146
  30. package/api/getPublicStats.ts +180 -180
  31. package/api/getRawStarRating.ts +57 -57
  32. package/api/getScore.ts +85 -85
  33. package/api/getTimestamp.ts +23 -23
  34. package/api/getUserScores.ts +175 -175
  35. package/api/nominateMap.ts +82 -69
  36. package/api/postBeatmapComment.ts +59 -59
  37. package/api/rankMapsArchive.ts +64 -64
  38. package/api/searchUsers.ts +56 -56
  39. package/api/setPasskey.ts +59 -59
  40. package/api/submitScore.ts +433 -433
  41. package/api/updateBeatmapPage.ts +229 -229
  42. package/handleApi.ts +20 -20
  43. package/index.html +2 -2
  44. package/index.ts +865 -864
  45. package/package.json +2 -2
  46. package/types/database.ts +39 -0
  47. package/utils/getUserBySession.ts +48 -48
  48. package/utils/requestUtils.ts +87 -87
  49. package/utils/security.ts +20 -20
  50. package/utils/star-calc/index.ts +72 -72
  51. package/utils/star-calc/osuUtils.ts +53 -53
  52. package/utils/star-calc/sspmParser.ts +398 -398
  53. package/utils/star-calc/sspmv1Parser.ts +165 -165
  54. package/utils/supabase.ts +13 -13
  55. package/utils/test +4 -4
  56. package/utils/validateToken.ts +7 -7
  57. package/vercel.json +12 -12
  58. package/package-lock.json +0 -8913
@@ -1,156 +1,168 @@
1
- import { NextResponse } from "next/server";
2
- import z from "zod";
3
- import { protectedApi, validUser } from "../utils/requestUtils";
4
- import { SSPMParser } from "../utils/star-calc/sspmParser";
5
- import { supabase } from "../utils/supabase";
6
- import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
7
- import { rateMap } from "../utils/star-calc";
8
- import { getUserBySession } from "../utils/getUserBySession";
9
- import { User } from "@supabase/supabase-js";
10
- const s3Client = new S3Client({
11
- region: "auto",
12
- endpoint: "https://s3.eu-central-003.backblazeb2.com",
13
- credentials: {
14
- secretAccessKey: process.env.SECRET_BUCKET || "",
15
- accessKeyId: process.env.ACCESS_BUCKET || "",
16
- },
17
- });
18
-
19
- export const Schema = {
20
- input: z.strictObject({
21
- url: z.string(),
22
- session: z.string(),
23
- updateFlag: z.boolean().optional(),
24
- }),
25
- output: z.strictObject({
26
- hash: z.string().optional(),
27
- error: z.string().optional(),
28
- }),
29
- };
30
-
31
- export async function POST(request: Request): Promise<NextResponse> {
32
- return protectedApi({
33
- request,
34
- schema: Schema,
35
- authorization: validUser,
36
- activity: handler,
37
- });
38
- }
39
-
40
- export async function handler({
41
- url,
42
- session,
43
- updateFlag,
44
- }: (typeof Schema)["input"]["_type"]): Promise<
45
- NextResponse<(typeof Schema)["output"]["_type"]>
46
- > {
47
- if (!url.startsWith(`https://static.rhythia.com/`))
48
- return NextResponse.json({ error: "Invalid url" });
49
-
50
- const request = await fetch(url);
51
- const bytes = await request.arrayBuffer();
52
- const parser = new SSPMParser(Buffer.from(bytes));
53
-
54
- const parsedData = parser.parse();
55
- const digested = parsedData.strings.mapID;
56
-
57
- const user = (await getUserBySession(session)) as User;
58
- let { data: userData, error: userError } = await supabase
59
- .from("profiles")
60
- .select("*")
61
- .eq("uid", user.id)
62
- .single();
63
-
64
- if (!userData) {
65
- return NextResponse.json({ error: "Bad user" });
66
- }
67
-
68
- if (
69
- userData.ban == "excluded" ||
70
- userData.ban == "restricted" ||
71
- userData.ban == "silenced"
72
- ) {
73
- return NextResponse.json(
74
- {
75
- error:
76
- "Silenced, restricted or excluded players can't create beatmaps their profile.",
77
- },
78
- { status: 404 }
79
- );
80
- }
81
-
82
- let { data: beatmapPage, error: errorlast } = await supabase
83
- .from("beatmapPages")
84
- .select(`*`)
85
- .eq("latestBeatmapHash", digested)
86
- .single();
87
-
88
- if (beatmapPage) {
89
- if (beatmapPage?.status !== "UNRANKED")
90
- return NextResponse.json({ error: "Only unranked maps can be updated" });
91
-
92
- if (!updateFlag) {
93
- return NextResponse.json({ error: "Already Exists" });
94
- } else if (beatmapPage.owner !== userData.id) {
95
- return NextResponse.json({ error: "Already Exists" });
96
- }
97
- }
98
-
99
- const imgkey = `beatmap-img-${Date.now()}-${digested}`;
100
-
101
- let buffer = Buffer.from([]);
102
- try {
103
- buffer = await require("sharp")(parsedData.cover)
104
- .resize(250)
105
- .jpeg({ mozjpeg: true })
106
- .toBuffer();
107
- } catch (error) {}
108
-
109
- let largeBuffer = Buffer.from([]);
110
- try {
111
- largeBuffer = await require("sharp")(parsedData.cover)
112
- .resize(850)
113
- .jpeg({ mozjpeg: true })
114
- .toBuffer();
115
- } catch (error) {}
116
-
117
- // Images
118
- const command = new PutObjectCommand({
119
- Bucket: "rhthia-avatars",
120
- Key: imgkey,
121
- Body: buffer,
122
- ContentType: "image/jpeg",
123
- });
124
-
125
- await s3Client.send(command);
126
-
127
- const command2 = new PutObjectCommand({
128
- Bucket: "rhthia-avatars",
129
- Key: imgkey + "large",
130
- Body: largeBuffer,
131
- ContentType: "image/jpeg",
132
- });
133
-
134
- await s3Client.send(command2);
135
- // Images End
136
-
137
- const markers = parsedData.markers.sort((a, b) => a.position - b.position);
138
-
139
- const upserted = await supabase.from("beatmaps").upsert({
140
- beatmapHash: digested,
141
- title: parsedData.strings.mapName,
142
- playcount: 0,
143
- difficulty: parsedData.metadata.difficulty,
144
- noteCount: parsedData.metadata.noteCount,
145
- length: markers[markers.length - 1].position,
146
- beatmapFile: url,
147
- image: `https://static.rhythia.com/${imgkey}`,
148
- imageLarge: `https://static.rhythia.com/${imgkey}large`,
149
- starRating: rateMap(parsedData),
150
- });
151
-
152
- if (upserted.error?.message.length) {
153
- return NextResponse.json({ error: upserted.error.message });
154
- }
155
- return NextResponse.json({ hash: digested });
156
- }
1
+ import { NextResponse } from "next/server";
2
+ import z from "zod";
3
+ import { protectedApi, validUser } from "../utils/requestUtils";
4
+ import { SSPMParser } from "../utils/star-calc/sspmParser";
5
+ import { supabase } from "../utils/supabase";
6
+ import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
7
+ import { rateMap } from "../utils/star-calc";
8
+ import { getUserBySession } from "../utils/getUserBySession";
9
+ import { User } from "@supabase/supabase-js";
10
+ const s3Client = new S3Client({
11
+ region: "auto",
12
+ endpoint: "https://s3.eu-central-003.backblazeb2.com",
13
+ credentials: {
14
+ secretAccessKey: process.env.SECRET_BUCKET || "",
15
+ accessKeyId: process.env.ACCESS_BUCKET || "",
16
+ },
17
+ });
18
+
19
+ export const Schema = {
20
+ input: z.strictObject({
21
+ url: z.string(),
22
+ session: z.string(),
23
+ updateFlag: z.boolean().optional(),
24
+ }),
25
+ output: z.strictObject({
26
+ hash: z.string().optional(),
27
+ error: z.string().optional(),
28
+ }),
29
+ };
30
+
31
+ export async function POST(request: Request): Promise<NextResponse> {
32
+ return protectedApi({
33
+ request,
34
+ schema: Schema,
35
+ authorization: validUser,
36
+ activity: handler,
37
+ });
38
+ }
39
+
40
+ export async function handler({
41
+ url,
42
+ session,
43
+ updateFlag,
44
+ }: (typeof Schema)["input"]["_type"]): Promise<
45
+ NextResponse<(typeof Schema)["output"]["_type"]>
46
+ > {
47
+ if (!url.startsWith(`https://static.rhythia.com/`))
48
+ return NextResponse.json({ error: "Invalid url" });
49
+
50
+ const request = await fetch(url);
51
+ const bytes = await request.arrayBuffer();
52
+ const parser = new SSPMParser(Buffer.from(bytes));
53
+
54
+ const parsedData = parser.parse();
55
+ const digested = parsedData.strings.mapID;
56
+
57
+ const user = (await getUserBySession(session)) as User;
58
+ let { data: userData, error: userError } = await supabase
59
+ .from("profiles")
60
+ .select("*")
61
+ .eq("uid", user.id)
62
+ .single();
63
+
64
+ if (!userData) {
65
+ return NextResponse.json({ error: "Bad user" });
66
+ }
67
+
68
+ if (
69
+ !parsedData.strings.mappers
70
+ .map((mapper) => mapper.toLowerCase().trim())
71
+ .includes((userData.username?.trim() || "").toLowerCase())
72
+ ) {
73
+ return NextResponse.json({
74
+ error: `You are not among the authors of the map. If you made the map, please add yourself as an author in the editor. Mappers: ${JSON.stringify(
75
+ parsedData.strings.mappers
76
+ )}, Your name: ${userData.username}`,
77
+ });
78
+ }
79
+
80
+ if (
81
+ userData.ban == "excluded" ||
82
+ userData.ban == "restricted" ||
83
+ userData.ban == "silenced"
84
+ ) {
85
+ return NextResponse.json(
86
+ {
87
+ error:
88
+ "Silenced, restricted or excluded players can't create beatmaps their profile.",
89
+ },
90
+ { status: 404 }
91
+ );
92
+ }
93
+
94
+ let { data: beatmapPage, error: errorlast } = await supabase
95
+ .from("beatmapPages")
96
+ .select(`*`)
97
+ .eq("latestBeatmapHash", digested)
98
+ .single();
99
+
100
+ if (beatmapPage) {
101
+ if (beatmapPage?.status !== "UNRANKED")
102
+ return NextResponse.json({ error: "Only unranked maps can be updated" });
103
+
104
+ if (!updateFlag) {
105
+ return NextResponse.json({ error: "Already Exists" });
106
+ } else if (beatmapPage.owner !== userData.id) {
107
+ return NextResponse.json({ error: "Already Exists" });
108
+ }
109
+ }
110
+
111
+ const imgkey = `beatmap-img-${Date.now()}-${digested}`;
112
+
113
+ let buffer = Buffer.from([]);
114
+ try {
115
+ buffer = await require("sharp")(parsedData.cover)
116
+ .resize(250)
117
+ .jpeg({ mozjpeg: true })
118
+ .toBuffer();
119
+ } catch (error) {}
120
+
121
+ let largeBuffer = Buffer.from([]);
122
+ try {
123
+ largeBuffer = await require("sharp")(parsedData.cover)
124
+ .resize(850)
125
+ .jpeg({ mozjpeg: true })
126
+ .toBuffer();
127
+ } catch (error) {}
128
+
129
+ // Images
130
+ const command = new PutObjectCommand({
131
+ Bucket: "rhthia-avatars",
132
+ Key: imgkey,
133
+ Body: buffer,
134
+ ContentType: "image/jpeg",
135
+ });
136
+
137
+ await s3Client.send(command);
138
+
139
+ const command2 = new PutObjectCommand({
140
+ Bucket: "rhthia-avatars",
141
+ Key: imgkey + "large",
142
+ Body: largeBuffer,
143
+ ContentType: "image/jpeg",
144
+ });
145
+
146
+ await s3Client.send(command2);
147
+ // Images End
148
+
149
+ const markers = parsedData.markers.sort((a, b) => a.position - b.position);
150
+
151
+ const upserted = await supabase.from("beatmaps").upsert({
152
+ beatmapHash: digested,
153
+ title: parsedData.strings.mapName,
154
+ playcount: 0,
155
+ difficulty: parsedData.metadata.difficulty,
156
+ noteCount: parsedData.metadata.noteCount,
157
+ length: markers[markers.length - 1].position,
158
+ beatmapFile: url,
159
+ image: `https://static.rhythia.com/${imgkey}`,
160
+ imageLarge: `https://static.rhythia.com/${imgkey}large`,
161
+ starRating: rateMap(parsedData),
162
+ });
163
+
164
+ if (upserted.error?.message.length) {
165
+ return NextResponse.json({ error: upserted.error.message });
166
+ }
167
+ return NextResponse.json({ hash: digested });
168
+ }
@@ -1,64 +1,64 @@
1
- import { NextResponse } from "next/server";
2
- import z from "zod";
3
- import { protectedApi, validUser } from "../utils/requestUtils";
4
- import { supabase } from "../utils/supabase";
5
- import { getUserBySession } from "../utils/getUserBySession";
6
- import { User } from "@supabase/supabase-js";
7
-
8
- export const Schema = {
9
- input: z.strictObject({
10
- session: z.string(),
11
- }),
12
- output: z.strictObject({
13
- error: z.string().optional(),
14
- id: z.number().optional(),
15
- }),
16
- };
17
-
18
- export async function POST(request: Request): Promise<NextResponse> {
19
- return protectedApi({
20
- request,
21
- schema: Schema,
22
- authorization: validUser,
23
- activity: handler,
24
- });
25
- }
26
-
27
- export async function handler({
28
- session,
29
- }: (typeof Schema)["input"]["_type"]): Promise<
30
- NextResponse<(typeof Schema)["output"]["_type"]>
31
- > {
32
- const user = (await getUserBySession(session)) as User;
33
- let { data: userData, error: userError } = await supabase
34
- .from("profiles")
35
- .select("*")
36
- .eq("uid", user.id)
37
- .single();
38
-
39
- if (!userData) return NextResponse.json({ error: "No user." });
40
-
41
- if (
42
- userData.ban == "excluded" ||
43
- userData.ban == "restricted" ||
44
- userData.ban == "silenced"
45
- ) {
46
- return NextResponse.json(
47
- {
48
- error:
49
- "Silenced, restricted or excluded players can't create beatmap pages.",
50
- },
51
- { status: 404 }
52
- );
53
- }
54
-
55
- const upserted = await supabase
56
- .from("beatmapPages")
57
- .upsert({
58
- owner: userData.id,
59
- })
60
- .select("*")
61
- .single();
62
-
63
- return NextResponse.json({ id: upserted.data?.id });
64
- }
1
+ import { NextResponse } from "next/server";
2
+ import z from "zod";
3
+ import { protectedApi, validUser } from "../utils/requestUtils";
4
+ import { supabase } from "../utils/supabase";
5
+ import { getUserBySession } from "../utils/getUserBySession";
6
+ import { User } from "@supabase/supabase-js";
7
+
8
+ export const Schema = {
9
+ input: z.strictObject({
10
+ session: z.string(),
11
+ }),
12
+ output: z.strictObject({
13
+ error: z.string().optional(),
14
+ id: z.number().optional(),
15
+ }),
16
+ };
17
+
18
+ export async function POST(request: Request): Promise<NextResponse> {
19
+ return protectedApi({
20
+ request,
21
+ schema: Schema,
22
+ authorization: validUser,
23
+ activity: handler,
24
+ });
25
+ }
26
+
27
+ export async function handler({
28
+ session,
29
+ }: (typeof Schema)["input"]["_type"]): Promise<
30
+ NextResponse<(typeof Schema)["output"]["_type"]>
31
+ > {
32
+ const user = (await getUserBySession(session)) as User;
33
+ let { data: userData, error: userError } = await supabase
34
+ .from("profiles")
35
+ .select("*")
36
+ .eq("uid", user.id)
37
+ .single();
38
+
39
+ if (!userData) return NextResponse.json({ error: "No user." });
40
+
41
+ if (
42
+ userData.ban == "excluded" ||
43
+ userData.ban == "restricted" ||
44
+ userData.ban == "silenced"
45
+ ) {
46
+ return NextResponse.json(
47
+ {
48
+ error:
49
+ "Silenced, restricted or excluded players can't create beatmap pages.",
50
+ },
51
+ { status: 404 }
52
+ );
53
+ }
54
+
55
+ const upserted = await supabase
56
+ .from("beatmapPages")
57
+ .upsert({
58
+ owner: userData.id,
59
+ })
60
+ .select("*")
61
+ .single();
62
+
63
+ return NextResponse.json({ id: upserted.data?.id });
64
+ }
package/api/createClan.ts CHANGED
@@ -1,81 +1,81 @@
1
- import { NextResponse } from "next/server";
2
- import z from "zod";
3
- import { protectedApi, validUser } from "../utils/requestUtils";
4
- import { supabase } from "../utils/supabase";
5
- import { getUserBySession } from "../utils/getUserBySession";
6
- import { User } from "@supabase/supabase-js";
7
-
8
- export const Schema = {
9
- input: z.strictObject({
10
- session: z.string(),
11
- name: z.string(),
12
- acronym: z.string(),
13
- }),
14
- output: z.object({
15
- error: z.string().optional(),
16
- }),
17
- };
18
-
19
- export async function POST(request: Request) {
20
- return protectedApi({
21
- request,
22
- schema: Schema,
23
- authorization: validUser,
24
- activity: handler,
25
- });
26
- }
27
-
28
- export async function handler(data: (typeof Schema)["input"]["_type"]) {
29
- const user = (await getUserBySession(data.session)) as User;
30
- let { data: queryUserData, error: userError } = await supabase
31
- .from("profiles")
32
- .select("*")
33
- .eq("uid", user.id)
34
- .single();
35
-
36
- if (!queryUserData) {
37
- return NextResponse.json({ error: "Can't find user" });
38
- }
39
-
40
- if (queryUserData.clan !== null) {
41
- return NextResponse.json({
42
- error: "You can't create clans while in a clan.",
43
- });
44
- }
45
-
46
- if (queryUserData.ban !== "cool") {
47
- return NextResponse.json({
48
- error: "You are have an active ban, can't create a clan.",
49
- });
50
- }
51
-
52
- if (data.name.length < 3 || data.name.length > 32) {
53
- return NextResponse.json({
54
- error: "Clan name must be between 3 and 32 characters.",
55
- });
56
- }
57
- if (data.acronym.length < 3 || data.acronym.length > 6) {
58
- return NextResponse.json({
59
- error: "Acronyms must be of length between 3 and 6",
60
- });
61
- }
62
-
63
- await supabase.from("clans").upsert({
64
- name: data.name,
65
- owner: queryUserData.id,
66
- acronym: data.acronym.toUpperCase(),
67
- });
68
-
69
- let { data: queryClanData, error: clanError } = await supabase
70
- .from("clans")
71
- .select("*")
72
- .eq("owner", queryUserData.id)
73
- .single();
74
-
75
- await supabase.from("profiles").upsert({
76
- id: queryUserData.id,
77
- clan: queryClanData?.id,
78
- });
79
-
80
- return NextResponse.json({});
81
- }
1
+ import { NextResponse } from "next/server";
2
+ import z from "zod";
3
+ import { protectedApi, validUser } from "../utils/requestUtils";
4
+ import { supabase } from "../utils/supabase";
5
+ import { getUserBySession } from "../utils/getUserBySession";
6
+ import { User } from "@supabase/supabase-js";
7
+
8
+ export const Schema = {
9
+ input: z.strictObject({
10
+ session: z.string(),
11
+ name: z.string(),
12
+ acronym: z.string(),
13
+ }),
14
+ output: z.object({
15
+ error: z.string().optional(),
16
+ }),
17
+ };
18
+
19
+ export async function POST(request: Request) {
20
+ return protectedApi({
21
+ request,
22
+ schema: Schema,
23
+ authorization: validUser,
24
+ activity: handler,
25
+ });
26
+ }
27
+
28
+ export async function handler(data: (typeof Schema)["input"]["_type"]) {
29
+ const user = (await getUserBySession(data.session)) as User;
30
+ let { data: queryUserData, error: userError } = await supabase
31
+ .from("profiles")
32
+ .select("*")
33
+ .eq("uid", user.id)
34
+ .single();
35
+
36
+ if (!queryUserData) {
37
+ return NextResponse.json({ error: "Can't find user" });
38
+ }
39
+
40
+ if (queryUserData.clan !== null) {
41
+ return NextResponse.json({
42
+ error: "You can't create clans while in a clan.",
43
+ });
44
+ }
45
+
46
+ if (queryUserData.ban !== "cool") {
47
+ return NextResponse.json({
48
+ error: "You are have an active ban, can't create a clan.",
49
+ });
50
+ }
51
+
52
+ if (data.name.length < 3 || data.name.length > 32) {
53
+ return NextResponse.json({
54
+ error: "Clan name must be between 3 and 32 characters.",
55
+ });
56
+ }
57
+ if (data.acronym.length < 3 || data.acronym.length > 6) {
58
+ return NextResponse.json({
59
+ error: "Acronyms must be of length between 3 and 6",
60
+ });
61
+ }
62
+
63
+ await supabase.from("clans").upsert({
64
+ name: data.name,
65
+ owner: queryUserData.id,
66
+ acronym: data.acronym.toUpperCase(),
67
+ });
68
+
69
+ let { data: queryClanData, error: clanError } = await supabase
70
+ .from("clans")
71
+ .select("*")
72
+ .eq("owner", queryUserData.id)
73
+ .single();
74
+
75
+ await supabase.from("profiles").upsert({
76
+ id: queryUserData.id,
77
+ clan: queryClanData?.id,
78
+ });
79
+
80
+ return NextResponse.json({});
81
+ }