Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tako0614 committed May 21, 2024
1 parent b7a23a6 commit b166f0a
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 160 deletions.
10 changes: 9 additions & 1 deletion components/Chats/Chat.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
Expand Down
4 changes: 2 additions & 2 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions islands/Chats/ChatList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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(
Expand Down
94 changes: 49 additions & 45 deletions islands/Chats/FriendRequest.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
Expand Down Expand Up @@ -80,37 +80,37 @@ export default function RegisterForm(props: { origin: string }) {
</div>
)}
</>
);
)
}

// 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 (
<>
<div class="w-full text-gray-900 text-sm rounded-lg h-16">
<VideoList />
</div>
</>
);
)
}

// Define the User component
function User({ icon, userName }: { icon: string, userName: string }) {
function User({ icon, userName }: { icon: string; userName: string }) {
return (
<>
<li class="c-talk-rooms flex mb-2 bg-white border border-gray-300">
Expand All @@ -129,30 +129,34 @@ function User({ icon, userName }: { icon: string, userName: string }) {
</a>
<div class="mt-auto mb-auto ml-auto flex">
<div class="ml-2">
<button class="w-1 h-1 bg-blue-400 text-lg text-white font-semibold rounded-full"></button>
<button class="w-1 h-1 bg-blue-400 text-lg text-white font-semibold rounded-full">
</button>
</div>
<div>
<button class="w-1 h-1 bg-blue-400 text-lg text-white font-semibold rounded-full"></button>
<button class="w-1 h-1 bg-blue-400 text-lg text-white font-semibold rounded-full">
</button>
</div>
</div>
</li>
<hr />
</>
);
)
}

// 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 (
<div className="container mx-auto mt-8">
Expand All @@ -168,15 +172,15 @@ const VideoList = () => {
</ul>
</div>
</div>
);
};
)
}

// 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("コピーに失敗しました!")
}
}
2 changes: 1 addition & 1 deletion models/friends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const friendsSchema = new mongoose.Schema({
},
},
],
default: []
default: [],
},
timestamp: { type: Date, default: Date.now },
})
Expand Down
112 changes: 64 additions & 48 deletions routes/api/v1/Users/icon.ts
Original file line number Diff line number Diff line change
@@ -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<Uint8Array | null> {
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<Uint8Array | null> {
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
}
}
/*
// 使用例
Expand All @@ -75,4 +91,4 @@ async function processImage(inputBuffer: Uint8Array): Promise<Uint8Array | null>
} else {
console.log("Failed to process the image.");
}
*/
*/
Loading

0 comments on commit b166f0a

Please sign in to comment.