rhythia-api 104.0.0 → 105.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/editAboutMe.ts +89 -0
- package/api/editProfile.ts +0 -1
- package/api/getAvatarUploadUrl.ts +9 -3
- package/api/getProfile.ts +2 -1
- package/index.ts +5 -0
- package/package.json +1 -1
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
import z from "zod";
|
|
3
|
+
import { Database } from "../types/database";
|
|
4
|
+
import { protectedApi, validUser } from "../utils/requestUtils";
|
|
5
|
+
import { supabase } from "../utils/supabase";
|
|
6
|
+
export const Schema = {
|
|
7
|
+
input: z.strictObject({
|
|
8
|
+
session: z.string(),
|
|
9
|
+
data: z.object({
|
|
10
|
+
about_me: z.string().optional(),
|
|
11
|
+
}),
|
|
12
|
+
}),
|
|
13
|
+
output: z.object({
|
|
14
|
+
error: z.string().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
|
+
data: (typeof Schema)["input"]["_type"]
|
|
29
|
+
): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
|
|
30
|
+
if (!data.data.about_me) {
|
|
31
|
+
return NextResponse.json(
|
|
32
|
+
{
|
|
33
|
+
error: "Missing body.",
|
|
34
|
+
},
|
|
35
|
+
{ status: 404 }
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (data.data.about_me.length > 500) {
|
|
40
|
+
return NextResponse.json(
|
|
41
|
+
{
|
|
42
|
+
error: "Too long.",
|
|
43
|
+
},
|
|
44
|
+
{ status: 404 }
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const user = (await supabase.auth.getUser(data.session)).data.user!;
|
|
49
|
+
let userData: Database["public"]["Tables"]["profiles"]["Update"];
|
|
50
|
+
|
|
51
|
+
// Find user's entry
|
|
52
|
+
{
|
|
53
|
+
let { data: queryUserData, error } = await supabase
|
|
54
|
+
.from("profiles")
|
|
55
|
+
.select("*")
|
|
56
|
+
.eq("uid", user.id);
|
|
57
|
+
|
|
58
|
+
if (!queryUserData?.length) {
|
|
59
|
+
return NextResponse.json(
|
|
60
|
+
{
|
|
61
|
+
error: "User cannot be retrieved from session",
|
|
62
|
+
},
|
|
63
|
+
{ status: 404 }
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
userData = queryUserData[0];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const upsertPayload: Database["public"]["Tables"]["profiles"]["Update"] = {
|
|
70
|
+
id: userData.id,
|
|
71
|
+
about_me: data.data.about_me,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const upsertResult = await supabase
|
|
75
|
+
.from("profiles")
|
|
76
|
+
.upsert(upsertPayload)
|
|
77
|
+
.select();
|
|
78
|
+
|
|
79
|
+
if (upsertResult.error) {
|
|
80
|
+
return NextResponse.json(
|
|
81
|
+
{
|
|
82
|
+
error: "Can't update..",
|
|
83
|
+
},
|
|
84
|
+
{ status: 404 }
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return NextResponse.json({});
|
|
89
|
+
}
|
package/api/editProfile.ts
CHANGED
|
@@ -3,10 +3,15 @@ import z from "zod";
|
|
|
3
3
|
import { protectedApi, validUser } from "../utils/requestUtils";
|
|
4
4
|
import { supabase } from "../utils/supabase";
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
PutBucketCorsCommand,
|
|
8
|
+
PutObjectCommand,
|
|
9
|
+
S3Client,
|
|
10
|
+
} from "@aws-sdk/client-s3";
|
|
7
11
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
8
12
|
|
|
9
13
|
const s3Client = new S3Client({
|
|
14
|
+
region: "auto",
|
|
10
15
|
endpoint: "https://s3.eu-central-003.backblazeb2.com",
|
|
11
16
|
credentials: {
|
|
12
17
|
secretAccessKey: "K0039mm4iKsteQOXpZSzf0+VDzuH89U",
|
|
@@ -51,9 +56,10 @@ export async function handler({
|
|
|
51
56
|
});
|
|
52
57
|
}
|
|
53
58
|
|
|
59
|
+
const key = `user-avatar-${Date.now()}-${user.id}`;
|
|
54
60
|
const command = new PutObjectCommand({
|
|
55
61
|
Bucket: "rhthia-avatars",
|
|
56
|
-
Key:
|
|
62
|
+
Key: key,
|
|
57
63
|
ContentLength: contentLength,
|
|
58
64
|
ContentType: contentType,
|
|
59
65
|
});
|
|
@@ -63,6 +69,6 @@ export async function handler({
|
|
|
63
69
|
});
|
|
64
70
|
return NextResponse.json({
|
|
65
71
|
url: presigned,
|
|
66
|
-
objectKey:
|
|
72
|
+
objectKey: key,
|
|
67
73
|
});
|
|
68
74
|
}
|
package/api/getProfile.ts
CHANGED
|
@@ -84,7 +84,8 @@ export async function handler(
|
|
|
84
84
|
.upsert({
|
|
85
85
|
uid: user.id,
|
|
86
86
|
about_me: "",
|
|
87
|
-
avatar_url:
|
|
87
|
+
avatar_url:
|
|
88
|
+
"https://rhthia-avatars.s3.eu-central-003.backblazeb2.com/user-avatar-1725309193296-72002e6b-321c-4f60-a692-568e0e75147d",
|
|
88
89
|
badges: ["Early Bird"],
|
|
89
90
|
username: `${user.user_metadata.full_name.slice(0, 20)}${Math.round(
|
|
90
91
|
Math.random() * 900000 + 100000
|
package/index.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { handleApi } from "./handleApi"
|
|
2
2
|
|
|
3
|
+
// ./api/editAboutMe.ts API
|
|
4
|
+
import { Schema as EditAboutMe } from "./api/editAboutMe"
|
|
5
|
+
export { Schema as SchemaEditAboutMe } from "./api/editAboutMe"
|
|
6
|
+
export const editAboutMe = handleApi({url:"/api/editAboutMe",...EditAboutMe})
|
|
7
|
+
|
|
3
8
|
// ./api/editProfile.ts API
|
|
4
9
|
import { Schema as EditProfile } from "./api/editProfile"
|
|
5
10
|
export { Schema as SchemaEditProfile } from "./api/editProfile"
|