Skip to content

Commit

Permalink
Refactor code to fix default value typo in userConfig model
Browse files Browse the repository at this point in the history
  • Loading branch information
tako0614 committed Jul 22, 2024
1 parent 6e94384 commit 9c3bd7c
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 58 deletions.
12 changes: 10 additions & 2 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ import * as $api_v2_client_chart from "./routes/api/v2/client/chart.ts";
import * as $api_v2_client_create_community from "./routes/api/v2/client/create/community.ts";
import * as $api_v2_client_create_group from "./routes/api/v2/client/create/group.ts";
import * as $api_v2_client_csrftoken from "./routes/api/v2/client/csrftoken.ts";
import * as $api_v2_client_friends_accept_friend from "./routes/api/v2/client/friends/accept/friend.ts";
import * as $api_v2_client_friends_info_ID_icon_friend from "./routes/api/v2/client/friends/info/[ID]/icon/friend.ts";
import * as $api_v2_client_friends_info_ID_icon_friendcode from "./routes/api/v2/client/friends/info/[ID]/icon/friendcode.ts";
import * as $api_v2_client_friends_info_ID_icon_group from "./routes/api/v2/client/friends/info/[ID]/icon/group.ts";
import * as $api_v2_client_friends_info_ID_profile_friend from "./routes/api/v2/client/friends/info/[ID]/profile/friend.ts";
import * as $api_v2_client_friends_info_ID_profile_friendcode from "./routes/api/v2/client/friends/info/[ID]/profile/friendcode.ts";
import * as $api_v2_client_friends_info_ID_profile_group from "./routes/api/v2/client/friends/info/[ID]/profile/group.ts";
import * as $api_v2_client_friends_list from "./routes/api/v2/client/friends/list.ts";
import * as $api_v2_client_friends_request_friend from "./routes/api/v2/client/friends/request/friend.ts";
import * as $api_v2_client_friends_request_key from "./routes/api/v2/client/friends/request/key.ts";
import * as $api_v2_client_friends_request_list from "./routes/api/v2/client/friends/request/list.ts";
import * as $api_v2_client_friends_request_name from "./routes/api/v2/client/friends/request/name.ts";
import * as $api_v2_client_main from "./routes/api/v2/client/main.ts";
import * as $api_v2_client_sessions_login from "./routes/api/v2/client/sessions/login.ts";
import * as $api_v2_client_sessions_logout from "./routes/api/v2/client/sessions/logout.ts";
Expand Down Expand Up @@ -87,7 +89,9 @@ import * as $api_v2_server_information_talk_ID_image from "./routes/api/v2/serve
import * as $api_v2_server_information_talk_ID_index from "./routes/api/v2/server/information/talk/[ID]/index.ts";
import * as $api_v2_server_information_users_changes from "./routes/api/v2/server/information/users/changes.ts";
import * as $api_v2_server_information_users_icon from "./routes/api/v2/server/information/users/icon.ts";
import * as $api_v2_server_information_users_name from "./routes/api/v2/server/information/users/name.ts";
import * as $api_v2_server_information_users_profile from "./routes/api/v2/server/information/users/profile.ts";
import * as $api_v2_server_information_users_uuid from "./routes/api/v2/server/information/users/uuid.ts";
import * as $api_v2_server_pubkey from "./routes/api/v2/server/pubkey.ts";
import * as $home_friends_name_ from "./routes/home/friends/[name].tsx";
import * as $home_index from "./routes/home/index.tsx";
Expand Down Expand Up @@ -127,15 +131,17 @@ const manifest = {
"./routes/api/v2/client/create/community.ts": $api_v2_client_create_community,
"./routes/api/v2/client/create/group.ts": $api_v2_client_create_group,
"./routes/api/v2/client/csrftoken.ts": $api_v2_client_csrftoken,
"./routes/api/v2/client/friends/accept/friend.ts": $api_v2_client_friends_accept_friend,
"./routes/api/v2/client/friends/info/[ID]/icon/friend.ts": $api_v2_client_friends_info_ID_icon_friend,
"./routes/api/v2/client/friends/info/[ID]/icon/friendcode.ts": $api_v2_client_friends_info_ID_icon_friendcode,
"./routes/api/v2/client/friends/info/[ID]/icon/group.ts": $api_v2_client_friends_info_ID_icon_group,
"./routes/api/v2/client/friends/info/[ID]/profile/friend.ts": $api_v2_client_friends_info_ID_profile_friend,
"./routes/api/v2/client/friends/info/[ID]/profile/friendcode.ts": $api_v2_client_friends_info_ID_profile_friendcode,
"./routes/api/v2/client/friends/info/[ID]/profile/group.ts": $api_v2_client_friends_info_ID_profile_group,
"./routes/api/v2/client/friends/list.ts": $api_v2_client_friends_list,
"./routes/api/v2/client/friends/request/friend.ts": $api_v2_client_friends_request_friend,
"./routes/api/v2/client/friends/request/key.ts": $api_v2_client_friends_request_key,
"./routes/api/v2/client/friends/request/list.ts": $api_v2_client_friends_request_list,
"./routes/api/v2/client/friends/request/name.ts": $api_v2_client_friends_request_name,
"./routes/api/v2/client/main.ts": $api_v2_client_main,
"./routes/api/v2/client/sessions/login.ts": $api_v2_client_sessions_login,
"./routes/api/v2/client/sessions/logout.ts": $api_v2_client_sessions_logout,
Expand Down Expand Up @@ -200,7 +206,9 @@ const manifest = {
"./routes/api/v2/server/information/talk/[ID]/index.ts": $api_v2_server_information_talk_ID_index,
"./routes/api/v2/server/information/users/changes.ts": $api_v2_server_information_users_changes,
"./routes/api/v2/server/information/users/icon.ts": $api_v2_server_information_users_icon,
"./routes/api/v2/server/information/users/name.ts": $api_v2_server_information_users_name,
"./routes/api/v2/server/information/users/profile.ts": $api_v2_server_information_users_profile,
"./routes/api/v2/server/information/users/uuid.ts": $api_v2_server_information_users_uuid,
"./routes/api/v2/server/pubkey.ts": $api_v2_server_pubkey,
"./routes/home/friends/[name].tsx": $home_friends_name_,
"./routes/home/index.tsx": $home_index,
Expand Down
2 changes: 1 addition & 1 deletion routes/api/v2/client/block/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const handler = {
}
const body = await req.json();
const domain = body.domain;
if (await takos.checkCsrfToken(body.csrftoken) === false) {
if (await takos.checkCsrfToken(body.csrftoken, ctx.state.data.sessionid) === false) {
return new Response(JSON.stringify({ status: false, message: "Invalid CSRF token" }), {
headers: { "Content-Type": "application/json" },
status: 400,
Expand Down
2 changes: 1 addition & 1 deletion routes/api/v2/client/block/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const handler = {
}
const userName = takos.splitUserName(userid).userName;
const userDomain = takos.splitUserName(userid).domain;
if (await takos.checkCsrfToken(body.csrftoken) === false) {
if (await takos.checkCsrfToken(body.csrftoken, ctx.state.data.sessionid) === false) {
return new Response(JSON.stringify({ status: false, message: "Invalid CSRF token" }), {
headers: { "Content-Type": "application/json" },
status: 400,
Expand Down
6 changes: 6 additions & 0 deletions routes/api/v2/client/friends/accept/friend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//フレンド申請を承認する
// POST /api/v2/client/friends/accept/friend
// { friendid: string}
// -> { status: boolean }
import friends from "../../../../../../models/friends.ts";
import users from "../../../../../../models/users.ts";
54 changes: 0 additions & 54 deletions routes/api/v2/client/friends/request/friend.ts

This file was deleted.

Empty file.
172 changes: 172 additions & 0 deletions routes/api/v2/client/friends/request/name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import takos from "../../../../../../util/takos.ts";
import userConfig from "../../../../../../models/userConfig.ts";
import remoteFriends from "../../../../../../models/remoteFriends.ts";
import friends from "../../../../../../models/friends.ts";
import requestAddFriend from "../../../../../../models/reqestAddFriend.ts";
import { load } from "$std/dotenv/mod.ts";
import users from "../../../../../../models/users.ts";
const env = await load();
export const handler = {
async GET(req: Request, ctx: any) {
if (!ctx.state.data.loggedIn) {
return new Response(JSON.stringify({ status: false, message: "Not Logged In" }));
}
const userid = ctx.state.data.userid;
let body;
try {
body = await req.json();
} catch (e) {
return new Response(JSON.stringify({ status: false, message: "Invalid request" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
const { userName, csrftoken } = body;
if (typeof userName !== "string") {
return new Response(JSON.stringify({ status: false, message: "Invalid userName" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
if (typeof csrftoken !== "string") {
return new Response(JSON.stringify({ status: false, message: "Invalid csrftoken" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
if (takos.checkUserName(userName) === false) {
return new Response(JSON.stringify({ status: false, message: "Invalid" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
if (await takos.checkCsrfToken(csrftoken, userid) === false) {
return new Response(JSON.stringify({ status: false, message: "Invalid CSRF token" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
const userDomain = takos.splitUserName(userName).domain;
if (userDomain !== env["DOMAIN"]) {
//friendのuuidを取得
const response = await fetch(`https://${userDomain}/api/v2/server/information/users/uuid`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ host: env["DOMAIN"], body: JSON.stringify({ userName }), signature: takos.signData(JSON.stringify({ userName }), await takos.getPrivateKey()) }),
});
const data = await response.json();
if (data.status === false) {
return new Response(JSON.stringify({ status: false, message: "User not found" }), {
headers: { "Content-Type": "application/json" },
status: 404,
});
}
const friendId = data.userName;
//すでにリクエストを送っているか確認
const request = await requestAddFriend.findOne({ userid });
if (request == null) {
return new Response(JSON.stringify({ status: false, message: "Already requested" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
const isAleredyRequested = request.requestedUser.find((user) => user.userID === friendId);
if (isAleredyRequested) {
return new Response(JSON.stringify({ status: false, message: "Already requested" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
//すでに友達か確認
const friendData = await friends.findOne({ user: userid });
if (friendData == null) {
return new Response(JSON.stringify({ status: false, message: "User not found" }), {
headers: { "Content-Type": "application/json" },
status: 404,
});
}
const isFriend = friendData.friends.find((friend) => friend.userid === friendId);
if (isFriend) {
return new Response(JSON.stringify({ status: false, message: "Already friend" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
//リクエストを送る
const res = await fetch(`https://${userDomain}/api/v2/server/friends/request/friend`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
host: env["DOMAIN"],
body: JSON.stringify({ userid, friendid: friendId }),
signature: takos.signData(JSON.stringify({ userid, friendid: friendId }), await takos.getPrivateKey()),
}),
});
const resData = await res.json();
if (resData.status === false) {
return new Response(JSON.stringify({ status: false, message: "Failed to send request" }), {
headers: { "Content-Type": "application/json" },
status: 500,
});
}
//リクエストを送信したことを記録
await requestAddFriend.updateOne({ userid }, { $push: { requestedUser: { userID: friendId } } });
return new Response(JSON.stringify({ status: true }), {
headers: { "Content-Type": "application/json" },
});
} else {
const friendInfo = await users.findOne({ userName: takos.splitUserName(userName).userName });
if (friendInfo === null) {
return new Response(JSON.stringify({ status: false, message: "User not found" }), {
headers: { "Content-Type": "application/json" },
status: 404,
});
}
const friendData = await friends.findOne({ user: userid });
if (friendData == null) {
return new Response(JSON.stringify({ status: false, message: "User not found" }), {
headers: { "Content-Type": "application/json" },
status: 404,
});
}
const isFriend = friendData.friends.find((friend) => friend.userid === friendInfo.uuid);
if (isFriend) {
return new Response(JSON.stringify({ status: false, message: "Already friend" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
if (friendData === null) {
return new Response(JSON.stringify({ status: false, message: "User not found" }), {
headers: { "Content-Type": "application/json" },
status: 404,
});
}
const isRequested = await requestAddFriend.findOne({ userid: friendInfo.uuid });
if (isRequested == null) {
return new Response(JSON.stringify({ status: false, message: "Already requested" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
const isAleredyRequested = isRequested.friendRequester.find((user) => user.userID === userid);
if (isAleredyRequested) {
return new Response(JSON.stringify({ status: false, message: "Already requested" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
//リクエストを送る
await requestAddFriend.updateOne({ userid: friendInfo.uuid }, { $push: { friendRequester: { userID: userid } } });
await requestAddFriend.updateOne({ userid }, { $push: { requestedUser: { userID: friendInfo.uuid } } });
return new Response(JSON.stringify({ status: true }), {
headers: { "Content-Type": "application/json" },
});
}
},
};
//{ host: string, body: string, signature: string }
40 changes: 40 additions & 0 deletions routes/api/v2/server/information/users/name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//uuidとuserNameを変換する
// POST /api/v2/server/information/users/uuid
// { host: string, body: string, signature: string }
// signatureは秘密鍵で署名されたJSON
// body: { uuid: string }
import users from "../../../../../../models/users.ts";
import takos from "../../../../../../util/takos.ts";
export const handler = {
async POST(req: any, ctx: any) {
const body = await req.json();
const host = body.host;
const publickey = await fetch(`https://${host}/api/v2/server/pubkey`).then((res) => res.json()).then((data) => data.publickey);
const verify = await takos.verifySignature(publickey, body.signature, body.body);
if (!verify) {
return new Response(JSON.stringify({
status: false,
message: "Invalid Signature",
}));
}
const data = JSON.parse(body.body);
const uuid = data.uuid;
const userInfo = await users.findOne({ uuid });
if (userInfo === null) {
return new Response(JSON.stringify({
status: false,
message: "User not found",
}));
}
const result = JSON.stringify({
status: true,
userName: userInfo.userName,
});
const signature = await takos.signData(result, await takos.getPrivateKey());
return new Response(JSON.stringify({
status: true,
result,
signature: new Uint8Array(signature),
}));
},
};
36 changes: 36 additions & 0 deletions routes/api/v2/server/information/users/uuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//uuidとuserNameを変換する
import users from "../../../../../../models/users.ts";
import takos from "../../../../../../util/takos.ts";
export const handler = {
async POST(req: any, ctx: any) {
const body = await req.json();
const host = body.host;
const publickey = await fetch(`https://${host}/api/v2/server/pubkey`).then((res) => res.json()).then((data) => data.publickey);
const verify = await takos.verifySignature(publickey, body.signature, body.body);
if (!verify) {
return new Response(JSON.stringify({
status: false,
message: "Invalid Signature",
}));
}
const data = JSON.parse(body.body);
const userName = data.userName;
const userInfo = await users.findOne({ userName });
if (userInfo === null) {
return new Response(JSON.stringify({
status: false,
message: "User not found",
}));
}
const result = JSON.stringify({
status: true,
userName: userInfo.uuid,
});
const signature = await takos.signData(result, await takos.getPrivateKey());
return new Response(JSON.stringify({
status: true,
result,
signature: new Uint8Array(signature),
}));
},
};

0 comments on commit 9c3bd7c

Please sign in to comment.