Skip to content

Commit

Permalink
Refactor code to add lazy loading for images in TalkListContent
Browse files Browse the repository at this point in the history
  • Loading branch information
tako0614 committed Jul 22, 2024
1 parent 9c3bd7c commit 50dde21
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 25 deletions.
2 changes: 2 additions & 0 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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_friendrequest from "./routes/api/v2/client/friends/info/[ID]/icon/friendrequest.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";
Expand Down Expand Up @@ -134,6 +135,7 @@ const manifest = {
"./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/friendrequest.ts": $api_v2_client_friends_info_ID_icon_friendrequest,
"./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,
Expand Down
6 changes: 3 additions & 3 deletions islands/FriendRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ const VideoList = () => {
const res = await fetch("/api/v2/client/friends/request/list");
const response = await res.json();
//response.resultが空の配列の場合、setItems([])を実行
if (response.status === true) {
if (response.status === false) {
return;
}
if (response.result.length === 0) {
Expand All @@ -212,13 +212,13 @@ const VideoList = () => {
? (
<>
{items.map((video, index) => {
if (!video || !video.icon) {
if (!video) {
return null;
}
return (
<User
key={index}
icon={video.icon}
icon={"/api/v2/client"}
userName={video.userName}
items={items}
setItems={setItems}
Expand Down
12 changes: 4 additions & 8 deletions islands/RequestFriendById.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,22 @@ function Input({
onClick={async () => {
setIsError(false);
setIsSending(true);

const origin = window.location.protocol + "//" +
window.location.host;
const csrftokenRes = await fetch(
`/api/v1/csrftoken?origin=${origin}`,
`/api/v2/client/csrftoken`,
);
const csrftoken = await csrftokenRes.json();
const result = await fetch("/api/v1/friends/request", {
const result = await fetch("/api/v2/client/friends/request/name", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
csrftoken: csrftoken.csrftoken,
type: "userName",
friendName: value,
userName: value,
}),
});
const res = await result.json();
if (res.status == "success") {
if (res.status == true) {
setIsSuccess(true);
setTimeout(() => setIsSuccess(false), 5000);
setIsSending(false);
Expand Down
38 changes: 37 additions & 1 deletion routes/api/v2/client/friends/accept/friend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,40 @@
// { friendid: string}
// -> { status: boolean }
import friends from "../../../../../../models/friends.ts";
import users from "../../../../../../models/users.ts";
import requestAddFriend from "../../../../../../models/reqestAddFriend.ts";
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 friendid = body.friendid;
const user = await users.findOne({ uuid: friendid });
if (user === null) {
return new Response(JSON.stringify({
status: false,
message: "User not found",
}));
}
const request = await requestAddFriend.findOne({ user: user.uuid });
if (request === null) {
return new Response(JSON.stringify({
status: false,
message: "Request not found",
}));
}
const isRequest = request.friendRequester.find((requester) => requester.userID === ctx.state.data.userid);
if (isRequest === undefined) {
return new Response(JSON.stringify({
status: false,
message: "Request not found",
}));
}
await requestAddFriend.updateOne({ user: user.uuid }, { $pull: { friendRequester: { userID: ctx.state.data.userid } } });
await requestAddFriend.updateOne({ user: ctx.state.data.userid }, { $pull: { requestedUser: { userID: user.uuid } } });
await friends.updateOne({ user: ctx.state.data.userid }, { $push: { friends: { userid: user.uuid } } });
await friends.updateOne({ user: user.uuid }, { $push: { friends: { userid: ctx.state.data.userid } } });
return new Response(JSON.stringify({
status: true,
}));
},
};
Empty file.
13 changes: 9 additions & 4 deletions routes/api/v2/client/friends/request/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requestAddFriend from "../../../../../../models/reqestAddFriend.ts";
import users from "../../../../../../models/users.ts";
export const handler = {
async GET(req: Request, ctx: any) {
if (!ctx.state.data.loggedIn) {
Expand All @@ -13,13 +14,17 @@ export const handler = {
status: 200,
});
}
const friendData = Promise.all(result.friendRequester.map(async (data: any) => {
const friendData = await Promise.all(result.friendRequester.map(async (data: any) => {
const friendData = await users.findOne({ uuid: data.userID });
if(friendData === null) {
return
}
return {
userName: data.userName + "@" + data.domain,
nickName: data.nickName,
userName: friendData.userName + "@" + data.domain,
nickName: friendData.nickName,
};
}));
return new Response(JSON.stringify({ status: true, friendData }), {
return new Response(JSON.stringify({ status: true, result: friendData }), {
headers: { "Content-Type": "application/json" },
status: 200,
});
Expand Down
19 changes: 10 additions & 9 deletions routes/api/v2/client/friends/request/name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ 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";
import { getCookies } from "$std/http/cookie.ts";
const env = await load();
export const handler = {
async GET(req: Request, ctx: any) {
async POST(req: Request, ctx: any) {
if (!ctx.state.data.loggedIn) {
return new Response(JSON.stringify({ status: false, message: "Not Logged In" }));
}
Expand All @@ -22,6 +23,8 @@ export const handler = {
});
}
const { userName, csrftoken } = body;
const cookies = getCookies(req.headers);
const sessionid = cookies.sessionid;
if (typeof userName !== "string") {
return new Response(JSON.stringify({ status: false, message: "Invalid userName" }), {
headers: { "Content-Type": "application/json" },
Expand All @@ -34,20 +37,15 @@ export const handler = {
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) {
if (await takos.checkCsrfToken(csrftoken, sessionid) === 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"]) {
console.log("imakoko")
//friendのuuidを取得
const response = await fetch(`https://${userDomain}/api/v2/server/information/users/uuid`, {
method: "POST",
Expand Down Expand Up @@ -126,20 +124,23 @@ export const handler = {
status: 404,
});
}
console.log("imakoko")
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,
});
}
console.log("imakoko")
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,
});
}
console.log("imakoko")
if (friendData === null) {
return new Response(JSON.stringify({ status: false, message: "User not found" }), {
headers: { "Content-Type": "application/json" },
Expand All @@ -162,7 +163,7 @@ export const handler = {
}
//リクエストを送る
await requestAddFriend.updateOne({ userid: friendInfo.uuid }, { $push: { friendRequester: { userID: userid } } });
await requestAddFriend.updateOne({ userid }, { $push: { requestedUser: { userID: friendInfo.uuid } } });
await requestAddFriend.updateOne({ userid: userid }, { $push: { requestedUser: { userID: friendInfo.uuid } } });
return new Response(JSON.stringify({ status: true }), {
headers: { "Content-Type": "application/json" },
});
Expand Down
11 changes: 11 additions & 0 deletions routes/api/v2/client/sessions/registers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { load } from "$std/dotenv/mod.ts";
import tempUsers from "../../../../../../models/tempUsers.ts";
import users from "../../../../../../models/users.ts";
import takos from "../../../../../../util/takos.ts";
import friends from "../../../../../../models/friends.ts";
import requestAddFriend from "../../../../../../models/reqestAddFriend.ts";
const env = await load();
export const handler = {
async POST(req: Request, ctx: any) {
Expand Down Expand Up @@ -144,6 +146,15 @@ export const handler = {
password: hashHex,
salt: salt,
});
await friends.create({
user: uuid,
friends: [],
});
await requestAddFriend.create({
userid: uuid,
friendRequester: [],
requestedUser: [],
});
return new Response(JSON.stringify({ status: true }), {
headers: { "Content-Type": "application/json" },
});
Expand Down

0 comments on commit 50dde21

Please sign in to comment.