rhythia-api 242.0.0 → 244.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 (43) hide show
  1. package/api/createBeatmap.ts +64 -40
  2. package/api/editProfile.ts +4 -67
  3. package/api/executeAdminOperation.ts +637 -27
  4. package/api/getAvatarUploadUrl.ts +90 -85
  5. package/api/getBeatmapPage.ts +2 -0
  6. package/api/getBeatmapPageById.ts +2 -0
  7. package/api/getBeatmaps.ts +110 -197
  8. package/api/getChangelog.ts +46 -0
  9. package/api/getCollection.ts +44 -31
  10. package/api/getMapUploadUrl.ts +90 -93
  11. package/api/getProfile.ts +297 -297
  12. package/api/getScore.ts +2 -0
  13. package/api/getVideoUploadUrl.ts +90 -85
  14. package/api/submitScoreInternal.ts +506 -461
  15. package/api/updateBeatmapPage.ts +6 -0
  16. package/beatmap-file-urls.json +29398 -0
  17. package/handleApi.ts +7 -4
  18. package/index.ts +193 -162
  19. package/package.json +7 -3
  20. package/queries/admin_delete_user.sql +42 -39
  21. package/queries/admin_remove_all_scores.sql +6 -3
  22. package/queries/admin_remove_score.sql +107 -0
  23. package/queries/admin_update_profile.sql +22 -0
  24. package/queries/get_beatmaps_v2.sql +48 -0
  25. package/queries/get_top_scores_for_beatmap3.sql +47 -38
  26. package/queries/profile_update_guards.sql +66 -0
  27. package/supabase/.temp/cli-latest +1 -0
  28. package/supabase/.temp/linked-project.json +1 -0
  29. package/types/database.ts +1702 -1450
  30. package/utils/beatmapFiles.ts +102 -0
  31. package/utils/beatmapHash.ts +239 -0
  32. package/utils/beatmapTopScores.ts +68 -84
  33. package/utils/getUserBySession.ts +3 -1
  34. package/utils/moderation.ts +101 -0
  35. package/utils/profileUpdateValidation.ts +51 -0
  36. package/utils/redis.ts +24 -0
  37. package/utils/requestUtils.ts +2 -2
  38. package/utils/rhrReplay.ts +122 -0
  39. package/utils/star-calc/formatSingle.ts +107 -0
  40. package/utils/star-calc/rhmParser.ts +214 -0
  41. package/utils/star-calc/sspmParser.ts +294 -160
  42. package/worker.ts +197 -195
  43. package/.env +0 -1
@@ -7,19 +7,22 @@ export const Schema = {
7
7
  input: z.strictObject({
8
8
  session: z.string(),
9
9
  collection: z.number(),
10
+ page: z.number().optional().default(1),
11
+ itemsPerPage: z.number().optional().default(30),
10
12
  }),
11
13
  output: z.object({
12
- collection: z.object({
13
- title: z.string(),
14
- description: z.string(),
15
- owner: z.object({
16
- id: z.number(),
17
- username: z.string(),
18
- avatar_url: z.string().nullable(),
19
- }),
20
- isList: z.boolean(),
21
- beatmaps: z.array(
22
- z.object({
14
+ collection: z.object({
15
+ title: z.string(),
16
+ description: z.string(),
17
+ owner: z.object({
18
+ id: z.number(),
19
+ username: z.string(),
20
+ avatar_url: z.string().nullable(),
21
+ }),
22
+ isList: z.boolean(),
23
+ beatmapCount: z.number(),
24
+ beatmaps: z.array(
25
+ z.object({
23
26
  id: z.number(),
24
27
  playcount: z.number().nullable().optional(),
25
28
  created_at: z.string().nullable().optional(),
@@ -37,6 +40,7 @@ export const Schema = {
37
40
  })
38
41
  ),
39
42
  }),
43
+ totalPages: z.number(),
40
44
  error: z.string().optional(),
41
45
  }),
42
46
  };
@@ -51,18 +55,21 @@ export async function POST(request: Request) {
51
55
  }
52
56
 
53
57
  export async function handler(data: (typeof Schema)["input"]["_type"]) {
54
- let { data: queryCollectionData, error: collectionError } = await supabase
58
+ const from = (data.page - 1) * data.itemsPerPage;
59
+ const to = from + data.itemsPerPage - 1;
60
+
61
+ const { data: queryCollectionData } = await supabase
55
62
  .from("beatmapCollections")
56
63
  .select(
57
64
  `
58
- *,
59
- profiles!inner(
60
- id,
61
- username,
62
- avatar_url
63
- )
64
- `
65
- )
65
+ *,
66
+ profiles!inner(
67
+ id,
68
+ username,
69
+ avatar_url
70
+ )
71
+ `
72
+ )
66
73
  .eq("id", data.collection)
67
74
  .single();
68
75
 
@@ -70,7 +77,7 @@ export async function handler(data: (typeof Schema)["input"]["_type"]) {
70
77
  return NextResponse.json({ error: "Can't find collection" });
71
78
  }
72
79
 
73
- let { data: queryBeatmaps, error: beatmapsError } = await supabase
80
+ const { data: queryBeatmaps, count } = await supabase
74
81
  .from("collectionRelations")
75
82
  .select(
76
83
  `
@@ -95,12 +102,16 @@ export async function handler(data: (typeof Schema)["input"]["_type"]) {
95
102
  username
96
103
  )
97
104
  )
98
- `
105
+ `,
106
+ { count: "exact" }
99
107
  )
100
- .eq("collection", data.collection);
108
+ .eq("collection", data.collection)
109
+ .order("sort", { ascending: true })
110
+ .order("id", { ascending: true })
111
+ .range(from, to);
101
112
 
102
113
  const formattedBeatmaps =
103
- queryBeatmaps?.map((relation) => ({
114
+ queryBeatmaps?.map((relation: any) => ({
104
115
  id: relation.beatmapPages.id,
105
116
  playcount: relation.beatmapPages.beatmaps.playcount,
106
117
  created_at: relation.beatmapPages.created_at,
@@ -119,15 +130,17 @@ export async function handler(data: (typeof Schema)["input"]["_type"]) {
119
130
 
120
131
  return NextResponse.json({
121
132
  collection: {
122
- owner: {
123
- username: queryCollectionData.profiles.username,
124
- id: queryCollectionData.profiles.id,
125
- avatar_url: queryCollectionData.profiles.avatar_url,
126
- },
127
- isList: queryCollectionData.is_list,
128
- title: queryCollectionData.title,
133
+ owner: {
134
+ username: queryCollectionData.profiles.username,
135
+ id: queryCollectionData.profiles.id,
136
+ avatar_url: queryCollectionData.profiles.avatar_url,
137
+ },
138
+ isList: queryCollectionData.is_list,
139
+ beatmapCount: count || 0,
140
+ title: queryCollectionData.title,
129
141
  description: queryCollectionData.description,
130
142
  beatmaps: formattedBeatmaps,
131
143
  },
144
+ totalPages: Math.max(1, Math.ceil((count || 0) / data.itemsPerPage)),
132
145
  });
133
146
  }
@@ -1,93 +1,90 @@
1
- import { NextResponse } from "../utils/response";
2
- import z from "zod";
3
- import { protectedApi, validUser } from "../utils/requestUtils";
4
- import { supabase } from "../utils/supabase";
5
-
6
- import {
7
- PutBucketCorsCommand,
8
- PutObjectCommand,
9
- S3Client,
10
- } from "@aws-sdk/client-s3";
11
- import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
12
- import { validateIntrinsicToken } from "../utils/validateToken";
13
- import { getUserBySession } from "../utils/getUserBySession";
14
- import { User } from "@supabase/supabase-js";
15
-
16
- const s3Client = new S3Client({
17
- region: "auto",
18
- endpoint: "https://s3.eu-central-003.backblazeb2.com",
19
- credentials: {
20
- secretAccessKey: process.env.SECRET_BUCKET || "",
21
- accessKeyId: process.env.ACCESS_BUCKET || "",
22
- },
23
- });
24
-
25
- export const Schema = {
26
- input: z.strictObject({
27
- mapName: z.string().optional(),
28
- session: z.string(),
29
- contentLength: z.number(),
30
- contentType: z.string(),
31
- intrinsicToken: z.string(),
32
- }),
33
- output: z.strictObject({
34
- error: z.string().optional(),
35
- url: z.string().optional(),
36
- objectKey: z.string().optional(),
37
- }),
38
- };
39
-
40
- export async function POST(request: Request): Promise<NextResponse> {
41
- return protectedApi({
42
- request,
43
- schema: Schema,
44
- authorization: validUser,
45
- activity: handler,
46
- });
47
- }
48
-
49
- export async function handler({
50
- mapName,
51
- session,
52
- contentLength,
53
- contentType,
54
- intrinsicToken,
55
- }: (typeof Schema)["input"]["_type"]): Promise<
56
- NextResponse<(typeof Schema)["output"]["_type"]>
57
- > {
58
- const user = (await getUserBySession(session)) as User;
59
-
60
- if (!validateIntrinsicToken(intrinsicToken)) {
61
- return NextResponse.json({
62
- error: "Invalid intrinsic token",
63
- });
64
- }
65
-
66
- if (contentLength > 100000000) {
67
- return NextResponse.json({
68
- error: "Max content length exceeded.",
69
- });
70
- }
71
-
72
- if (contentType !== "application/octet-stream") {
73
- return NextResponse.json({
74
- error: "Unnacceptable format",
75
- });
76
- }
77
-
78
- const key = `rhythia-${mapName ? mapName : user.id}-${Date.now()}.sspm`;
79
- const command = new PutObjectCommand({
80
- Bucket: "rhthia-avatars",
81
- Key: key,
82
- ContentLength: contentLength,
83
- ContentType: contentType,
84
- });
85
-
86
- const presigned = await getSignedUrl(s3Client, command, {
87
- expiresIn: 3600,
88
- });
89
- return NextResponse.json({
90
- url: presigned,
91
- objectKey: key,
92
- });
93
- }
1
+ import { NextResponse } from "../utils/response";
2
+ import z from "zod";
3
+ import { protectedApi, validUser } from "../utils/requestUtils";
4
+
5
+ import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
6
+ import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
7
+ import { validateIntrinsicToken } from "../utils/validateToken";
8
+ import { getUserBySession } from "../utils/getUserBySession";
9
+ import { User } from "@supabase/supabase-js";
10
+
11
+ const s3Client = new S3Client({
12
+ region: "auto",
13
+ endpoint: "https://s3.eu-central-003.backblazeb2.com",
14
+ credentials: {
15
+ secretAccessKey: process.env.SECRET_BUCKET || "",
16
+ accessKeyId: process.env.ACCESS_BUCKET || "",
17
+ },
18
+ });
19
+
20
+ export const Schema = {
21
+ input: z.strictObject({
22
+ mapName: z.string().optional(),
23
+ session: z.string(),
24
+ contentLength: z.number(),
25
+ contentType: z.string(),
26
+ intrinsicToken: z.string(),
27
+ }),
28
+ output: z.strictObject({
29
+ error: z.string().optional(),
30
+ url: z.string().optional(),
31
+ objectKey: z.string().optional(),
32
+ }),
33
+ };
34
+
35
+ export async function POST(request: Request): Promise<NextResponse> {
36
+ return protectedApi({
37
+ request,
38
+ schema: Schema,
39
+ authorization: validUser,
40
+ activity: handler,
41
+ });
42
+ }
43
+
44
+ export async function handler({
45
+ mapName,
46
+ session,
47
+ contentLength,
48
+ contentType,
49
+ intrinsicToken,
50
+ }: (typeof Schema)["input"]["_type"]): Promise<
51
+ NextResponse<(typeof Schema)["output"]["_type"]>
52
+ > {
53
+ const user = (await getUserBySession(session)) as User;
54
+
55
+ if (!validateIntrinsicToken(intrinsicToken)) {
56
+ return NextResponse.json({
57
+ error: "Invalid intrinsic token",
58
+ });
59
+ }
60
+
61
+ if (contentLength > 100000000) {
62
+ return NextResponse.json({
63
+ error: "Max content length exceeded.",
64
+ });
65
+ }
66
+
67
+ if (contentType !== "application/octet-stream") {
68
+ return NextResponse.json({
69
+ error: "Unnacceptable format",
70
+ });
71
+ }
72
+
73
+ const key = `rhythia-${mapName ? mapName : user.id}-${Date.now()}.sspm`;
74
+ const command = new PutObjectCommand({
75
+ Bucket: "rhthia-avatars",
76
+ Key: key,
77
+ ContentLength: contentLength,
78
+ ContentType: contentType,
79
+ ContentDisposition: "attachment",
80
+ });
81
+
82
+ const presigned = await getSignedUrl(s3Client, command, {
83
+ expiresIn: 3600,
84
+ signableHeaders: new Set(["content-type"]),
85
+ });
86
+ return NextResponse.json({
87
+ url: presigned,
88
+ objectKey: key,
89
+ });
90
+ }