rhythia-api 138.0.0 → 141.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.
- package/api/createBeatmap.ts +22 -3
- package/api/deleteBeatmapPage.ts +69 -0
- package/api/submitScore.ts +26 -3
- package/api/updateBeatmapPage.ts +3 -0
- package/index.ts +5 -0
- package/package.json +1 -1
- package/scripts/ci-deploy.ts +0 -76
- package/scripts/test.ts +0 -96
- package/scripts/update.ts +0 -49
package/api/createBeatmap.ts
CHANGED
|
@@ -18,6 +18,7 @@ export const Schema = {
|
|
|
18
18
|
input: z.strictObject({
|
|
19
19
|
url: z.string(),
|
|
20
20
|
session: z.string(),
|
|
21
|
+
updateFlag: z.boolean().optional(),
|
|
21
22
|
}),
|
|
22
23
|
output: z.strictObject({
|
|
23
24
|
hash: z.string().optional(),
|
|
@@ -36,6 +37,8 @@ export async function POST(request: Request): Promise<NextResponse> {
|
|
|
36
37
|
|
|
37
38
|
export async function handler({
|
|
38
39
|
url,
|
|
40
|
+
session,
|
|
41
|
+
updateFlag,
|
|
39
42
|
}: (typeof Schema)["input"]["_type"]): Promise<
|
|
40
43
|
NextResponse<(typeof Schema)["output"]["_type"]>
|
|
41
44
|
> {
|
|
@@ -49,6 +52,17 @@ export async function handler({
|
|
|
49
52
|
const parsedData = parser.parse();
|
|
50
53
|
const digested = parsedData.strings.mapID;
|
|
51
54
|
|
|
55
|
+
const user = (await supabase.auth.getUser(session)).data.user!;
|
|
56
|
+
let { data: userData, error: userError } = await supabase
|
|
57
|
+
.from("profiles")
|
|
58
|
+
.select("*")
|
|
59
|
+
.eq("uid", user.id)
|
|
60
|
+
.single();
|
|
61
|
+
|
|
62
|
+
if (!userData) {
|
|
63
|
+
return NextResponse.json({ error: "Bad user" });
|
|
64
|
+
}
|
|
65
|
+
|
|
52
66
|
let { data: beatmapPage, error: errorlast } = await supabase
|
|
53
67
|
.from("beatmapPages")
|
|
54
68
|
.select(`*`)
|
|
@@ -56,7 +70,11 @@ export async function handler({
|
|
|
56
70
|
.single();
|
|
57
71
|
|
|
58
72
|
if (beatmapPage) {
|
|
59
|
-
|
|
73
|
+
if (!updateFlag) {
|
|
74
|
+
return NextResponse.json({ error: "Already Exists" });
|
|
75
|
+
} else if (beatmapPage.owner !== userData.id) {
|
|
76
|
+
return NextResponse.json({ error: "Already Exists" });
|
|
77
|
+
}
|
|
60
78
|
}
|
|
61
79
|
|
|
62
80
|
const imgkey = `beatmap-img-${Date.now()}-${digested}`;
|
|
@@ -77,14 +95,15 @@ export async function handler({
|
|
|
77
95
|
});
|
|
78
96
|
|
|
79
97
|
await s3Client.send(command);
|
|
80
|
-
parsedData.markers.sort((a, b) => a.position - b.position);
|
|
98
|
+
const markers = parsedData.markers.sort((a, b) => a.position - b.position);
|
|
99
|
+
|
|
81
100
|
const upserted = await supabase.from("beatmaps").upsert({
|
|
82
101
|
beatmapHash: digested,
|
|
83
102
|
title: parsedData.strings.mapName,
|
|
84
103
|
playcount: 0,
|
|
85
104
|
difficulty: parsedData.metadata.difficulty,
|
|
86
105
|
noteCount: parsedData.metadata.noteCount,
|
|
87
|
-
length:
|
|
106
|
+
length: markers[markers.length - 1].position,
|
|
88
107
|
beatmapFile: url,
|
|
89
108
|
image: `https://static.rhythia.com/${imgkey}`,
|
|
90
109
|
starRating: rateMap(parsedData),
|
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
|
|
6
|
+
export const Schema = {
|
|
7
|
+
input: z.strictObject({
|
|
8
|
+
session: z.string(),
|
|
9
|
+
id: z.number(),
|
|
10
|
+
}),
|
|
11
|
+
output: z.strictObject({
|
|
12
|
+
error: z.string().optional(),
|
|
13
|
+
}),
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export async function POST(request: Request): Promise<NextResponse> {
|
|
17
|
+
return protectedApi({
|
|
18
|
+
request,
|
|
19
|
+
schema: Schema,
|
|
20
|
+
authorization: validUser,
|
|
21
|
+
activity: handler,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function handler({
|
|
26
|
+
session,
|
|
27
|
+
id,
|
|
28
|
+
}: (typeof Schema)["input"]["_type"]): Promise<
|
|
29
|
+
NextResponse<(typeof Schema)["output"]["_type"]>
|
|
30
|
+
> {
|
|
31
|
+
const user = (await supabase.auth.getUser(session)).data.user!;
|
|
32
|
+
let { data: userData, error: userError } = await supabase
|
|
33
|
+
.from("profiles")
|
|
34
|
+
.select("*")
|
|
35
|
+
.eq("uid", user.id)
|
|
36
|
+
.single();
|
|
37
|
+
|
|
38
|
+
let { data: pageData, error: pageError } = await supabase
|
|
39
|
+
.from("beatmapPages")
|
|
40
|
+
.select("*")
|
|
41
|
+
.eq("id", id)
|
|
42
|
+
.single();
|
|
43
|
+
|
|
44
|
+
if (!pageData) return NextResponse.json({ error: "No beatmap." });
|
|
45
|
+
|
|
46
|
+
let { data: beatmapData, error: bmPageError } = await supabase
|
|
47
|
+
.from("beatmaps")
|
|
48
|
+
.select("*")
|
|
49
|
+
.eq("beatmapHash", pageData.latestBeatmapHash || "-1-1-1-1")
|
|
50
|
+
.single();
|
|
51
|
+
|
|
52
|
+
if (!userData) return NextResponse.json({ error: "No user." });
|
|
53
|
+
if (!beatmapData) return NextResponse.json({ error: "No beatmap." });
|
|
54
|
+
|
|
55
|
+
if (userData.id !== pageData.owner)
|
|
56
|
+
return NextResponse.json({ error: "Non-authz user." });
|
|
57
|
+
|
|
58
|
+
if (pageData.status !== "UNRANKED")
|
|
59
|
+
return NextResponse.json({ error: "Only unranked maps can be updated" });
|
|
60
|
+
|
|
61
|
+
await supabase.from("beatmapPageComments").delete().eq("beatmapPage", id);
|
|
62
|
+
await supabase.from("beatmapPages").delete().eq("id", id);
|
|
63
|
+
await supabase
|
|
64
|
+
.from("beatmaps")
|
|
65
|
+
.delete()
|
|
66
|
+
.eq("beatmapHash", beatmapData.beatmapHash);
|
|
67
|
+
|
|
68
|
+
return NextResponse.json({});
|
|
69
|
+
}
|
package/api/submitScore.ts
CHANGED
|
@@ -60,7 +60,7 @@ export async function handler({
|
|
|
60
60
|
console.log(userData);
|
|
61
61
|
let { data: beatmaps, error } = await supabase
|
|
62
62
|
.from("beatmaps")
|
|
63
|
-
.select("
|
|
63
|
+
.select("*")
|
|
64
64
|
.eq("beatmapHash", data.mapHash)
|
|
65
65
|
.single();
|
|
66
66
|
|
|
@@ -81,10 +81,33 @@ export async function handler({
|
|
|
81
81
|
|
|
82
82
|
let newPlaycount = 1;
|
|
83
83
|
|
|
84
|
-
if (beatmaps) {
|
|
85
|
-
|
|
84
|
+
if (!beatmaps) {
|
|
85
|
+
return NextResponse.json(
|
|
86
|
+
{
|
|
87
|
+
error: "Map not submitted",
|
|
88
|
+
},
|
|
89
|
+
{ status: 500 }
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (beatmaps.noteCount !== data.mapNoteCount) {
|
|
94
|
+
return NextResponse.json(
|
|
95
|
+
{
|
|
96
|
+
error: "Wrong map",
|
|
97
|
+
},
|
|
98
|
+
{ status: 500 }
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
if (beatmaps.noteCount !== data.mapNoteCount) {
|
|
102
|
+
return NextResponse.json(
|
|
103
|
+
{
|
|
104
|
+
error: "Wrong map",
|
|
105
|
+
},
|
|
106
|
+
{ status: 500 }
|
|
107
|
+
);
|
|
86
108
|
}
|
|
87
109
|
|
|
110
|
+
newPlaycount = (beatmaps.playcount || 1) + 1;
|
|
88
111
|
await supabase.from("beatmaps").upsert({
|
|
89
112
|
beatmapHash: data.mapHash,
|
|
90
113
|
title: data.mapTitle,
|
package/api/updateBeatmapPage.ts
CHANGED
|
@@ -59,6 +59,9 @@ export async function handler({
|
|
|
59
59
|
if (userData.id !== pageData?.owner)
|
|
60
60
|
return NextResponse.json({ error: "Non-authz user." });
|
|
61
61
|
|
|
62
|
+
if (pageData?.status !== "UNRANKED")
|
|
63
|
+
return NextResponse.json({ error: "Only unranked maps can be updated" });
|
|
64
|
+
|
|
62
65
|
const upserted = await supabase
|
|
63
66
|
.from("beatmapPages")
|
|
64
67
|
.upsert({
|
package/index.ts
CHANGED
|
@@ -15,6 +15,11 @@ import { Schema as CreateBeatmapPage } from "./api/createBeatmapPage"
|
|
|
15
15
|
export { Schema as SchemaCreateBeatmapPage } from "./api/createBeatmapPage"
|
|
16
16
|
export const createBeatmapPage = handleApi({url:"/api/createBeatmapPage",...CreateBeatmapPage})
|
|
17
17
|
|
|
18
|
+
// ./api/deleteBeatmapPage.ts API
|
|
19
|
+
import { Schema as DeleteBeatmapPage } from "./api/deleteBeatmapPage"
|
|
20
|
+
export { Schema as SchemaDeleteBeatmapPage } from "./api/deleteBeatmapPage"
|
|
21
|
+
export const deleteBeatmapPage = handleApi({url:"/api/deleteBeatmapPage",...DeleteBeatmapPage})
|
|
22
|
+
|
|
18
23
|
// ./api/editAboutMe.ts API
|
|
19
24
|
import { Schema as EditAboutMe } from "./api/editAboutMe"
|
|
20
25
|
export { Schema as SchemaEditAboutMe } from "./api/editAboutMe"
|
package/package.json
CHANGED
package/scripts/ci-deploy.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import * as git from "isomorphic-git";
|
|
3
|
-
import http from "isomorphic-git/http/node";
|
|
4
|
-
import path from "path";
|
|
5
|
-
|
|
6
|
-
// Set up the repository URL
|
|
7
|
-
const repoUrl = `https://${process.env.GIT_KEY}@github.com/cunev/rhythia-api.git`;
|
|
8
|
-
|
|
9
|
-
const sourceBranch = process.env.SOURCE_BRANCH!;
|
|
10
|
-
const targetBranch = process.env.TARGET_BRANCH!;
|
|
11
|
-
|
|
12
|
-
async function cloneBranch() {
|
|
13
|
-
const dir = path.join("/tmp", "repo");
|
|
14
|
-
|
|
15
|
-
try {
|
|
16
|
-
// Ensure the directory is clean before cloning
|
|
17
|
-
if (fs.existsSync(dir)) {
|
|
18
|
-
fs.rmdirSync(dir, { recursive: true });
|
|
19
|
-
console.log(`Deleted existing directory: ${dir}`);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Clone the repository into a temporary directory
|
|
23
|
-
await git.clone({
|
|
24
|
-
fs,
|
|
25
|
-
http,
|
|
26
|
-
dir,
|
|
27
|
-
url: repoUrl,
|
|
28
|
-
ref: sourceBranch,
|
|
29
|
-
singleBranch: true,
|
|
30
|
-
depth: 1,
|
|
31
|
-
});
|
|
32
|
-
console.log(`Cloned ${sourceBranch} branch from remote repository.`);
|
|
33
|
-
|
|
34
|
-
// Check out the source branch
|
|
35
|
-
await git.checkout({ fs, dir, ref: sourceBranch });
|
|
36
|
-
console.log(`Checked out to branch: ${sourceBranch}`);
|
|
37
|
-
|
|
38
|
-
// Pull the latest changes from the source branch
|
|
39
|
-
await git.pull({
|
|
40
|
-
fs,
|
|
41
|
-
http,
|
|
42
|
-
dir,
|
|
43
|
-
ref: sourceBranch,
|
|
44
|
-
singleBranch: true,
|
|
45
|
-
author: {
|
|
46
|
-
name: process.env.GIT_USER,
|
|
47
|
-
email: "you@example.com", // Replace with the actual email
|
|
48
|
-
},
|
|
49
|
-
});
|
|
50
|
-
console.log(`Pulled latest changes from branch: ${sourceBranch}`);
|
|
51
|
-
|
|
52
|
-
// Create and checkout the target branch from the source branch
|
|
53
|
-
await git.branch({ fs, dir, ref: targetBranch });
|
|
54
|
-
await git.checkout({ fs, dir, ref: targetBranch });
|
|
55
|
-
console.log(`Created and checked out to branch: ${targetBranch}`);
|
|
56
|
-
|
|
57
|
-
// Push the new branch to the remote repository
|
|
58
|
-
await git.push({
|
|
59
|
-
fs,
|
|
60
|
-
http,
|
|
61
|
-
dir,
|
|
62
|
-
remote: "origin",
|
|
63
|
-
ref: targetBranch,
|
|
64
|
-
force: true,
|
|
65
|
-
onAuth: () => ({
|
|
66
|
-
username: process.env.GIT_KEY, // GitHub token or password
|
|
67
|
-
password: "",
|
|
68
|
-
}),
|
|
69
|
-
});
|
|
70
|
-
console.log(`Pushed new branch to remote: ${targetBranch}`);
|
|
71
|
-
} catch (error) {
|
|
72
|
-
console.error("Error performing Git operations:", error.message);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
cloneBranch();
|
package/scripts/test.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { createClient } from "@supabase/supabase-js";
|
|
2
|
-
import { Database } from "../types/database";
|
|
3
|
-
import { SSPMParser } from "../utils/star-calc/sspmParser";
|
|
4
|
-
import { rateMap, rateMapOld } from "../utils/star-calc";
|
|
5
|
-
import { V1SSPMParser } from "../utils/star-calc/sspmv1Parser";
|
|
6
|
-
|
|
7
|
-
export const supabase = createClient<Database>(
|
|
8
|
-
`https://pfkajngbllcbdzoylrvp.supabase.co`,
|
|
9
|
-
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InBma2FqbmdibGxjYmR6b3lscnZwIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTcxODU3NjA3MCwiZXhwIjoyMDM0MTUyMDcwfQ.XKUlQWvzmcYyirM-Zi4nwhiEKcpx1xLS97QUyuR3MoY",
|
|
10
|
-
{
|
|
11
|
-
auth: {
|
|
12
|
-
autoRefreshToken: false,
|
|
13
|
-
persistSession: false,
|
|
14
|
-
},
|
|
15
|
-
}
|
|
16
|
-
);
|
|
17
|
-
|
|
18
|
-
async function main() {
|
|
19
|
-
let qry = supabase
|
|
20
|
-
.from("beatmapPages")
|
|
21
|
-
.select(
|
|
22
|
-
`
|
|
23
|
-
owner,
|
|
24
|
-
created_at,
|
|
25
|
-
id,
|
|
26
|
-
status,
|
|
27
|
-
beatmaps!inner(
|
|
28
|
-
playcount,
|
|
29
|
-
ranked,
|
|
30
|
-
beatmapFile,
|
|
31
|
-
image,
|
|
32
|
-
starRating,
|
|
33
|
-
difficulty,
|
|
34
|
-
title
|
|
35
|
-
),
|
|
36
|
-
profiles!inner(
|
|
37
|
-
username
|
|
38
|
-
)`
|
|
39
|
-
)
|
|
40
|
-
.order("created_at", { ascending: false });
|
|
41
|
-
|
|
42
|
-
qry = qry.ilike("beatmaps.title", "%Ca%");
|
|
43
|
-
qry = qry.ilike("profiles.username", "%Ca%");
|
|
44
|
-
|
|
45
|
-
const data = await qry;
|
|
46
|
-
console.log(data);
|
|
47
|
-
// console.log(data.data[]);
|
|
48
|
-
// const req = await fetch(`https://cdn.rhythia.net/index.json`);
|
|
49
|
-
// const json = Object.values(await req.json()) as any[];
|
|
50
|
-
// console.log(json[0]);
|
|
51
|
-
|
|
52
|
-
// let count = 0;
|
|
53
|
-
// for (const map of json) {
|
|
54
|
-
// const request = await fetch(map.download);
|
|
55
|
-
// const bytes = await request.arrayBuffer();
|
|
56
|
-
// const parser = new SSPMParser(Buffer.from(bytes));
|
|
57
|
-
// let rate = 0;
|
|
58
|
-
// try {
|
|
59
|
-
// const data = parser.parse();
|
|
60
|
-
// rate = rateMap(data);
|
|
61
|
-
// } catch (error) {
|
|
62
|
-
// const parserOld = new V1SSPMParser(Buffer.from(bytes));
|
|
63
|
-
// const data = parserOld.parse();
|
|
64
|
-
|
|
65
|
-
// rate = rateMapOld(data);
|
|
66
|
-
// }
|
|
67
|
-
// await supabase.from("beatmaps").upsert({
|
|
68
|
-
// beatmapHash: map.id,
|
|
69
|
-
// starRating: rate,
|
|
70
|
-
// });
|
|
71
|
-
// count++;
|
|
72
|
-
// console.log(count, json.length, map.id, rate);
|
|
73
|
-
// }
|
|
74
|
-
// for (const map of json) {
|
|
75
|
-
// const reqMap = await fetch(map.link);
|
|
76
|
-
// const buff = await reqMap.arrayBuffer();
|
|
77
|
-
// writeFileSync("./toSubmit/" + map.id + ".sspm", Buffer.from(buff));
|
|
78
|
-
// }
|
|
79
|
-
|
|
80
|
-
// const { data: mapData, error } = await supabase
|
|
81
|
-
// .from("beatmapPages")
|
|
82
|
-
// .select("id,nominations,owner,status")
|
|
83
|
-
// .eq("owner", 10)
|
|
84
|
-
// .eq("status", "RANKED");
|
|
85
|
-
|
|
86
|
-
// if (!mapData) {
|
|
87
|
-
// throw "lolz";
|
|
88
|
-
// }
|
|
89
|
-
|
|
90
|
-
// for (const element of mapData) {
|
|
91
|
-
|
|
92
|
-
// console.log(data.error);
|
|
93
|
-
// }
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
main();
|
package/scripts/update.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { $ } from "bun";
|
|
2
|
-
import { readdirSync, readFileSync, writeFileSync } from "fs";
|
|
3
|
-
import { lowerFirst, upperFirst } from "lodash";
|
|
4
|
-
import path from "path";
|
|
5
|
-
const packageJson = JSON.parse(readFileSync("./package.json", "utf-8"));
|
|
6
|
-
|
|
7
|
-
const versions = packageJson.version.split(".");
|
|
8
|
-
versions[0] = Number(versions[0]) + 1;
|
|
9
|
-
|
|
10
|
-
packageJson.version = versions.join(".");
|
|
11
|
-
|
|
12
|
-
writeFileSync("./package.json", JSON.stringify(packageJson, null, 2));
|
|
13
|
-
|
|
14
|
-
const apis = readdirSync("./api");
|
|
15
|
-
|
|
16
|
-
const exports: string[] = [];
|
|
17
|
-
exports.push(`import { handleApi } from "./handleApi"`);
|
|
18
|
-
|
|
19
|
-
for (const api of apis) {
|
|
20
|
-
if (
|
|
21
|
-
!readFileSync(path.join("./api", api), "utf-8").includes(
|
|
22
|
-
"export const Schema"
|
|
23
|
-
)
|
|
24
|
-
) {
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
exports.push(`\n// ./api/${api} API`);
|
|
28
|
-
|
|
29
|
-
const apiName = path.parse(api).name;
|
|
30
|
-
exports.push(
|
|
31
|
-
`import { Schema as ${upperFirst(apiName)} } from "./api/${apiName}"`
|
|
32
|
-
);
|
|
33
|
-
exports.push(
|
|
34
|
-
`export { Schema as Schema${upperFirst(apiName)} } from "./api/${apiName}"`
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
exports.push(
|
|
38
|
-
`export const ${lowerFirst(
|
|
39
|
-
apiName
|
|
40
|
-
)} = handleApi({url:"/api/${apiName}",...${upperFirst(apiName)}})`
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
exports.push(`export { handleApi } from "./handleApi"`);
|
|
44
|
-
|
|
45
|
-
writeFileSync("./index.ts", exports.join("\n"));
|
|
46
|
-
|
|
47
|
-
// const conf = readFileSync("./.cred", "utf-8");
|
|
48
|
-
// await $`npm logout`.nothrow();
|
|
49
|
-
await $`yarn publish`;
|