From b166f0a10d1d1eeaddc24bbbcff43b5482163bc1 Mon Sep 17 00:00:00 2001 From: tako0614 Date: Wed, 22 May 2024 01:24:39 +0900 Subject: [PATCH] fix --- components/Chats/Chat.tsx | 10 ++- fresh.gen.ts | 4 +- islands/Chats/ChatList.jsx | 6 +- islands/Chats/FriendRequest.tsx | 94 +++++++++++++------------ models/friends.ts | 2 +- routes/api/v1/Users/icon.ts | 112 +++++++++++++++++------------- routes/api/v1/friends/reqLists.ts | 31 +++++---- routes/api/v1/friends/request.ts | 91 +++++++++++++----------- takos.ts | 9 ++- 9 files changed, 199 insertions(+), 160 deletions(-) diff --git a/components/Chats/Chat.tsx b/components/Chats/Chat.tsx index 52652130..a0419713 100644 --- a/components/Chats/Chat.tsx +++ b/components/Chats/Chat.tsx @@ -1,7 +1,15 @@ import Footer from "../Footer.tsx" import ChatMain from "../../islands/Chats/ChatMain.tsx" import ChatHeader from "../../islands/Chats/ChatHeader.tsx" -export default function Talks(props: { isAddFriendForm?: any; isChoiceUser?: any; addFriendKey?: any; roomid?: any; isSetting?: any }) { +export default function Talks( + props: { + isAddFriendForm?: any + isChoiceUser?: any + addFriendKey?: any + roomid?: any + isSetting?: any + }, +) { if (props.isAddFriendForm) { return ( <> diff --git a/fresh.gen.ts b/fresh.gen.ts index 907be2a5..2f6ad648 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -16,7 +16,7 @@ import * as $api_v1_chats_friendkey from "./routes/api/v1/chats/friendkey.js" import * as $api_v1_chats_message from "./routes/api/v1/chats/message.js" import * as $api_v1_chats_talk from "./routes/api/v1/chats/talk.js" import * as $api_v1_chats_talkdata from "./routes/api/v1/chats/talkdata.ts" -import * as $api_v1_csrfToken from "./routes/api/v1/csrfToken.ts" +import * as $api_v1_csrfToken from "./routes/api/v1/csrftoken.ts" import * as $api_v1_friends_ID_icon from "./routes/api/v1/friends/[ID]/icon.js" import * as $api_v1_friends_ID_info from "./routes/api/v1/friends/[ID]/info.ts" import * as $api_v1_friends_reqLists from "./routes/api/v1/friends/reqLists.ts" @@ -69,7 +69,7 @@ const manifest = { "./routes/api/v1/chats/message.js": $api_v1_chats_message, "./routes/api/v1/chats/talk.js": $api_v1_chats_talk, "./routes/api/v1/chats/talkdata.ts": $api_v1_chats_talkdata, - "./routes/api/v1/csrfToken.ts": $api_v1_csrfToken, + "./routes/api/v1/csrftoken.ts": $api_v1_csrfToken, "./routes/api/v1/friends/[ID]/icon.js": $api_v1_friends_ID_icon, "./routes/api/v1/friends/[ID]/info.ts": $api_v1_friends_ID_info, "./routes/api/v1/friends/reqLists.ts": $api_v1_friends_reqLists, diff --git a/islands/Chats/ChatList.jsx b/islands/Chats/ChatList.jsx index 6fc3f3cd..db363375 100644 --- a/islands/Chats/ChatList.jsx +++ b/islands/Chats/ChatList.jsx @@ -24,7 +24,7 @@ function ChatList(props) { ) useEffect(async () => { const csrftokenres = await fetch( - "./api/v1/csrfToken?origin=http://localhost:8000", + "./api/v1/csrftoken?origin=http://localhost:8000", { method: "GET", }, @@ -53,7 +53,7 @@ function ChatList(props) { return } else if (res.status == "csrftoken error") { const csrftokenres = await fetch( - "./api/v1/csrfToken?origin=http://localhost:8000", + "./api/v1/csrftoken?origin=http://localhost:8000", { method: "GET", }, @@ -199,7 +199,7 @@ const AddFriendForm = (props) => { const origin = window.location.protocol + "//" + window.location.host const csrftokenRes = await fetch( - `./api/v1/csrfToken?origin=${origin}`, + `./api/v1/csrftoken?origin=${origin}`, ) const csrftoken = await csrftokenRes.json() const result = await fetch( diff --git a/islands/Chats/FriendRequest.tsx b/islands/Chats/FriendRequest.tsx index 3fd56f72..affe4f46 100644 --- a/islands/Chats/FriendRequest.tsx +++ b/islands/Chats/FriendRequest.tsx @@ -1,38 +1,38 @@ -import { useEffect, useState } from "preact/hooks"; +import { useEffect, useState } from "preact/hooks" // Define the InputProps interface for type-checking interface InputProps { - value: string; - setValue: (value: string) => void; - origin: string; + value: string + setValue: (value: string) => void + origin: string } // Define the RegisterForm component export default function RegisterForm(props: { origin: string }) { - const [showModal, setShowModal] = useState(false); - const [value, setValue] = useState(""); + const [showModal, setShowModal] = useState(false) + const [value, setValue] = useState("") // Toggle the modal visibility const handleButtonClick = () => { - setShowModal(!showModal); - }; + setShowModal(!showModal) + } // Fetch data whenever the modal is shown useEffect(() => { if (showModal) { const fetchData = async () => { - const resp = await fetch("./api/v1/chats/friendkey?reload=false"); - const data = await resp.json(); + const resp = await fetch("./api/v1/chats/friendkey?reload=false") + const data = await resp.json() if (data.status === false) { - console.log("error"); - return; + console.log("error") + return } - const url = props.origin + data.addFriendKey; - setValue(url); - }; - fetchData(); + const url = props.origin + data.addFriendKey + setValue(url) + } + fetchData() } - }, [showModal, props.origin]); + }, [showModal, props.origin]) return ( <> @@ -80,25 +80,25 @@ export default function RegisterForm(props: { origin: string }) { )} - ); + ) } // Define the Input component function Input({ value, setValue, origin }: InputProps) { const handleChangeUrl = (event: any) => { - event.preventDefault(); + event.preventDefault() const updateUrl = async () => { - const resp = await fetch("./api/v1/chats/friendkey?reload=true"); - const data = await resp.json(); + const resp = await fetch("./api/v1/chats/friendkey?reload=true") + const data = await resp.json() if (data.status === false) { - console.log("error"); - return; + console.log("error") + return } - const url = origin + data.addFriendKey; - setValue(url); - }; - updateUrl(); - }; + const url = origin + data.addFriendKey + setValue(url) + } + updateUrl() + } return ( <> @@ -106,11 +106,11 @@ function Input({ value, setValue, origin }: InputProps) { - ); + ) } // Define the User component -function User({ icon, userName }: { icon: string, userName: string }) { +function User({ icon, userName }: { icon: string; userName: string }) { return ( <>
  • @@ -129,30 +129,34 @@ function User({ icon, userName }: { icon: string, userName: string }) {
    - +
    - +

  • - ); + ) } // Define the VideoList component const VideoList = () => { - const [items, setItems] = useState<{ icon: string, userName: string }[]>([]); + const [items, setItems] = useState<{ icon: string; userName: string }[]>([]) useEffect(() => { const fetchData = async () => { - const res = await fetch("./api/v1/friends/reqLists"); - const response = await res.json(); - setItems(response.result); - }; - fetchData(); - }, []); + const res = await fetch("./api/v1/friends/reqLists") + const response = await res.json() + setItems(response.result) + } + fetchData() + }, []) return (
    @@ -168,15 +172,15 @@ const VideoList = () => {
    - ); -}; + ) +} // Function to copy text to clipboard async function copyToClipboard(value: string) { try { - await navigator.clipboard.writeText(value); - alert("URLをコピーしました!"); + await navigator.clipboard.writeText(value) + alert("URLをコピーしました!") } catch (err) { - alert("コピーに失敗しました!"); + alert("コピーに失敗しました!") } } diff --git a/models/friends.ts b/models/friends.ts index 80851f53..b301faa7 100644 --- a/models/friends.ts +++ b/models/friends.ts @@ -22,7 +22,7 @@ export const friendsSchema = new mongoose.Schema({ }, }, ], - default: [] + default: [], }, timestamp: { type: Date, default: Date.now }, }) diff --git a/routes/api/v1/Users/icon.ts b/routes/api/v1/Users/icon.ts index da5ecd9c..a74c962d 100644 --- a/routes/api/v1/Users/icon.ts +++ b/routes/api/v1/Users/icon.ts @@ -1,61 +1,77 @@ import { getCookies } from "$std/http/cookie.ts" import csrftoken from "../../../../models/csrftoken.ts" import Users from "../../../../models/users.ts" -import { ensureDir } from "$std/fs/ensure_dir.ts"; -import sharp from "sharp"; +import { ensureDir } from "$std/fs/ensure_dir.ts" +import sharp from "sharp" export const handler = { async POST(req: Request, ctx: any) { try { - const data = await req.json() - const cookies = getCookies(req.headers) - if (typeof data.csrftoken !== "string") { - return new Response(JSON.stringify({ status: "error" }), { - headers: { "Content-Type": "application/json" }, - status: 403, - }) - } - const iscsrfToken = await csrftoken.findOne({ token: data.csrftoken }) - if (iscsrfToken === null || iscsrfToken === undefined) { - return new Response(JSON.stringify({ status: "error" }), { - headers: { "Content-Type": "application/json" }, - status: 403, - }) - } - if (iscsrfToken.sessionID !== cookies.sessionid) { - return new Response(JSON.stringify({ status: "error" }), { - headers: { "Content-Type": "application/json" }, - status: 403, - }) - } - await csrftoken.deleteOne({ token: data.csrftoken }) - const userid = ctx.state.data.userid.toString() - const icon = data.icon - const result = await processImage(icon) - if(result === null) { - return - } - console.log(result) + const data = await req.json() + const cookies = getCookies(req.headers) + if (typeof data.csrftoken !== "string") { + return new Response(JSON.stringify({ status: "error" }), { + headers: { "Content-Type": "application/json" }, + status: 403, + }) + } + const iscsrfToken = await csrftoken.findOne({ token: data.csrftoken }) + if (iscsrfToken === null || iscsrfToken === undefined) { + return new Response(JSON.stringify({ status: "error" }), { + headers: { "Content-Type": "application/json" }, + status: 403, + }) + } + if (iscsrfToken.sessionID !== cookies.sessionid) { + return new Response(JSON.stringify({ status: "error" }), { + headers: { "Content-Type": "application/json" }, + status: 403, + }) + } + await csrftoken.deleteOne({ token: data.csrftoken }) + const userid = ctx.state.data.userid.toString() + const icon = data.icon + const result = await processImage(icon) + if (result === null) { + return + } + await Deno.writeFile( + `./files/userIcons/${ctx.state.data.userid.toString}.webp`, + result, + ) + return new Response(JSON.stringify({ status: true }), { + headers: { "Content-Type": "application/json" }, + status: 200, + }) } catch (error) { - console.log(error) + console.log(error) } }, } -async function processImage(inputBuffer: Uint8Array): Promise { - try { - const image = sharp(inputBuffer); - const metadata = await image.metadata(); - if (metadata.format && ["jpeg", "png", "gif", "webp", "tiff", "avif", "heif"].includes(metadata.format)) { - // 画像ファイルであると判断 - const outputBuffer = await image.resize(512, 512).toFormat("webp").toBuffer(); - return outputBuffer; - } else { - console.log(`The provided data is not a recognized image format.`); - return null; - } - } catch (error) { - console.error(`Failed to process the image: ${error.message}`); - return null; + +async function processImage( + inputBuffer: Uint8Array, +): Promise { + try { + const image = sharp(inputBuffer) + const metadata = await image.metadata() + if ( + metadata.format && + ["jpeg", "png", "gif", "webp", "tiff", "avif", "heif", "jpg"].includes( + metadata.format, + ) + ) { + // 画像ファイルであると判断 + const outputBuffer = await image.resize(512, 512).toFormat("webp") + .toBuffer() + return outputBuffer + } else { + console.log(`The provided data is not a recognized image format.`) + return null } + } catch (error) { + console.error(`Failed to process the image: ${error.message}`) + return null + } } /* // 使用例 @@ -75,4 +91,4 @@ async function processImage(inputBuffer: Uint8Array): Promise } else { console.log("Failed to process the image."); } - */ \ No newline at end of file + */ diff --git a/routes/api/v1/friends/reqLists.ts b/routes/api/v1/friends/reqLists.ts index 4210d5b8..5d74724e 100644 --- a/routes/api/v1/friends/reqLists.ts +++ b/routes/api/v1/friends/reqLists.ts @@ -20,23 +20,26 @@ export const handler = { return } const result = await Promise.all( - userFriendInfo.Applicant.map(async (obj: { userID: any; timestamp: any }) => { - const userInfo = await users.findOne({ _id: obj.userID }); - if (userInfo == null) { - return; - } - console.log(userInfo); - return { - userName: userInfo.userName, - icon: `./api/v1/friends/${userInfo.userName}/icon?isRequestList=true`, - timestamp: obj.timestamp, - }; - }) - ); + userFriendInfo.Applicant.map( + async (obj: { userID: any; timestamp: any }) => { + const userInfo = await users.findOne({ _id: obj.userID }) + if (userInfo == null) { + return + } + console.log(userInfo) + return { + userName: userInfo.userName, + icon: + `./api/v1/friends/${userInfo.userName}/icon?isRequestList=true`, + timestamp: obj.timestamp, + } + }, + ), + ) return new Response(JSON.stringify({ status: true, result: result }), { headers: { "Content-Type": "application/json" }, status: 200, - }); + }) } catch (error) { console.error("Error in getFriendInfoByID: ", error) return new Response(JSON.stringify({ status: "error" }), { diff --git a/routes/api/v1/friends/request.ts b/routes/api/v1/friends/request.ts index 0b3f830a..7b400450 100644 --- a/routes/api/v1/friends/request.ts +++ b/routes/api/v1/friends/request.ts @@ -1,8 +1,8 @@ -import { getCookies } from "$std/http/cookie.ts"; -import csrftoken from "../../../../models/csrftoken.ts"; -import Friends from "../../../../models/friends.ts"; -import requestAddFriend from "../../../../models/reqestAddFriend.ts"; -import Users from "../../../../models/users.ts"; +import { getCookies } from "$std/http/cookie.ts" +import csrftoken from "../../../../models/csrftoken.ts" +import Friends from "../../../../models/friends.ts" +import requestAddFriend from "../../../../models/reqestAddFriend.ts" +import Users from "../../../../models/users.ts" export const handler = { async POST(req: Request, ctx: any) { @@ -10,125 +10,134 @@ export const handler = { return new Response(JSON.stringify({ status: "Please Login" }), { headers: { "Content-Type": "application/json" }, status: 401, - }); + }) } - const data = await req.json(); - const cookies = getCookies(req.headers); + const data = await req.json() + const cookies = getCookies(req.headers) if (typeof data.csrftoken !== "string") { return new Response(JSON.stringify({ status: "error" }), { headers: { "Content-Type": "application/json" }, status: 403, - }); + }) } - const csrfTokenRecord = await csrftoken.findOne({ token: data.csrftoken }); + const csrfTokenRecord = await csrftoken.findOne({ token: data.csrftoken }) if (!csrfTokenRecord || csrfTokenRecord.sessionID !== cookies.sessionid) { return new Response(JSON.stringify({ status: "error" }), { headers: { "Content-Type": "application/json" }, status: 403, - }); + }) } - await csrftoken.deleteOne({ token: data.csrftoken }); - const userid = ctx.state.data.userid.toString(); + await csrftoken.deleteOne({ token: data.csrftoken }) + const userid = ctx.state.data.userid.toString() if (data.type === "acceptRequest") { // Handle friend request acceptance } if (data.type === "AddFriendKey") { - const { addFriendKey } = data; - const addFriendUserInfo = await Users.findOne({ addFriendKey: addFriendKey }); + const { addFriendKey } = data + const addFriendUserInfo = await Users.findOne({ + addFriendKey: addFriendKey, + }) if (!addFriendKey || !addFriendUserInfo) { return new Response(JSON.stringify({ status: "error" }), { headers: { "Content-Type": "application/json" }, status: 404, - }); + }) } - const friendsInfo = await Friends.findOne({ user: userid }); + const friendsInfo = await Friends.findOne({ user: userid }) if (!friendsInfo) { - await Friends.create({ user: userid }); + await Friends.create({ user: userid }) } - const existingRequest = await requestAddFriend.findOne({ userID: addFriendUserInfo._id.toString() }); + const existingRequest = await requestAddFriend.findOne({ + userID: addFriendUserInfo._id.toString(), + }) if (!existingRequest) { - await requestAddFriend.create({ userID: addFriendUserInfo._id.toString(), Applicant: [{ userID: userid }] }); + await requestAddFriend.create({ + userID: addFriendUserInfo._id.toString(), + Applicant: [{ userID: userid }], + }) } else { const isAlreadySentReq = existingRequest.Applicant.some( - (applicant: any) => applicant.userID === userid - ); + (applicant: any) => applicant.userID === userid, + ) if (!isAlreadySentReq) { await requestAddFriend.updateOne( { userID: addFriendUserInfo._id }, - { $push: { Applicant: { userID: userid } } } - ); + { $push: { Applicant: { userID: userid } } }, + ) } } return new Response(JSON.stringify({ status: "success" }), { headers: { "Content-Type": "application/json" }, status: 200, - }); + }) } else if (data.type === "userName") { - const userName = ctx.state.data.userName; - const friendName = data.friendName; + const userName = ctx.state.data.userName + const friendName = data.friendName - const isAlreadyFriend = await Friends.findOne({ userName: userName }); + const isAlreadyFriend = await Friends.findOne({ userName: userName }) if (!isAlreadyFriend || isAlreadyFriend.friends.includes(friendName)) { return new Response(JSON.stringify({ status: "error" }), { headers: { "Content-Type": "application/json" }, status: 403, - }); + }) } - const friendRequest = await requestAddFriend.findOne({ userName: friendName }); + const friendRequest = await requestAddFriend.findOne({ + userName: friendName, + }) if (!friendRequest) { return new Response(JSON.stringify({ status: "error" }), { headers: { "Content-Type": "application/json" }, status: 403, - }); + }) } const isAlreadyRequested = friendRequest.Applicant.some( - (applicant: any) => applicant.userName === userName - ); + (applicant: any) => applicant.userName === userName, + ) if (isAlreadyRequested) { return new Response(JSON.stringify({ status: "error" }), { headers: { "Content-Type": "application/json" }, status: 403, - }); + }) } try { await requestAddFriend.updateOne( { userName: friendName }, - { $push: { Applicant: { userName: userName } } } - ); + { $push: { Applicant: { userName: userName } } }, + ) return new Response(JSON.stringify({ status: "success" }), { headers: { "Content-Type": "application/json" }, status: 200, - }); + }) } catch (error) { - console.error(error); + console.error(error) return new Response(JSON.stringify({ status: "error" }), { headers: { "Content-Type": "application/json" }, status: 500, - }); + }) } } return new Response(JSON.stringify({ status: "error" }), { headers: { "Content-Type": "application/json" }, status: 400, - }); + }) }, -}; \ No newline at end of file +} diff --git a/takos.ts b/takos.ts index 2769ab8d..25b73620 100644 --- a/takos.ts +++ b/takos.ts @@ -1,11 +1,10 @@ import { parse } from "$std/dotenv/parse.ts" import { isWindows } from "https://deno.land/std@0.216.0/path/_os.ts" -import $ from "https://deno.land/x/dax@0.38.0/mod.ts"; +import $ from "https://deno.land/x/dax@0.38.0/mod.ts" const parsedArgs = Deno.args const os = Deno.build.os -if(parsedArgs[0] == "install") { - if(os == "windows") { - +if (parsedArgs[0] == "install") { + if (os == "windows") { } -} \ No newline at end of file +}