Skip to content

Commit

Permalink
Refactor code to use 'timestamp' instead of 'time' in ChatTalkContent
Browse files Browse the repository at this point in the history
  • Loading branch information
tako0614 committed Jul 17, 2024
1 parent 6c24cb4 commit 026329b
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 78 deletions.
11 changes: 3 additions & 8 deletions components/OtherMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
const ChatOtherMessage = (
{ time, message, sender, senderNickName, isPrimary }: any,
) => {
const ChatOtherMessage = ({ name, time, message, isPrimary, nickName }: any) => {
const isPrimaryClass = isPrimary ? "c-talk-chat other primary" : "c-talk-chat other subsequent";

return (
<li class={isPrimaryClass}>
<div class="c-talk-chat-box">
{isPrimary && (
<div class="c-talk-chat-icon">
<img
src={`/api/v1/friends/${sender}/icon/`}
src={`/api/v2/client/friends/info/${name}/icon/friend`}
alt=""
class="rounded-full text-white dark:text-black"
/>
</div>
)}

<div class="c-talk-chat-right">
{isPrimary && (
<div class="c-talk-chat-name">
<p>{senderNickName}</p>
<p>{nickName}</p>
</div>
)}

<div class="c-talk-chat-msg">
<p>
{convertLineBreak(message)}
Expand Down
2 changes: 1 addition & 1 deletion components/WelcomeMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const ChatOtherMessage = (
<img
src={iconPath}
alt=""
class="rounded-full text-white dark:text-black"
class="rounded-full text-white"
/>
</div>
)}
Expand Down
48 changes: 43 additions & 5 deletions components/welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function welcome() {
<div class="w-full">
<div class="flex items-center mb-4">
<img
src="./logo-mine.jpg"
src="/logo-mine.jpg"
alt="logo"
class="w-20 h-20 rounded-lg shadow-md"
/>
Expand Down Expand Up @@ -142,6 +142,44 @@ function welcome() {
<img src="/api/v2/client/chart" class="w-full" alt="list" />
</div>
</div>
<div class="mt-4">
<div class="bg-white text-black rounded-lg shadow-[0_12px_32px_#00000040] p-6 max-w-[472px] lg:ml-[100px] lg:mt-[20px] mx-auto">
<div class="text-center">
<p class="text-sm text-gray-700">© 2024 Tomiyama Shota.</p>
<p class="text-base text-gray-600">
Operat By takoserver
</p>

<p class="text-sm text-gray-700">
This site is protected by reCAPTCHA and the Google
<a href="https://policies.google.com/privacy">Privacy Policy</a> and
<a href="https://policies.google.com/terms">Terms of Service</a> apply.
</p>
<div class="flex justify-between w-5/6 mx-auto">
<p>
<a href="https://www.takos.jp/privacypolicy">
プライバシーポリシー
</a>
</p>
<p class="ml-4">
<a href="https://www.takos.jp/terms">
利用規約
</a>
</p>
<p class="ml-4">
<a href="https://x.com/takoserver_com">
SNS
</a>
</p>
<p class="ml-4">
<a href="https://line.me/ti/g2/Q0c8YJlkh5f_hkDuODxp39XF9A7BOCFqezaAHA?utm_source=invitation&utm_medium=link_copy&utm_campaign=default">
コミュニティー
</a>
</p>
</div>
</div>
</div>
</div>
<div class="h-20"></div>
</div>
<div class="lg:w-[590px] hidden lg:block top-0 bottom-0 right-0 fixed">
Expand All @@ -151,16 +189,16 @@ function welcome() {
let iconPath = "";
switch (data.userName) {
case "tako":
iconPath = "./static/tako.jpeg";
iconPath = "/static/tako.jpeg";
break;
case "371tti":
iconPath = "./static/371tti.jpg";
iconPath = "/static/371tti.jpg";
break;
case "なん":
iconPath = "./static/なん.jpeg";
iconPath = "/static/なん.jpeg";
break;
default:
iconPath = "./people.jpeg";
iconPath = "/people.jpeg";
break;
}
if (data.img) {
Expand Down
81 changes: 32 additions & 49 deletions islands/ChatTalkContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import ChatDate from "../components/ChatDate.tsx";
import ChatSendMessage from "../components/SendMessage.tsx";
import ChatOtherMessage from "../components/OtherMessage.tsx";
import { AppStateType } from "../util/types.ts";
import { splitUserName } from "../util/takosClient.ts";
interface Messages {
messageid: string;
userName: string;
messages: string;
timestamp: string;
type: string;
}

function ChatTalkMain({ state }: { state: AppStateType }) {
let SendPrimary = true;
let OtherPrimary = true;
Expand All @@ -20,18 +22,16 @@ function ChatTalkMain({ state }: { state: AppStateType }) {
const isEncodeDate = new Date(DateState).toLocaleDateString() !==
date.toLocaleDateString();
DateState = data.timestamp;
if (data.type == "text") {
if (data.userName == state.userName) {

if (data.type === "text") {
console.log(splitUserName(data.userName).domain, window.location.host);
if (state.userName === splitUserName(data.userName).userName && splitUserName(data.userName).domain === window.location.host) {
if (SendPrimary) {
SendPrimary = false;
OtherPrimary = true;
return (
<>
{isEncodeDate && (
<ChatDate
date={new Date(data.timestamp)}
/>
)}
{isEncodeDate && <ChatDate date={new Date(data.timestamp)} />}
<ChatSendMessage
isRead={data.isRead}
time={data.timestamp}
Expand All @@ -42,16 +42,13 @@ function ChatTalkMain({ state }: { state: AppStateType }) {
</>
);
}
// 前のメッセージから1分以上経過のものはprimaryに
const prevDate = new Date(state.talkData.value[index - 1].time);
if (date.getTime() - prevDate.getTime() > 60000) {

// 前のメッセージから1時間以上経過のものはprimaryに
const prevDate = index > 0 ? new Date(state.talkData.value[index - 1].timestamp) : null;
if (prevDate && (date.getTime() - prevDate.getTime() > 3600000)) {
return (
<>
{isEncodeDate && (
<ChatDate
date={new Date(data.timestamp)}
/>
)}
{isEncodeDate && <ChatDate date={new Date(data.timestamp)} />}
<ChatSendMessage
isRead={data.isRead}
time={data.timestamp}
Expand All @@ -65,11 +62,7 @@ function ChatTalkMain({ state }: { state: AppStateType }) {

return (
<>
{isEncodeDate && (
<ChatDate
date={new Date(data.timestamp)}
/>
)}
{isEncodeDate && <ChatDate date={new Date(data.timestamp)} />}
<ChatSendMessage
isRead={data.isRead}
time={data.timestamp}
Expand All @@ -83,16 +76,11 @@ function ChatTalkMain({ state }: { state: AppStateType }) {
if (OtherPrimary) {
OtherPrimary = false;
SendPrimary = true;
console.log(data);
return (
<>
{isEncodeDate && (
<ChatDate
date={new Date(data.time)}
/>
)}
{isEncodeDate && <ChatDate date={new Date(data.timestamp)} />}
<ChatOtherMessage
time={data.time}
time={data.timestamp}
message={data.message}
name={data.userName}
nickName={data.nickName}
Expand All @@ -101,64 +89,59 @@ function ChatTalkMain({ state }: { state: AppStateType }) {
</>
);
}
// 前のメッセージから1分以上経過のものはprimaryに
const prevDate = new Date(state.talkData.value[index - 1].time);
if (date.getTime() - prevDate.getTime() > 60000) {

// 前のメッセージから1時間以上経過のものはprimaryに
const prevDate = index > 0 ? new Date(state.talkData.value[index - 1].timestamp) : null;
if (prevDate && (date.getTime() - prevDate.getTime() > 3600000)) {
return (
<>
{isEncodeDate && (
<ChatDate
date={new Date(data.time)}
/>
)}
{isEncodeDate && <ChatDate date={new Date(data.timestamp)} />}
<ChatOtherMessage
time={data.time}
time={data.timestamp}
message={data.message}
name={data.nickName}
name={data.userName}
nickName={data.nickName}
isPrimary={true}
/>
</>
);
}

return (
<>
{isEncodeDate && (
<ChatDate
date={new Date(data.time)}
/>
)}
{isEncodeDate && <ChatDate date={new Date(data.timestamp)} />}
<ChatOtherMessage
time={data.time}
time={data.timestamp}
message={data.message}
name={data.nickName}
userName={data.userName}
name={data.userName}
nickName={data.nickName}
isPrimary={false}
/>
</>
);
}
} else {
return <ChatDate date={new Date(data.time)} />;
return <ChatDate date={new Date(data.timestamp)} />;
}
})}
</>
);
}

function ChatTalk({ state }: { state: AppStateType }) {
if (state.isChoiceUser.value === true) {
return (
<ul class="c-talk-chat-list">
<ul className="c-talk-chat-list">
<ChatTalkMain state={state} />
</ul>
);
} else {
return (
<div class="flex w-full h-full">
<p class="m-auto">友達を選択してください</p>
<div className="flex w-full h-full">
<p className="m-auto">友達を選択してください</p>
</div>
);
}
}

export default ChatTalk;
2 changes: 1 addition & 1 deletion routes/api/v2/client/friends/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const env = await load();
export const handler = {
async GET(req: any, ctx: any) {
if (!ctx.state.data.loggedIn) {
return ctx.json({ status: false, message: "You are not logged in" });
return new Response(JSON.stringify({ status: false, message: "Not Logged In" }));
}
const userid = ctx.state.data.userid;
const roomsData = await rooms.find({ "users.userid": userid });
Expand Down
2 changes: 1 addition & 1 deletion routes/api/v2/client/friends/request/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import requestAddFriend from "../../../../../../models/reqestAddFriend.ts";
export const handler = {
async GET(req: Request, ctx: any) {
if (!ctx.state.data.loggedIn) {
return ctx.json({ status: false, message: "You are not logged in" });
return new Response(JSON.stringify({ status: false, message: "Not Logged In" }));
}
const sessionid = ctx.state.data.sessionid;
const userid = ctx.state.data.userid;
Expand Down
10 changes: 8 additions & 2 deletions routes/api/v2/client/settings/privacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ import userConfig from "../../../../../models/userConfig.ts";
export const handler = {
async POST(req: Request, ctx: any) {
if (!ctx.state.data.loggedIn) {
return ctx.json({ status: false, message: "You are not logged in" });
return new Response(JSON.stringify({ status: false, message: "Not Logged In" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
let body;
try {
body = await req.json();
} catch (e) {
return ctx.json({ status: false, message: "Invalid JSON" });
return new Response(JSON.stringify({ status: false, message: "Invalid Body" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
const userid = ctx.state.data.userid;
const setting = body.setting;
Expand Down
4 changes: 2 additions & 2 deletions routes/api/v2/client/talks/friend/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const handler = {
if (message.messageType === "text") {
return {
messageid: message.messageid,
userName: CacheUser.userName,
userName: CacheUser.userName + "@" + takos.splitUserName(message.userid).domain,
nickName: CacheUser.nickName,
message: message.message,
timestamp: message.timestamp,
Expand Down Expand Up @@ -142,7 +142,7 @@ export const handler = {
if (message.messageType === "text") {
return {
messageid: message.messageid,
userName: userInfo?.userName,
userName: userInfo?.userName + "@" + takos.splitUserName(message.userid).domain,
nickName: userInfo?.nickName,
message: message.message,
timestamp: message.timestamp,
Expand Down
5 changes: 4 additions & 1 deletion routes/api/v2/client/talks/friend/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export const handler = {
try {
body = await req.json();
} catch (e) {
return ctx.json({ status: false, message: "Invalid JSON" });
return new Response(JSON.stringify({ status: false, message: "Invalid Body" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
const message = body.text;
const sessionid = body.sessionid;
Expand Down
5 changes: 4 additions & 1 deletion routes/api/v2/client/users/friendkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { getCookies } from "$std/http/cookie.ts";
export const handler = {
async GET(req: Request, ctx: any) {
if (!ctx.state.data.loggedIn) {
return ctx.json({ status: false, message: "You are not logged in" });
return new Response(JSON.stringify({ status: false, message: "Not Logged In" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
const userid = ctx.state.data.userid;
const url = new URL(req.url);
Expand Down
2 changes: 1 addition & 1 deletion routes/api/v2/client/users/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import takos from "../../../../../util/takos.ts";
export const handler = {
async GET(req: Request, ctx: any) {
if (!ctx.state.data.loggedIn) {
return ctx.json({ status: false, message: "You are not logged in" });
return new Response("Not Logged In", { status: 401 });
}
const userid = ctx.state.data.userid;
try {
Expand Down
5 changes: 4 additions & 1 deletion routes/api/v2/client/users/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import userConfig from "../../../../../models/userConfig.ts";
export const handler = {
async GET(req: Request, ctx: any) {
if (!ctx.state.data.loggedIn) {
return ctx.json({ status: false, message: "You are not logged in" });
return new Response(JSON.stringify({ status: false, message: "Not Logged In" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
const userid = ctx.state.data.userid;
const config = await userConfig.findOne({ userid: userid });
Expand Down
Loading

0 comments on commit 026329b

Please sign in to comment.