Skip to content

Commit 1c22e3e

Browse files
committed
fix types and some flaws
1 parent 28369e4 commit 1c22e3e

File tree

5 files changed

+37
-24
lines changed

5 files changed

+37
-24
lines changed

common/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export type {
4040
export type SSEChatEvents = {
4141
"Chat:Append": {
4242
message: Message;
43+
sender: string; // user name
4344
};
4445
"Chat:Update": {
4546
id: number;
@@ -50,3 +51,8 @@ export type SSEChatEvents = {
5051
};
5152
"Chat:Ping": "";
5253
};
54+
export type SSEChatEventEnum = "Chat:Append" | "Chat:Update" | "Chat:Delete";
55+
export type SSEChatEvent<T extends SSEChatEventEnum> = {
56+
event: T;
57+
data: SSEChatEvents[T];
58+
};

server/src/functions/chat.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
SendMessage,
99
ShareRoomID,
1010
} from "common/types";
11+
import { HTTPException } from "hono/http-exception";
1112
import * as db from "../database/chat";
1213
import { areAllMatched, getRelation } from "../database/matches";
1314
import { getUserByID } from "../database/users";
@@ -37,12 +38,13 @@ export async function sendDM(
3738
from: UserID,
3839
to: UserID,
3940
send: SendMessage,
40-
): Promise<http.Response<Message>> {
41+
): Promise<Message> {
4142
const rel = await getRelation(from, to);
4243
if (rel.status === "REJECTED")
43-
return http.forbidden(
44-
"You cannot send a message because the friendship request was rejected.",
45-
);
44+
throw new HTTPException(403, {
45+
message:
46+
"You cannot send a message because the friendship request was rejected.",
47+
});
4648

4749
// they are now MATCHED
4850
const msg: Omit<Omit<Message, "id">, "isPicture"> = {
@@ -53,8 +55,9 @@ export async function sendDM(
5355
};
5456

5557
const result = await db.sendDM(rel.id, msg);
56-
if (!result) return http.internalError("Failed to send DM");
57-
return http.created(result);
58+
if (!result)
59+
throw new HTTPException(500, { message: "Failed to send message" });
60+
return result;
5861
}
5962

6063
export async function getDM(

server/src/router/chat.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import {
77
SharedRoomSchema,
88
} from "common/zod/schemas";
99
import { Hono } from "hono";
10+
import { HTTPException } from "hono/http-exception";
1011
import { z } from "zod";
1112
import * as db from "../database/chat";
13+
import { prisma } from "../database/client";
1214
import { getUserId } from "../firebase/auth/db";
1315
import * as core from "../functions/chat";
1416
import { json, param } from "../lib/validator";
@@ -28,14 +30,23 @@ const router = new Hono()
2830
const friend = c.req.valid("param").userid;
2931
const send = c.req.valid("json");
3032
const result = await core.sendDM(user, friend, send);
31-
if (result.ok) {
32-
sse.send(friend, {
33-
event: "Chat:Append",
34-
data: { message: result.body },
35-
});
36-
}
37-
c.status(result.code);
38-
return c.json(result.body);
33+
const senderName = await prisma.user.findUnique({
34+
where: {
35+
id: user,
36+
},
37+
select: {
38+
name: true,
39+
},
40+
});
41+
if (!senderName)
42+
throw new HTTPException(500, { message: "sender not found???" });
43+
44+
sse.send(friend, {
45+
event: "Chat:Append",
46+
data: { message: result, sender: senderName?.name },
47+
});
48+
c.status(201);
49+
return c.json(result);
3950
})
4051

4152
// GET a DM Room with userId, CREATE one if not found.

server/src/router/sse.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import type { SSEChatEvents, UserID } from "common/types";
1+
import type { SSEChatEvent, SSEChatEventEnum, UserID } from "common/types";
22
import { Hono } from "hono";
33
import { HTTPException } from "hono/http-exception";
44
import { streamSSE } from "hono/streaming";
55
import { getUserIdFromToken } from "../firebase/auth/db";
66

77
export const sseChatPath = (id: UserID) => `sse:chat:${id}`;
8-
type SSEChatEventEnum = "Chat:Append" | "Chat:Update" | "Chat:Delete";
9-
type SSEChatEvent<T extends SSEChatEventEnum> = {
10-
event: T;
11-
data: SSEChatEvents[T];
12-
};
138
export function send<T extends SSEChatEventEnum>(
149
to: UserID,
1510
event: SSEChatEvent<T>,

web/components/data/socket.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ async function main({ signal }: { signal: AbortSignal }) {
2424

2525
sse.addEventListener(
2626
"Chat:Append",
27-
async (ev) => {
27+
(ev) => {
2828
console.log(ev);
2929
const data = JSON.parse(ev.data) as SSEChatEvents["Chat:Append"];
3030
const msg = data.message;
3131
// if it exists and caught the create sig
3232
if (handlers.onCreate?.(msg)) return;
33-
const creator = await user.get(msg.creator);
34-
if (!creator) return;
35-
enqueueSnackbar(`${creator.name}さんからのメッセージ : ${msg.content}`, {
33+
enqueueSnackbar(`${data.sender}さんからのメッセージ : ${msg.content}`, {
3634
variant: "info",
3735
});
3836
},

0 commit comments

Comments
 (0)