From 6d4af0c1beba793feeef62b683a548bad0b9eca0 Mon Sep 17 00:00:00 2001 From: kotto5 Date: Sun, 18 Feb 2024 23:39:26 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[backend]=20login=20logoff=20=E6=99=82?= =?UTF-8?q?=E3=81=ABuser=20=E3=81=AEname=20=E3=82=82=E9=80=81=E4=BF=A1?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/chat/chat.service.ts | 26 +++++++++++++++++--------- backend/test/utils/matcher.ts | 5 +++-- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/backend/src/chat/chat.service.ts b/backend/src/chat/chat.service.ts index 809b0092..94084594 100644 --- a/backend/src/chat/chat.service.ts +++ b/backend/src/chat/chat.service.ts @@ -18,6 +18,12 @@ export enum UserStatus { Online = 0b1, } +type OnlineStatus = { + name: string; + userId: number; + status: UserStatus; +}; + @Injectable() @WebSocketGateway() export class ChatService { @@ -181,7 +187,7 @@ export class ChatService { this.statuses.set(user.id, UserStatus.Online); client.emit('online-status', this.getUserStatuses()); client.broadcast.emit('online-status', [ - { userId: user.id, status: UserStatus.Online }, + { userId: user.id, status: UserStatus.Online, name: user.name }, ]); } catch (error) { console.log(error); @@ -209,6 +215,7 @@ export class ChatService { const emitData = { userId: this.getUserId(client), status: UserStatus.Offline, + name: this.getUser(client).name, }; if (emitData.userId) { client.broadcast.emit('online-status', [emitData]); @@ -216,14 +223,15 @@ export class ChatService { this.removeClient(client); } - getUserStatuses(): { - userId: number; - status: UserStatus; - }[] { - return Array.from(this.statuses).map(([userId, status]) => ({ - userId, - status, - })); + getUserStatuses(): OnlineStatus[] { + return Array.from(this.users).map(([id, user]) => { + id; // TODO : remove this + return { + userId: user.id, + name: user.name, + status: this.statuses.get(user.id) || UserStatus.Offline, + }; + }); } private async expectNotBlockedBy(blockerId: number, userId: number) { diff --git a/backend/test/utils/matcher.ts b/backend/test/utils/matcher.ts index 265e9623..22618aec 100644 --- a/backend/test/utils/matcher.ts +++ b/backend/test/utils/matcher.ts @@ -129,12 +129,13 @@ export function expectPostGenerateTwoFactorAuthenticationSecretResponse( } export function expectOnlineStatusResponse( - users: { userId: number; status: UserStatus }[], + users: { userId: number; status: UserStatus; name: string }[], ) { - type User = { userId: number; status: UserStatus }; + type User = { userId: number; status: UserStatus; name: string }; const expected: User[] = users.map(() => ({ userId: expect.any(Number), status: expect.any(Number), + name: expect.any(String), })); expect(users).toEqual(expected); } From baf765e31e0488ac0636f1961a463bbc1a4ab0d8 Mon Sep 17 00:00:00 2001 From: kotto5 Date: Sun, 18 Feb 2024 23:39:42 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[frontend]=20=E5=8F=97=E3=81=91=E5=8F=96?= =?UTF-8?q?=E3=81=A3=E3=81=9Fuser=20=E5=90=8D=E3=82=92=E5=87=BA=E5=8A=9B?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/app/lib/client-socket-provider.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/frontend/app/lib/client-socket-provider.tsx b/frontend/app/lib/client-socket-provider.tsx index 0056bdb3..729fa7e4 100644 --- a/frontend/app/lib/client-socket-provider.tsx +++ b/frontend/app/lib/client-socket-provider.tsx @@ -62,10 +62,15 @@ export default function SocketProvider() { }); }; - const handleOnlineStatus = (users: { userId: number; status: number }[]) => { + const handleOnlineStatus = ( + users: { userId: number; status: number; name: string }[] + ) => { + const description = users.map((u) => { + return `[${u.name} has logged ${u.status === 1 ? "in" : "out"}] `; + }); toast({ title: "online-status", - description: JSON.stringify(users), + description, }); }; From 217a40180a6701f54155897eec847374733c157b Mon Sep 17 00:00:00 2001 From: kotto5 Date: Sun, 18 Feb 2024 23:42:21 +0900 Subject: [PATCH 3/3] make fmt --- frontend/app/lib/client-socket-provider.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/app/lib/client-socket-provider.tsx b/frontend/app/lib/client-socket-provider.tsx index 729fa7e4..dc835a28 100644 --- a/frontend/app/lib/client-socket-provider.tsx +++ b/frontend/app/lib/client-socket-provider.tsx @@ -63,7 +63,7 @@ export default function SocketProvider() { }; const handleOnlineStatus = ( - users: { userId: number; status: number; name: string }[] + users: { userId: number; status: number; name: string }[], ) => { const description = users.map((u) => { return `[${u.name} has logged ${u.status === 1 ? "in" : "out"}] `;