Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9c3bd7c

Browse files
committedJul 22, 2024·
Refactor code to fix default value typo in userConfig model
1 parent 6e94384 commit 9c3bd7c

File tree

9 files changed

+266
-58
lines changed

9 files changed

+266
-58
lines changed
 

‎fresh.gen.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ import * as $api_v2_client_chart from "./routes/api/v2/client/chart.ts";
1414
import * as $api_v2_client_create_community from "./routes/api/v2/client/create/community.ts";
1515
import * as $api_v2_client_create_group from "./routes/api/v2/client/create/group.ts";
1616
import * as $api_v2_client_csrftoken from "./routes/api/v2/client/csrftoken.ts";
17+
import * as $api_v2_client_friends_accept_friend from "./routes/api/v2/client/friends/accept/friend.ts";
1718
import * as $api_v2_client_friends_info_ID_icon_friend from "./routes/api/v2/client/friends/info/[ID]/icon/friend.ts";
1819
import * as $api_v2_client_friends_info_ID_icon_friendcode from "./routes/api/v2/client/friends/info/[ID]/icon/friendcode.ts";
1920
import * as $api_v2_client_friends_info_ID_icon_group from "./routes/api/v2/client/friends/info/[ID]/icon/group.ts";
2021
import * as $api_v2_client_friends_info_ID_profile_friend from "./routes/api/v2/client/friends/info/[ID]/profile/friend.ts";
2122
import * as $api_v2_client_friends_info_ID_profile_friendcode from "./routes/api/v2/client/friends/info/[ID]/profile/friendcode.ts";
2223
import * as $api_v2_client_friends_info_ID_profile_group from "./routes/api/v2/client/friends/info/[ID]/profile/group.ts";
2324
import * as $api_v2_client_friends_list from "./routes/api/v2/client/friends/list.ts";
24-
import * as $api_v2_client_friends_request_friend from "./routes/api/v2/client/friends/request/friend.ts";
25+
import * as $api_v2_client_friends_request_key from "./routes/api/v2/client/friends/request/key.ts";
2526
import * as $api_v2_client_friends_request_list from "./routes/api/v2/client/friends/request/list.ts";
27+
import * as $api_v2_client_friends_request_name from "./routes/api/v2/client/friends/request/name.ts";
2628
import * as $api_v2_client_main from "./routes/api/v2/client/main.ts";
2729
import * as $api_v2_client_sessions_login from "./routes/api/v2/client/sessions/login.ts";
2830
import * as $api_v2_client_sessions_logout from "./routes/api/v2/client/sessions/logout.ts";
@@ -87,7 +89,9 @@ import * as $api_v2_server_information_talk_ID_image from "./routes/api/v2/serve
8789
import * as $api_v2_server_information_talk_ID_index from "./routes/api/v2/server/information/talk/[ID]/index.ts";
8890
import * as $api_v2_server_information_users_changes from "./routes/api/v2/server/information/users/changes.ts";
8991
import * as $api_v2_server_information_users_icon from "./routes/api/v2/server/information/users/icon.ts";
92+
import * as $api_v2_server_information_users_name from "./routes/api/v2/server/information/users/name.ts";
9093
import * as $api_v2_server_information_users_profile from "./routes/api/v2/server/information/users/profile.ts";
94+
import * as $api_v2_server_information_users_uuid from "./routes/api/v2/server/information/users/uuid.ts";
9195
import * as $api_v2_server_pubkey from "./routes/api/v2/server/pubkey.ts";
9296
import * as $home_friends_name_ from "./routes/home/friends/[name].tsx";
9397
import * as $home_index from "./routes/home/index.tsx";
@@ -127,15 +131,17 @@ const manifest = {
127131
"./routes/api/v2/client/create/community.ts": $api_v2_client_create_community,
128132
"./routes/api/v2/client/create/group.ts": $api_v2_client_create_group,
129133
"./routes/api/v2/client/csrftoken.ts": $api_v2_client_csrftoken,
134+
"./routes/api/v2/client/friends/accept/friend.ts": $api_v2_client_friends_accept_friend,
130135
"./routes/api/v2/client/friends/info/[ID]/icon/friend.ts": $api_v2_client_friends_info_ID_icon_friend,
131136
"./routes/api/v2/client/friends/info/[ID]/icon/friendcode.ts": $api_v2_client_friends_info_ID_icon_friendcode,
132137
"./routes/api/v2/client/friends/info/[ID]/icon/group.ts": $api_v2_client_friends_info_ID_icon_group,
133138
"./routes/api/v2/client/friends/info/[ID]/profile/friend.ts": $api_v2_client_friends_info_ID_profile_friend,
134139
"./routes/api/v2/client/friends/info/[ID]/profile/friendcode.ts": $api_v2_client_friends_info_ID_profile_friendcode,
135140
"./routes/api/v2/client/friends/info/[ID]/profile/group.ts": $api_v2_client_friends_info_ID_profile_group,
136141
"./routes/api/v2/client/friends/list.ts": $api_v2_client_friends_list,
137-
"./routes/api/v2/client/friends/request/friend.ts": $api_v2_client_friends_request_friend,
142+
"./routes/api/v2/client/friends/request/key.ts": $api_v2_client_friends_request_key,
138143
"./routes/api/v2/client/friends/request/list.ts": $api_v2_client_friends_request_list,
144+
"./routes/api/v2/client/friends/request/name.ts": $api_v2_client_friends_request_name,
139145
"./routes/api/v2/client/main.ts": $api_v2_client_main,
140146
"./routes/api/v2/client/sessions/login.ts": $api_v2_client_sessions_login,
141147
"./routes/api/v2/client/sessions/logout.ts": $api_v2_client_sessions_logout,
@@ -200,7 +206,9 @@ const manifest = {
200206
"./routes/api/v2/server/information/talk/[ID]/index.ts": $api_v2_server_information_talk_ID_index,
201207
"./routes/api/v2/server/information/users/changes.ts": $api_v2_server_information_users_changes,
202208
"./routes/api/v2/server/information/users/icon.ts": $api_v2_server_information_users_icon,
209+
"./routes/api/v2/server/information/users/name.ts": $api_v2_server_information_users_name,
203210
"./routes/api/v2/server/information/users/profile.ts": $api_v2_server_information_users_profile,
211+
"./routes/api/v2/server/information/users/uuid.ts": $api_v2_server_information_users_uuid,
204212
"./routes/api/v2/server/pubkey.ts": $api_v2_server_pubkey,
205213
"./routes/home/friends/[name].tsx": $home_friends_name_,
206214
"./routes/home/index.tsx": $home_index,

‎routes/api/v2/client/block/server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const handler = {
1414
}
1515
const body = await req.json();
1616
const domain = body.domain;
17-
if (await takos.checkCsrfToken(body.csrftoken) === false) {
17+
if (await takos.checkCsrfToken(body.csrftoken, ctx.state.data.sessionid) === false) {
1818
return new Response(JSON.stringify({ status: false, message: "Invalid CSRF token" }), {
1919
headers: { "Content-Type": "application/json" },
2020
status: 400,

‎routes/api/v2/client/block/user.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const handler = {
2626
}
2727
const userName = takos.splitUserName(userid).userName;
2828
const userDomain = takos.splitUserName(userid).domain;
29-
if (await takos.checkCsrfToken(body.csrftoken) === false) {
29+
if (await takos.checkCsrfToken(body.csrftoken, ctx.state.data.sessionid) === false) {
3030
return new Response(JSON.stringify({ status: false, message: "Invalid CSRF token" }), {
3131
headers: { "Content-Type": "application/json" },
3232
status: 400,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//フレンド申請を承認する
2+
// POST /api/v2/client/friends/accept/friend
3+
// { friendid: string}
4+
// -> { status: boolean }
5+
import friends from "../../../../../../models/friends.ts";
6+
import users from "../../../../../../models/users.ts";

‎routes/api/v2/client/friends/request/friend.ts

-54
This file was deleted.

‎routes/api/v2/client/friends/request/key.ts

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import takos from "../../../../../../util/takos.ts";
2+
import userConfig from "../../../../../../models/userConfig.ts";
3+
import remoteFriends from "../../../../../../models/remoteFriends.ts";
4+
import friends from "../../../../../../models/friends.ts";
5+
import requestAddFriend from "../../../../../../models/reqestAddFriend.ts";
6+
import { load } from "$std/dotenv/mod.ts";
7+
import users from "../../../../../../models/users.ts";
8+
const env = await load();
9+
export const handler = {
10+
async GET(req: Request, ctx: any) {
11+
if (!ctx.state.data.loggedIn) {
12+
return new Response(JSON.stringify({ status: false, message: "Not Logged In" }));
13+
}
14+
const userid = ctx.state.data.userid;
15+
let body;
16+
try {
17+
body = await req.json();
18+
} catch (e) {
19+
return new Response(JSON.stringify({ status: false, message: "Invalid request" }), {
20+
headers: { "Content-Type": "application/json" },
21+
status: 400,
22+
});
23+
}
24+
const { userName, csrftoken } = body;
25+
if (typeof userName !== "string") {
26+
return new Response(JSON.stringify({ status: false, message: "Invalid userName" }), {
27+
headers: { "Content-Type": "application/json" },
28+
status: 400,
29+
});
30+
}
31+
if (typeof csrftoken !== "string") {
32+
return new Response(JSON.stringify({ status: false, message: "Invalid csrftoken" }), {
33+
headers: { "Content-Type": "application/json" },
34+
status: 400,
35+
});
36+
}
37+
if (takos.checkUserName(userName) === false) {
38+
return new Response(JSON.stringify({ status: false, message: "Invalid" }), {
39+
headers: { "Content-Type": "application/json" },
40+
status: 400,
41+
});
42+
}
43+
if (await takos.checkCsrfToken(csrftoken, userid) === false) {
44+
return new Response(JSON.stringify({ status: false, message: "Invalid CSRF token" }), {
45+
headers: { "Content-Type": "application/json" },
46+
status: 400,
47+
});
48+
}
49+
const userDomain = takos.splitUserName(userName).domain;
50+
if (userDomain !== env["DOMAIN"]) {
51+
//friendのuuidを取得
52+
const response = await fetch(`https://${userDomain}/api/v2/server/information/users/uuid`, {
53+
method: "POST",
54+
headers: {
55+
"Content-Type": "application/json",
56+
},
57+
body: JSON.stringify({ host: env["DOMAIN"], body: JSON.stringify({ userName }), signature: takos.signData(JSON.stringify({ userName }), await takos.getPrivateKey()) }),
58+
});
59+
const data = await response.json();
60+
if (data.status === false) {
61+
return new Response(JSON.stringify({ status: false, message: "User not found" }), {
62+
headers: { "Content-Type": "application/json" },
63+
status: 404,
64+
});
65+
}
66+
const friendId = data.userName;
67+
//すでにリクエストを送っているか確認
68+
const request = await requestAddFriend.findOne({ userid });
69+
if (request == null) {
70+
return new Response(JSON.stringify({ status: false, message: "Already requested" }), {
71+
headers: { "Content-Type": "application/json" },
72+
status: 400,
73+
});
74+
}
75+
const isAleredyRequested = request.requestedUser.find((user) => user.userID === friendId);
76+
if (isAleredyRequested) {
77+
return new Response(JSON.stringify({ status: false, message: "Already requested" }), {
78+
headers: { "Content-Type": "application/json" },
79+
status: 400,
80+
});
81+
}
82+
//すでに友達か確認
83+
const friendData = await friends.findOne({ user: userid });
84+
if (friendData == null) {
85+
return new Response(JSON.stringify({ status: false, message: "User not found" }), {
86+
headers: { "Content-Type": "application/json" },
87+
status: 404,
88+
});
89+
}
90+
const isFriend = friendData.friends.find((friend) => friend.userid === friendId);
91+
if (isFriend) {
92+
return new Response(JSON.stringify({ status: false, message: "Already friend" }), {
93+
headers: { "Content-Type": "application/json" },
94+
status: 400,
95+
});
96+
}
97+
//リクエストを送る
98+
const res = await fetch(`https://${userDomain}/api/v2/server/friends/request/friend`, {
99+
method: "POST",
100+
headers: {
101+
"Content-Type": "application/json",
102+
},
103+
body: JSON.stringify({
104+
host: env["DOMAIN"],
105+
body: JSON.stringify({ userid, friendid: friendId }),
106+
signature: takos.signData(JSON.stringify({ userid, friendid: friendId }), await takos.getPrivateKey()),
107+
}),
108+
});
109+
const resData = await res.json();
110+
if (resData.status === false) {
111+
return new Response(JSON.stringify({ status: false, message: "Failed to send request" }), {
112+
headers: { "Content-Type": "application/json" },
113+
status: 500,
114+
});
115+
}
116+
//リクエストを送信したことを記録
117+
await requestAddFriend.updateOne({ userid }, { $push: { requestedUser: { userID: friendId } } });
118+
return new Response(JSON.stringify({ status: true }), {
119+
headers: { "Content-Type": "application/json" },
120+
});
121+
} else {
122+
const friendInfo = await users.findOne({ userName: takos.splitUserName(userName).userName });
123+
if (friendInfo === null) {
124+
return new Response(JSON.stringify({ status: false, message: "User not found" }), {
125+
headers: { "Content-Type": "application/json" },
126+
status: 404,
127+
});
128+
}
129+
const friendData = await friends.findOne({ user: userid });
130+
if (friendData == null) {
131+
return new Response(JSON.stringify({ status: false, message: "User not found" }), {
132+
headers: { "Content-Type": "application/json" },
133+
status: 404,
134+
});
135+
}
136+
const isFriend = friendData.friends.find((friend) => friend.userid === friendInfo.uuid);
137+
if (isFriend) {
138+
return new Response(JSON.stringify({ status: false, message: "Already friend" }), {
139+
headers: { "Content-Type": "application/json" },
140+
status: 400,
141+
});
142+
}
143+
if (friendData === null) {
144+
return new Response(JSON.stringify({ status: false, message: "User not found" }), {
145+
headers: { "Content-Type": "application/json" },
146+
status: 404,
147+
});
148+
}
149+
const isRequested = await requestAddFriend.findOne({ userid: friendInfo.uuid });
150+
if (isRequested == null) {
151+
return new Response(JSON.stringify({ status: false, message: "Already requested" }), {
152+
headers: { "Content-Type": "application/json" },
153+
status: 400,
154+
});
155+
}
156+
const isAleredyRequested = isRequested.friendRequester.find((user) => user.userID === userid);
157+
if (isAleredyRequested) {
158+
return new Response(JSON.stringify({ status: false, message: "Already requested" }), {
159+
headers: { "Content-Type": "application/json" },
160+
status: 400,
161+
});
162+
}
163+
//リクエストを送る
164+
await requestAddFriend.updateOne({ userid: friendInfo.uuid }, { $push: { friendRequester: { userID: userid } } });
165+
await requestAddFriend.updateOne({ userid }, { $push: { requestedUser: { userID: friendInfo.uuid } } });
166+
return new Response(JSON.stringify({ status: true }), {
167+
headers: { "Content-Type": "application/json" },
168+
});
169+
}
170+
},
171+
};
172+
//{ host: string, body: string, signature: string }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//uuidとuserNameを変換する
2+
// POST /api/v2/server/information/users/uuid
3+
// { host: string, body: string, signature: string }
4+
// signatureは秘密鍵で署名されたJSON
5+
// body: { uuid: string }
6+
import users from "../../../../../../models/users.ts";
7+
import takos from "../../../../../../util/takos.ts";
8+
export const handler = {
9+
async POST(req: any, ctx: any) {
10+
const body = await req.json();
11+
const host = body.host;
12+
const publickey = await fetch(`https://${host}/api/v2/server/pubkey`).then((res) => res.json()).then((data) => data.publickey);
13+
const verify = await takos.verifySignature(publickey, body.signature, body.body);
14+
if (!verify) {
15+
return new Response(JSON.stringify({
16+
status: false,
17+
message: "Invalid Signature",
18+
}));
19+
}
20+
const data = JSON.parse(body.body);
21+
const uuid = data.uuid;
22+
const userInfo = await users.findOne({ uuid });
23+
if (userInfo === null) {
24+
return new Response(JSON.stringify({
25+
status: false,
26+
message: "User not found",
27+
}));
28+
}
29+
const result = JSON.stringify({
30+
status: true,
31+
userName: userInfo.userName,
32+
});
33+
const signature = await takos.signData(result, await takos.getPrivateKey());
34+
return new Response(JSON.stringify({
35+
status: true,
36+
result,
37+
signature: new Uint8Array(signature),
38+
}));
39+
},
40+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//uuidとuserNameを変換する
2+
import users from "../../../../../../models/users.ts";
3+
import takos from "../../../../../../util/takos.ts";
4+
export const handler = {
5+
async POST(req: any, ctx: any) {
6+
const body = await req.json();
7+
const host = body.host;
8+
const publickey = await fetch(`https://${host}/api/v2/server/pubkey`).then((res) => res.json()).then((data) => data.publickey);
9+
const verify = await takos.verifySignature(publickey, body.signature, body.body);
10+
if (!verify) {
11+
return new Response(JSON.stringify({
12+
status: false,
13+
message: "Invalid Signature",
14+
}));
15+
}
16+
const data = JSON.parse(body.body);
17+
const userName = data.userName;
18+
const userInfo = await users.findOne({ userName });
19+
if (userInfo === null) {
20+
return new Response(JSON.stringify({
21+
status: false,
22+
message: "User not found",
23+
}));
24+
}
25+
const result = JSON.stringify({
26+
status: true,
27+
userName: userInfo.uuid,
28+
});
29+
const signature = await takos.signData(result, await takos.getPrivateKey());
30+
return new Response(JSON.stringify({
31+
status: true,
32+
result,
33+
signature: new Uint8Array(signature),
34+
}));
35+
},
36+
};

0 commit comments

Comments
 (0)
Please sign in to comment.