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.
- package/.prettierrc.json +6 -6
- package/api/addCollectionMap.ts +82 -82
- package/api/approveMap.ts +78 -78
- package/api/chartPublicStats.ts +32 -32
- package/api/createBeatmap.ts +168 -156
- package/api/createBeatmapPage.ts +64 -64
- package/api/createClan.ts +81 -81
- package/api/createCollection.ts +58 -58
- package/api/deleteBeatmapPage.ts +77 -72
- package/api/deleteCollection.ts +59 -59
- package/api/deleteCollectionMap.ts +71 -71
- package/api/editAboutMe.ts +91 -91
- package/api/editClan.ts +90 -90
- package/api/editCollection.ts +77 -77
- package/api/editProfile.ts +123 -123
- package/api/getAvatarUploadUrl.ts +85 -85
- package/api/getBadgedUsers.ts +56 -56
- package/api/getBeatmapComments.ts +57 -57
- package/api/getBeatmapPage.ts +106 -106
- package/api/getBeatmapPageById.ts +99 -99
- package/api/getBeatmapStarRating.ts +53 -53
- package/api/getBeatmaps.ts +159 -159
- package/api/getClan.ts +77 -77
- package/api/getCollection.ts +130 -130
- package/api/getCollections.ts +128 -126
- package/api/getLeaderboard.ts +136 -136
- package/api/getMapUploadUrl.ts +93 -93
- package/api/getPassToken.ts +55 -55
- package/api/getProfile.ts +146 -146
- package/api/getPublicStats.ts +180 -180
- package/api/getRawStarRating.ts +57 -57
- package/api/getScore.ts +85 -85
- package/api/getTimestamp.ts +23 -23
- package/api/getUserScores.ts +175 -175
- package/api/nominateMap.ts +82 -69
- package/api/postBeatmapComment.ts +59 -59
- package/api/rankMapsArchive.ts +64 -64
- package/api/searchUsers.ts +56 -56
- package/api/setPasskey.ts +59 -59
- package/api/submitScore.ts +433 -433
- package/api/updateBeatmapPage.ts +229 -229
- package/handleApi.ts +20 -20
- package/index.html +2 -2
- package/index.ts +865 -864
- package/package.json +2 -2
- package/types/database.ts +39 -0
- package/utils/getUserBySession.ts +48 -48
- package/utils/requestUtils.ts +87 -87
- package/utils/security.ts +20 -20
- package/utils/star-calc/index.ts +72 -72
- package/utils/star-calc/osuUtils.ts +53 -53
- package/utils/star-calc/sspmParser.ts +398 -398
- package/utils/star-calc/sspmv1Parser.ts +165 -165
- package/utils/supabase.ts +13 -13
- package/utils/test +4 -4
- package/utils/validateToken.ts +7 -7
- package/vercel.json +12 -12
- package/package-lock.json +0 -8913
package/api/createBeatmap.ts
CHANGED
|
@@ -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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
) {
|
|
73
|
-
return NextResponse.json(
|
|
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
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
+
}
|
package/api/createBeatmapPage.ts
CHANGED
|
@@ -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
|
+
}
|