Skip to content

Commit 39bf6e7

Browse files
authored
fixed websocket (#557)
# PRの概要 ## 具体的な変更内容 ## 影響範囲 ## 動作要件 ## 補足 ## レビューリクエストを出す前にチェック! - [ ] 改めてセルフレビューしたか - [ ] 手動での動作検証を行ったか - [ ] server の機能追加ならば、テストを書いたか - 理由: 書いた | server の機能追加ではない - [ ] 間違った使い方が存在するならば、それのドキュメントをコメントで書いたか - 理由: 書いた | 間違った使い方は存在しない - [ ] わかりやすいPRになっているか <!-- レビューリクエスト後は、Slackでもメンションしてお願いすることを推奨します。 -->
1 parent 7561741 commit 39bf6e7

File tree

7 files changed

+102
-93
lines changed

7 files changed

+102
-93
lines changed

server/src/database/chat.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@ export async function markAsRead(
127127
reader: UserID,
128128
message: MessageID,
129129
) {
130-
const val = {
131-
readerId: reader,
132-
messageId: message,
133-
relationId: rel,
134-
};
135130
return await prisma.message.updateMany({
136131
where: {
137132
id: {
@@ -161,6 +156,7 @@ export async function sendDM(
161156
try {
162157
const message = await prisma.message.create({
163158
data: {
159+
// isPicture: false, // todo: bring it back
164160
relationId: relation,
165161
read: false,
166162
...content,

server/src/functions/chat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function sendDM(
5656
};
5757

5858
const result = await db.sendDM(rel.value.id, msg);
59-
if (!result.ok) return http.internalError("");
59+
if (!result.ok) return http.internalError("Failed to send DM");
6060
return http.created(result.value);
6161
}
6262

server/src/router/chat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ router.post("/dm/to/:userid", async (req, res) => {
3838

3939
const result = await core.sendDM(user.value, friend.value, send.data);
4040
if (result.ok) {
41-
ws.sendMessage(result?.body, friend.value);
41+
ws.sendMessage(result.body, friend.value);
4242
}
4343
res.status(result.code).send(result.body);
4444
});

web/api/internal/endpoints.ts

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { CourseID, Day, GUID } from "common/types";
22
import type { MessageID, ShareRoomID } from "common/types";
3+
import { panic } from "~/lib/utils";
34

4-
export const origin: string | null = process.env.NEXT_PUBLIC_API_ENDPOINT ?? "";
5-
if (!origin) throw new Error("process.env.NEXT_PUBLIC_API_ENDPOINT not found!");
5+
export const API_ENDPOINT: string =
6+
process.env.NEXT_PUBLIC_API_ENDPOINT ??
7+
panic("process.env.NEXT_PUBLIC_API_ENDPOINT not found!");
68

79
// TODO: de-export this and use one from /common
810
export type UserID = number;
@@ -17,7 +19,7 @@ export type UserID = number;
1719
* - 500: internal error.
1820
**/
1921
export const user = (userId: UserID) => {
20-
return `${origin}/users/id/${userId}`;
22+
return `${API_ENDPOINT}/users/id/${userId}`;
2123
};
2224

2325
/**
@@ -36,7 +38,7 @@ export const user = (userId: UserID) => {
3638
* - 500: internal error.
3739
*
3840
**/
39-
export const users = `${origin}/users`;
41+
export const users = `${API_ENDPOINT}/users`;
4042

4143
/**
4244
* [v] 実装済み
@@ -47,7 +49,7 @@ export const users = `${origin}/users`;
4749
* - 401: auth error.
4850
* - 500: internal error
4951
**/
50-
export const recommendedUsers = `${origin}/users/recommended`;
52+
export const recommendedUsers = `${API_ENDPOINT}/users/recommended`;
5153

5254
/**
5355
* [v] 実装済み
@@ -72,7 +74,7 @@ export const recommendedUsers = `${origin}/users/recommended`;
7274
* - 500: internal error.
7375
*
7476
**/
75-
export const me = `${origin}/users/me`;
77+
export const me = `${API_ENDPOINT}/users/me`;
7678

7779
/**
7880
* [v] 実装済み
@@ -83,7 +85,7 @@ export const me = `${origin}/users/me`;
8385
* - 401: unauthorized.
8486
* - 500: internal error.
8587
**/
86-
export const matchedUsers = `${origin}/users/matched`;
88+
export const matchedUsers = `${API_ENDPOINT}/users/matched`;
8789

8890
/**
8991
* [v] 実装済み
@@ -94,7 +96,7 @@ export const matchedUsers = `${origin}/users/matched`;
9496
* - 401: unauthorized.
9597
* - 500: internal error.
9698
**/
97-
export const pendingRequestsToMe = `${origin}/users/pending/to-me`;
99+
export const pendingRequestsToMe = `${API_ENDPOINT}/users/pending/to-me`;
98100

99101
/**
100102
* [v] 実装済み
@@ -105,7 +107,7 @@ export const pendingRequestsToMe = `${origin}/users/pending/to-me`;
105107
* - 401: unauthorized.
106108
* - 500: internal error.
107109
**/
108-
export const pendingRequestsFromMe = `${origin}/users/pending/from-me`;
110+
export const pendingRequestsFromMe = `${API_ENDPOINT}/users/pending/from-me`;
109111

110112
/**
111113
* [v] 実装済み
@@ -117,7 +119,7 @@ export const pendingRequestsFromMe = `${origin}/users/pending/from-me`;
117119
* - 500: internal error.
118120
**/
119121
export const userByGUID = (guid: GUID) => {
120-
return `${origin}/users/guid/${guid}`;
122+
return `${API_ENDPOINT}/users/guid/${guid}`;
121123
};
122124

123125
// this one may be public to anyone.
@@ -131,7 +133,7 @@ export const userByGUID = (guid: GUID) => {
131133
* - 500: internal error.
132134
**/
133135
export const userExists = (guid: GUID) => {
134-
return `${origin}/users/exists/${guid}`;
136+
return `${API_ENDPOINT}/users/exists/${guid}`;
135137
};
136138

137139
/**
@@ -145,7 +147,7 @@ export const userExists = (guid: GUID) => {
145147
* - 500: internal error.
146148
**/
147149
export const match = (opponentID: UserID) => {
148-
return `${origin}/matches/${opponentID}`;
150+
return `${API_ENDPOINT}/matches/${opponentID}`;
149151
};
150152

151153
/**
@@ -158,7 +160,7 @@ export const match = (opponentID: UserID) => {
158160
* - 401: unauthorized.
159161
* - 500: internal error.
160162
**/
161-
export const matches = `${origin}/matches`;
163+
export const matches = `${API_ENDPOINT}/matches`;
162164

163165
/**
164166
* [x] 実装済み
@@ -180,7 +182,7 @@ export const matches = `${origin}/matches`;
180182
* - 500: internal error.
181183
*/
182184
export const coursesUserId = (userId: UserID) => {
183-
return `${origin}/courses/userId/${userId}`;
185+
return `${API_ENDPOINT}/courses/userId/${userId}`;
184186
};
185187

186188
/**
@@ -206,7 +208,7 @@ export const coursesUserId = (userId: UserID) => {
206208
* - 401: unauthorized.
207209
* - 500: internal error.
208210
*/
209-
export const coursesMine = `${origin}/courses/mine`;
211+
export const coursesMine = `${API_ENDPOINT}/courses/mine`;
210212

211213
/**
212214
* [v] 実装済み
@@ -218,7 +220,7 @@ export const coursesMine = `${origin}/courses/mine`;
218220
* - 500: internal error.
219221
*/
220222
export const coursesMineOverlaps = (courseId: CourseID) => {
221-
return `${origin}/courses/mine/overlaps/${courseId}`;
223+
return `${API_ENDPOINT}/courses/mine/overlaps/${courseId}`;
222224
};
223225

224226
/**
@@ -231,7 +233,7 @@ export const coursesMineOverlaps = (courseId: CourseID) => {
231233
* - 500: internal error.
232234
**/
233235
export const coursesDayPeriod = (day: Day, period: number) => {
234-
return `${origin}/courses/day-period?day=${day}&period=${period}`;
236+
return `${API_ENDPOINT}/courses/day-period?day=${day}&period=${period}`;
235237
};
236238

237239
/**
@@ -243,7 +245,7 @@ export const coursesDayPeriod = (day: Day, period: number) => {
243245
* - 500: internal error.
244246
**/
245247
export const sendRequest = (opponentId: UserID) => {
246-
return `${origin}/requests/send/${opponentId}`;
248+
return `${API_ENDPOINT}/requests/send/${opponentId}`;
247249
};
248250

249251
/**
@@ -254,7 +256,7 @@ export const sendRequest = (opponentId: UserID) => {
254256
* - 500: internal error
255257
**/
256258
export const cancelRequest = (opponentId: UserID) => {
257-
return `${origin}/requests/cancel/${opponentId}`;
259+
return `${API_ENDPOINT}/requests/cancel/${opponentId}`;
258260
};
259261
/**
260262
* [v] 実装済み
@@ -265,7 +267,7 @@ export const cancelRequest = (opponentId: UserID) => {
265267
* - 500: internal error.
266268
**/
267269
export const acceptRequest = (opponentId: UserID) => {
268-
return `${origin}/requests/accept/${opponentId}`;
270+
return `${API_ENDPOINT}/requests/accept/${opponentId}`;
269271
};
270272

271273
/**
@@ -278,13 +280,13 @@ export const acceptRequest = (opponentId: UserID) => {
278280
* - 500: internal error.
279281
**/
280282
export const rejectRequest = (opponentId: UserID) => {
281-
return `${origin}/requests/reject/${opponentId}`;
283+
return `${API_ENDPOINT}/requests/reject/${opponentId}`;
282284
};
283285

284286
/**
285287
**/
286288
export const markAsRead = (friendId: UserID, messageId: MessageID) => {
287-
return `${origin}/chat/mark-as-read/${friendId}/${messageId}`;
289+
return `${API_ENDPOINT}/chat/mark-as-read/${friendId}/${messageId}`;
288290
};
289291
/**
290292
* []実装済み
@@ -295,7 +297,7 @@ export const markAsRead = (friendId: UserID, messageId: MessageID) => {
295297
* - 401: unauthorized
296298
* - 500: internal error
297299
*/
298-
export const roomOverview = `${origin}/chat/overview`;
300+
export const roomOverview = `${API_ENDPOINT}/chat/overview`;
299301

300302
/**
301303
* []実装済み
@@ -308,7 +310,7 @@ export const roomOverview = `${origin}/chat/overview`;
308310
* - 403: Forbidden
309311
* - 500: internal error
310312
**/
311-
export const dmTo = (userId: UserID) => `${origin}/chat/dm/to/${userId}`;
313+
export const dmTo = (userId: UserID) => `${API_ENDPOINT}/chat/dm/to/${userId}`;
312314

313315
/**
314316
* PUT -> start dm with userId. created one if none was found. authorized.
@@ -322,7 +324,8 @@ export const dmTo = (userId: UserID) => `${origin}/chat/dm/to/${userId}`;
322324
* - 403: forbidden. you and the user are not matched yet.
323325
* - 500: internal error.
324326
**/
325-
export const dmWith = (userId: UserID) => `${origin}/chat/dm/with/${userId}`;
327+
export const dmWith = (userId: UserID) =>
328+
`${API_ENDPOINT}/chat/dm/with/${userId}`;
326329

327330
/**
328331
* POST -> Create a room. authenticated
@@ -334,15 +337,15 @@ export const dmWith = (userId: UserID) => `${origin}/chat/dm/with/${userId}`;
334337
* - 403: forbidden (cannot invite non-friends)
335338
* - 500: internal error
336339
**/
337-
export const sharedRooms = `${origin}/chat/shared`;
340+
export const sharedRooms = `${API_ENDPOINT}/chat/shared`;
338341

339342
/** authorized
340343
* GET -> Get info of a room (including the message log).
341344
* PATCH -> update room info. (excluding the message log).
342345
* - body: UpdateRoom
343346
**/
344347
export const sharedRoom = (roomId: ShareRoomID) =>
345-
`${origin}/chat/shared/${roomId}`;
348+
`${API_ENDPOINT}/chat/shared/${roomId}`;
346349

347350
/** POST: invite. authorized
348351
* - body: UserID[]
@@ -353,24 +356,24 @@ export const sharedRoom = (roomId: ShareRoomID) =>
353356
* - 500: internal error
354357
**/
355358
export const roomInvite = (roomId: ShareRoomID) =>
356-
`${origin}/chat/shared/id/${roomId}/invite`;
359+
`${API_ENDPOINT}/chat/shared/id/${roomId}/invite`;
357360

358361
/**
359362
* PATCH: authorized body=SendMessage
360363
* DELETE: authorized
361364
**/
362365
export const message = (messageId: MessageID) =>
363-
`${origin}/chat/messages/id/${messageId}`;
366+
`${API_ENDPOINT}/chat/messages/id/${messageId}`;
364367

365368
/**
366369
* GET: get profile picture of URL (this is usually hard-encoded in pictureURL so this variable is barely used)
367370
*/
368-
export const pictureOf = (guid: GUID) => `${origin}/picture/${guid}`;
371+
export const pictureOf = (guid: GUID) => `${API_ENDPOINT}/picture/${guid}`;
369372

370373
/**
371374
* POST: update my profile picture.
372375
*/
373-
export const picture = `${origin}/picture`;
376+
export const picture = `${API_ENDPOINT}/picture`;
374377

375378
export default {
376379
user,

0 commit comments

Comments
 (0)