From 505cdefefb790a04cd8f4006afb676cafa986241 Mon Sep 17 00:00:00 2001 From: "M. E. Abdelsalam" Date: Wed, 25 Dec 2024 16:55:29 +0200 Subject: [PATCH] refactor: wss revert has been reimplemented to utilize socket.io acknowledgement. --- apps/nova/src/hooks/chat.ts | 2 +- packages/types/src/wss.ts | 38 ++--- services/server/fixtures/wss.ts | 24 ++- services/server/src/wss/handlers/base.ts | 5 +- services/server/src/wss/handlers/call.ts | 5 +- .../server/src/wss/handlers/connection.ts | 17 +- .../server/src/wss/handlers/inputDevices.ts | 6 +- services/server/src/wss/handlers/message.ts | 149 +++++++++--------- services/server/src/wss/handlers/peer.ts | 6 +- services/server/tests/wss/messages.test.ts | 78 ++++----- 10 files changed, 155 insertions(+), 175 deletions(-) diff --git a/apps/nova/src/hooks/chat.ts b/apps/nova/src/hooks/chat.ts index 9f6fcc30..256044a9 100644 --- a/apps/nova/src/hooks/chat.ts +++ b/apps/nova/src/hooks/chat.ts @@ -16,7 +16,7 @@ export function useChat(onMessage?: OnMessage) { const sendMessage = useCallback( ({ roomId, text }: { roomId: number; text: string }) => { - socket?.emit(Wss.ClientEvent.SendMessage, { ref: 1, roomId, text }); + socket?.emit(Wss.ClientEvent.SendMessage, { roomId, text }); }, [socket] ); diff --git a/packages/types/src/wss.ts b/packages/types/src/wss.ts index 70660f66..1ab6d9e2 100644 --- a/packages/types/src/wss.ts +++ b/packages/types/src/wss.ts @@ -32,7 +32,6 @@ export enum ServerEvent { RoomMessageDeleted = "RoomMessageDeleted", RoomMessageRead = "RoomMessageRead", JoinedRooms = "JoinedRooms", - Revert = "Revert", MessageRead = "MessageRead", @@ -72,7 +71,7 @@ export enum Room { ServerStats = "ServerStats", } -export enum RevertErrorCode { +export enum AcknowledgeCodes { EmptyText = 'empty-text', RoomNotFound = 'room-not-found', MessageNotFound = 'message-not-found', @@ -80,39 +79,23 @@ export enum RevertErrorCode { NotOwner = 'not-owner', Unreachable = 'unreachable', Unallowed = 'unallowed', + Ok = 'ok', } -export type WithRevetCode = T & {code: RevertErrorCode}; - -export type RevertPayload = WithRevetCode< - | { - type: "send-message"; - ref: number; - reason: string; - } - | { - type: "update-message" | "delete-message" | "mark-msg-as-read"; - id: number; - reason: string; - } - | { - type: "user-typing"; - roomId: number; - reason: string; - }>; - -type EventCallback = (arg: T) => Promise | void; +export type AcknowledgePayload = { + code: AcknowledgeCodes; + message?: string; +} + +export type AcknowledgeCallback = (payload?: AcknowledgePayload) => any; + +type EventCallback = (arg: T, callback?: AcknowledgeCallback) => Promise | void; /** * Events emitted by the client */ export type ClientEventsMap = { [ClientEvent.SendMessage]: EventCallback<{ - /** - * Temporarily id set by the client. Will be emitted by the server incase - * the is a problem with tihs message. - */ - ref: number; roomId: number; text: string; }>; @@ -144,7 +127,6 @@ export type ServerEventsMap = { roomId: number; messageId: number; }>; - [ServerEvent.Revert]: EventCallback; [ServerEvent.RoomMessageRead]: EventCallback<{ userId: number }>; [ServerEvent.UserJoinedCall]: EventCallback<{ peerId: string }>; diff --git a/services/server/fixtures/wss.ts b/services/server/fixtures/wss.ts index 68387d2f..30e9fd70 100644 --- a/services/server/fixtures/wss.ts +++ b/services/server/fixtures/wss.ts @@ -13,27 +13,37 @@ export class ClientSocket { } userTyping(roomId: number) { - this.client.emit(Wss.ClientEvent.UserTyping, { roomId }); + return this.emit( Wss.ClientEvent.UserTyping, { roomId }); } joinCall(callId: number, type: ICall.Type) { - this.client.emit(Wss.ClientEvent.JoinCall, { callId, type }); + return this.emit(Wss.ClientEvent.JoinCall, { callId, type }); } leaveCall(callId: number) { - this.client.emit(Wss.ClientEvent.LeaveCall, { callId }); + return this.emit(Wss.ClientEvent.LeaveCall, { callId }); } - sendMessage(roomId: number, ref: number, text: string) { - this.client.emit(Wss.ClientEvent.SendMessage, { roomId, ref, text }); + sendMessage(roomId: number, text: string) { + return this.emit(Wss.ClientEvent.SendMessage, { roomId, text }); } deleteMessage(msgId: number) { - this.client.emit(Wss.ClientEvent.DeleteMessage, { id: msgId }); + return this.emit(Wss.ClientEvent.DeleteMessage, { id: msgId }); } markMessageAsRead(msgId: number) { - this.client.emit(Wss.ClientEvent.MarkMessageAsRead, { id: msgId }); + return this.emit(Wss.ClientEvent.MarkMessageAsRead, { id: msgId }); + } + + async emit( + event: keyof Wss.ClientEventsMap, + data: any + ): Promise { + return new Promise((resolve, _) => { + this.client.emit( event, data, (ack) => ack && resolve(ack)); + setTimeout(() => resolve({ code: Wss.AcknowledgeCodes.Ok }), 2_000); + }); } /** diff --git a/services/server/src/wss/handlers/base.ts b/services/server/src/wss/handlers/base.ts index 847d9be2..c35b9e1e 100644 --- a/services/server/src/wss/handlers/base.ts +++ b/services/server/src/wss/handlers/base.ts @@ -1,4 +1,5 @@ import { IUser, Wss } from "@litespace/types"; +import { AcknowledgeCallback, AcknowledgePayload } from "@litespace/types/dist/esm/wss"; import { Socket } from "socket.io"; export abstract class WssHandler { @@ -19,7 +20,7 @@ export abstract class WssHandler { this.socket.broadcast.to(room).emit(event, ...data); } - revert(payload: Wss.RevertPayload) { - this.socket.emit(Wss.ServerEvent.Revert, payload); + protected must(callback: AcknowledgeCallback | undefined, payload: AcknowledgePayload): void { + return callback ? callback(payload) : console.error("Socket.io callback is not defined!"); } } diff --git a/services/server/src/wss/handlers/call.ts b/services/server/src/wss/handlers/call.ts index cbb07ed2..af7ddc6d 100644 --- a/services/server/src/wss/handlers/call.ts +++ b/services/server/src/wss/handlers/call.ts @@ -64,7 +64,8 @@ export class Call extends WssHandler { this.broadcast( Wss.ServerEvent.MemberJoinedCall, asCallRoomId(callId), - { userId: user.id } // TODO: define the payload struct type in the types package + { userId: user.id }, // TODO: define the payload struct type in the types package + () => {} ); }); if (result instanceof Error) stdout.error(result.message); @@ -97,7 +98,7 @@ export class Call extends WssHandler { .to(asCallRoomId(callId)) .emit(Wss.ServerEvent.MemberLeftCall, { userId: user.id, - }); + }, () => {}); }); if (result instanceof Error) stdout.error(result.message); } diff --git a/services/server/src/wss/handlers/connection.ts b/services/server/src/wss/handlers/connection.ts index 3d9f656a..d7390729 100644 --- a/services/server/src/wss/handlers/connection.ts +++ b/services/server/src/wss/handlers/connection.ts @@ -58,7 +58,8 @@ export class Connection extends WssHandler { this.broadcast( Wss.ServerEvent.UserStatusChanged, room.toString(), - { online } + { online }, + () => {} ); } } @@ -66,7 +67,11 @@ export class Connection extends WssHandler { private async emitServerStats() { background.on("message", (message: PartentPortMessage) => { if (message.type === PartentPortMessageType.Stats) - return this.socket.emit(Wss.ServerEvent.ServerStats, message.stats); + return this.socket.emit( + Wss.ServerEvent.ServerStats, + message.stats, + () => {} + ); }); } @@ -124,8 +129,10 @@ export class Connection extends WssHandler { // notify members that a member has left the call this.socket.broadcast .to(asCallRoomId(callId)) - .emit(Wss.ServerEvent.MemberLeftCall, { - userId: user.id, - }); + .emit( + Wss.ServerEvent.MemberLeftCall, + { userId: user.id }, + () => {} + ); } } diff --git a/services/server/src/wss/handlers/inputDevices.ts b/services/server/src/wss/handlers/inputDevices.ts index c136b2a2..78fdfb03 100644 --- a/services/server/src/wss/handlers/inputDevices.ts +++ b/services/server/src/wss/handlers/inputDevices.ts @@ -1,11 +1,9 @@ import { logger, safe } from "@litespace/sol"; -import { rooms } from "@litespace/models"; import { isGhost } from "@litespace/auth"; import { Wss } from "@litespace/types"; import { id, boolean } from "@/validation/utils"; import { WssHandler } from "@/wss/handlers/base"; import zod from "zod"; -import { isEmpty } from "lodash"; const toggleCameraPayload = zod.object({ call: id, camera: boolean }); const toggleMicPayload = zod.object({ call: id, mic: boolean }); @@ -31,7 +29,7 @@ export class InputDevices extends WssHandler { this.broadcast(Wss.ServerEvent.CameraToggled, call.toString(), { user: user.id, camera, - }); + }, () => {}); }); if (error instanceof Error) stdout.error(error.message); } @@ -45,7 +43,7 @@ export class InputDevices extends WssHandler { this.broadcast(Wss.ServerEvent.MicToggled, call.toString(), { user: user.id, mic, - }); + }, () => {}); }); if (error instanceof Error) stdout.error(error.message); } diff --git a/services/server/src/wss/handlers/message.ts b/services/server/src/wss/handlers/message.ts index 6bc3ded7..723b410d 100644 --- a/services/server/src/wss/handlers/message.ts +++ b/services/server/src/wss/handlers/message.ts @@ -12,7 +12,6 @@ const stdout = logger("wss"); const sendMessagePayload = zod.object({ roomId: id, - ref: id, text: zod.string(), }); const updateMessagePayload = zod.object({ text: string, id }); @@ -22,7 +21,10 @@ const markMessageAsReadPayload = zod.object({ id }); export class Messages extends WssHandler { public init(): Messages { - this.socket.on(Wss.ClientEvent.SendMessage, this.onSendMessage.bind(this)); + this.socket.on( + Wss.ClientEvent.SendMessage, + this.onSendMessage.bind(this) + ); this.socket.on( Wss.ClientEvent.UpdateMessage, this.onUpdateMessage.bind(this) @@ -31,7 +33,10 @@ export class Messages extends WssHandler { Wss.ClientEvent.DeleteMessage, this.onDeleteMessage.bind(this) ); - this.socket.on(Wss.ClientEvent.UserTyping, this.onUserTyping.bind(this)); + this.socket.on( + Wss.ClientEvent.UserTyping, + this.onUserTyping.bind(this) + ); this.socket.on( Wss.ClientEvent.MarkMessageAsRead, this.onMarkMessageAsRead.bind(this) @@ -39,37 +44,34 @@ export class Messages extends WssHandler { return this; } - async onSendMessage(data: unknown) { + async onSendMessage(data: unknown, callback?: Wss.AcknowledgeCallback) { const error = await safe(async () => { const user = this.user; if (isGhost(user)) return; - const { roomId, ref, text } = sendMessagePayload.parse(data); - - const revert = (code: Wss.RevertErrorCode, reason: string) => - this.revert({ type: "send-message", ref, reason, code }); + const { roomId, text } = sendMessagePayload.parse(data); // todo: set a max message length - if (!text) return revert( - Wss.RevertErrorCode.EmptyText, - "Cannot send an empty message." - ); + if (!text) return this.must(callback, { + code: Wss.AcknowledgeCodes.EmptyText, + message: "Cannot send an empty message." + }); const room = await rooms.findById(roomId); - if (!room) return revert( - Wss.RevertErrorCode.RoomNotFound, - "Cannot find the room in the database." - ); + if (!room) return this.must(callback, { + code: Wss.AcknowledgeCodes.RoomNotFound, + message: "Cannot find the room in the database." + }); const userId = user.id; stdout.log(`u:${userId} is sending a message to r:${roomId}`); const members = await rooms.findRoomMembers({ roomIds: [roomId] }); const member = members.map((member) => member.id).includes(userId); - if (!member) return revert( - Wss.RevertErrorCode.NotMember, - "The user is not a member of the room!" - ); + if (!member) return this.must(callback, { + code: Wss.AcknowledgeCodes.NotMember, + message: "The user is not a member of the room!" + }); const message = await messages.create({ text, @@ -87,69 +89,65 @@ export class Messages extends WssHandler { if (error instanceof Error) stdout.log(error.message); } - async onUpdateMessage(data: unknown) { + async onUpdateMessage(data: unknown, callback?: Wss.AcknowledgeCallback) { const error = await safe(async () => { const user = this.user; if (isGhost(user)) return; const { id, text } = updateMessagePayload.parse(data); - const revert = (code: Wss.RevertErrorCode, reason: string) => - this.revert({ type: "update-message", id, reason, code }); - const message = await messages.findById(id); if (!message || message.deleted) - return revert( - Wss.RevertErrorCode.MessageNotFound, - "Cannot find the message in the database." - ); + return this.must(callback, { + code: Wss.AcknowledgeCodes.MessageNotFound, + message: "Cannot find the message in the database." + }); const owner = message.userId === user.id; - if (!owner) return revert( - Wss.RevertErrorCode.NotOwner, - "The user is not the sender/owner of the message to be updated." - ); - if (!text) return revert( - Wss.RevertErrorCode.EmptyText, - "Cannot send an empty message." - ); + if (!owner) return this.must(callback, { + code: Wss.AcknowledgeCodes.NotOwner, + message: "The user is not the sender/owner of the message to be updated." + }); + if (!text) return this.must(callback, { + code: Wss.AcknowledgeCodes.EmptyText, + message: "Cannot send an empty message." + }); const updated = await messages.update(id, { text }); - if (!updated) return revert( - Wss.RevertErrorCode.Unreachable, - "Something went wrong! This needs to be debugged in the backend." - ); + if (!updated) return this.must(callback, { + code: Wss.AcknowledgeCodes.Unreachable, + message: "Something went wrong! This needs to be debugged in the backend." + }); this.broadcast( Wss.ServerEvent.RoomMessageUpdated, asChatRoomId(message.roomId), - updated + updated, + () => {} ); }); if (error instanceof Error) stdout.error(error.message); } - async onDeleteMessage(data: unknown) { + async onDeleteMessage(data: unknown, callback?: Wss.AcknowledgeCallback) { const error = await safe(async () => { const user = this.user; if (isGhost(user)) return; const { id } = deleteMessagePayload.parse(data); - const revert = (code: Wss.RevertErrorCode, reason: string) => - this.revert({ type: "delete-message", id, reason, code }); - const message = await messages.findById(id); - if (!message || message.deleted) return revert( - Wss.RevertErrorCode.MessageNotFound, - "Message not found; perhabs it's already deleted." - ); + if (!message || message.deleted) + return this.must(callback, { + code: Wss.AcknowledgeCodes.MessageNotFound, + message: "Message not found; perhabs it's already deleted." + }); const owner = message.userId === user.id; - if (!owner) return revert( - Wss.RevertErrorCode.NotOwner, - "The user is not the sender/owner of the message to be deleted." - ); + if (!owner) return this.must(callback, { + code: Wss.AcknowledgeCodes.NotOwner, + message: "The user is not the sender/owner of the message to be deleted." + }); await messages.markAsDeleted(id); @@ -159,19 +157,17 @@ export class Messages extends WssHandler { { roomId: message.roomId, messageId: message.id, - } + }, + () => {} ); }); if (error instanceof Error) stdout.error(error.message); } - async onUserTyping(data: unknown) { + async onUserTyping(data: unknown, callback?: Wss.AcknowledgeCallback) { const error = await safe(async () => { const { roomId } = userTypingPayload.parse(data); - const revert = (code: Wss.RevertErrorCode, reason: string) => - this.revert({ type: "user-typing", roomId, reason, code }); - const user = this.user; if (isGhost(user)) return; @@ -179,10 +175,10 @@ export class Messages extends WssHandler { if (isEmpty(members)) return; const isMember = members.find((member) => member.id === user.id); - if (!isMember) return revert( - Wss.RevertErrorCode.NotMember, - "The user is not a member of the room!" - ); + if (!isMember) return this.must(callback, { + code: Wss.AcknowledgeCodes.NotMember, + message: "The user is not a member of the room!" + }); this.socket.to(asChatRoomId(roomId)).emit(Wss.ServerEvent.UserTyping, { roomId, @@ -193,37 +189,34 @@ export class Messages extends WssHandler { if (error instanceof Error) stdout.error(error.message); } - async onMarkMessageAsRead(data: unknown) { + async onMarkMessageAsRead(data: unknown, callback?: Wss.AcknowledgeCallback) { const error = await safe(async () => { const user = this.user; if (isGhost(user)) return; const { id } = markMessageAsReadPayload.parse(data); - const revert = (code: Wss.RevertErrorCode, reason: string) => - this.revert({ type: "mark-msg-as-read", id, reason, code }); - const message = await messages.findById(id); if (!message || message.deleted) - return revert( - Wss.RevertErrorCode.MessageNotFound, - "Message is not found in the database. I might be deleted." - ); + return this.must(callback, { + code: Wss.AcknowledgeCodes.MessageNotFound, + message: "Message is not found in the database. I might be deleted." + }); const members = await rooms.findRoomMembers({ roomIds: [message.roomId], }); const isMember = members.find((member) => member.id === user.id); - if (!isMember) return revert( - Wss.RevertErrorCode.NotMember, - "The user is not a member of the room!" - ); + if (!isMember) return this.must(callback, { + code: Wss.AcknowledgeCodes.NotMember, + message: "The user is not a member of the room!" + }); if (user.id === message.userId) - return revert( - Wss.RevertErrorCode.Unallowed, - "The user cannot mark his own message as read." - ); + return this.must(callback, { + code: Wss.AcknowledgeCodes.Unallowed, + message: "The user cannot mark his own message as read." + }); await messages.markAsRead(id); diff --git a/services/server/src/wss/handlers/peer.ts b/services/server/src/wss/handlers/peer.ts index 283d29c8..6e15a0c6 100644 --- a/services/server/src/wss/handlers/peer.ts +++ b/services/server/src/wss/handlers/peer.ts @@ -34,7 +34,11 @@ export class Peer extends WssHandler { this.socket.join(callId.toString()); this.socket .to(callId.toString()) - .emit(Wss.ServerEvent.UserJoinedCall, { peerId }); + .emit( + Wss.ServerEvent.UserJoinedCall, + { peerId }, + () => {} + ); }); if (error instanceof Error) stdout.error(error.message); } diff --git a/services/server/tests/wss/messages.test.ts b/services/server/tests/wss/messages.test.ts index 0f56a5a9..8cec9431 100644 --- a/services/server/tests/wss/messages.test.ts +++ b/services/server/tests/wss/messages.test.ts @@ -17,10 +17,9 @@ describe("wss message test suite", () => { const tutor = await tutorApi.findCurrentUser(); const tutorSocket = new ClientSocket(tutor.token); - tutorSocket.sendMessage(123, 1, "The lesson will start soon."); + const res = await tutorSocket.sendMessage(123, "The lesson will start soon."); - const res = await tutorSocket.wait(Wss.ServerEvent.Revert); - expect(res.type).to.eq("send-message"); + expect(res.code).to.eq(Wss.AcknowledgeCodes.RoomNotFound); }); it("should throw forbidden error if the sender is not a member.", async () => { @@ -33,10 +32,9 @@ describe("wss message test suite", () => { const student = await studentApi.findCurrentUser(); const studentSocket = new ClientSocket(student.token); - studentSocket.sendMessage(roomId, 1, "Hello."); + const res = await studentSocket.sendMessage(roomId, "Hello."); - const res = await studentSocket.wait(Wss.ServerEvent.Revert); - expect(res.type).to.eq("send-message"); + expect(res.code).to.eq(Wss.AcknowledgeCodes.NotMember); }); it("should successfully add message to the database.", async () => { @@ -49,10 +47,8 @@ describe("wss message test suite", () => { const roomId = await rooms.create([tutor.user.id, student.user.id]); const studentSocket = new ClientSocket(student.token); - studentSocket.sendMessage(roomId, 1, "Hello."); - - // wait for message to be saved in db - await studentSocket.wait(Wss.ServerEvent.RoomMessage); + const res = await studentSocket.sendMessage(roomId, "Hello."); + expect(res.code).to.eq(Wss.AcknowledgeCodes.Ok); const msgs = await messages.findRoomMessages({ room: roomId }); expect(msgs.total).to.eq(1); @@ -71,7 +67,7 @@ describe("wss message test suite", () => { const tutorSocket = new ClientSocket(tutor.token); const studentSocket = new ClientSocket(student.token); - tutorSocket.sendMessage(roomId, 1, "Lesson will start soon."); + tutorSocket.sendMessage(roomId, "Lesson will start soon."); const res = await studentSocket.wait(Wss.ServerEvent.RoomMessage); expect(res.text).to.eq("Lesson will start soon."); @@ -82,10 +78,9 @@ describe("wss message test suite", () => { const student = await studentApi.findCurrentUser(); const studentSocket = new ClientSocket(student.token); - studentSocket.sendMessage(123, 1, "Hello."); + const res = await studentSocket.sendMessage(123, "Hello."); - const res = await studentSocket.wait(Wss.ServerEvent.Revert); - expect(res.code).to.eq(Wss.RevertErrorCode.RoomNotFound); + expect(res.code).to.eq(Wss.AcknowledgeCodes.RoomNotFound); }); }); @@ -96,10 +91,8 @@ describe("wss message test suite", () => { const tutor = await tutorApi.findCurrentUser() const tutorSocket = new ClientSocket(tutor.token); - tutorSocket.deleteMessage(123); - const res = await tutorSocket.wait(Wss.ServerEvent.Revert); - - expect(res.code).to.eq(Wss.RevertErrorCode.MessageNotFound); + const res = await tutorSocket.deleteMessage(123); + expect(res.code).to.eq(Wss.AcknowledgeCodes.MessageNotFound); }); it("should return not found if the message is already deleted.", async () => { @@ -108,7 +101,7 @@ describe("wss message test suite", () => { const tutorSocket = new ClientSocket(tutor.token); const roomId = await rooms.create([tutor.user.id]); - tutorSocket.sendMessage(roomId, 1, "Lesson will start soon."); + tutorSocket.sendMessage(roomId, "Lesson will start soon."); // wait for message to be saved in db const msg = await tutorSocket.wait(Wss.ServerEvent.RoomMessage); @@ -117,10 +110,8 @@ describe("wss message test suite", () => { await tutorSocket.wait(Wss.ServerEvent.RoomMessageDeleted); // second deletion - tutorSocket.deleteMessage(msg.id); - const res = await tutorSocket.wait(Wss.ServerEvent.Revert); - - expect(res.code).to.eq(Wss.RevertErrorCode.MessageNotFound); + const res = await tutorSocket.deleteMessage(msg.id); + expect(res.code).to.eq(Wss.AcknowledgeCodes.MessageNotFound); }); it("should return forbidden if the user is not the owner.", async () => { @@ -129,7 +120,7 @@ describe("wss message test suite", () => { const tutorSocket = new ClientSocket(tutor.token); const roomId = await rooms.create([tutor.user.id]); - tutorSocket.sendMessage(roomId, 1, "Lesson will start soon."); + tutorSocket.sendMessage(roomId, "Lesson will start soon."); // wait for message to be saved in db const msg = await tutorSocket.wait(Wss.ServerEvent.RoomMessage); @@ -137,10 +128,8 @@ describe("wss message test suite", () => { const secTutor = await secTutorApi.findCurrentUser() const secTutorSocket = new ClientSocket(secTutor.token); - secTutorSocket.deleteMessage(msg.id); - const res = await secTutorSocket.wait(Wss.ServerEvent.Revert); - - expect(res.code).to.eq(Wss.RevertErrorCode.NotOwner); + const res = await secTutorSocket.deleteMessage(msg.id); + expect(res.code).to.eq(Wss.AcknowledgeCodes.NotOwner); }); it("should successfully delete the message (mark as deleted).", async () => { @@ -149,7 +138,7 @@ describe("wss message test suite", () => { const tutorSocket = new ClientSocket(tutor.token); const roomId = await rooms.create([tutor.user.id]); - tutorSocket.sendMessage(roomId, 1, "Lesson will start soon."); + tutorSocket.sendMessage(roomId, "Lesson will start soon."); // wait for message to be saved in db const msg = await tutorSocket.wait(Wss.ServerEvent.RoomMessage); @@ -173,10 +162,9 @@ describe("wss message test suite", () => { const student = await studentApi.findCurrentUser(); const studentSocket = new ClientSocket(student.token); - studentSocket.markMessageAsRead(123) - - const res = await studentSocket.wait(Wss.ServerEvent.Revert) - expect(res.code).to.eq(Wss.RevertErrorCode.MessageNotFound); + + const res = await studentSocket.markMessageAsRead(123); + expect(res.code).to.eq(Wss.AcknowledgeCodes.MessageNotFound); }); it("should throw not found error if the message is deleted.", async () => { @@ -191,14 +179,13 @@ describe("wss message test suite", () => { const tutorSocket = new ClientSocket(tutor.token); const studentSocket = new ClientSocket(student.token); - tutorSocket.sendMessage(roomId, 1, "Lesson will start soon."); + tutorSocket.sendMessage(roomId, "Lesson will start soon."); // wait for message to be saved in db const msg = await tutorSocket.wait(Wss.ServerEvent.RoomMessage); await messages.markAsDeleted(msg.id); - studentSocket.markMessageAsRead(msg.id) - const res = await studentSocket.wait(Wss.ServerEvent.Revert); - expect(res.code).to.eq(Wss.RevertErrorCode.MessageNotFound); + const res = await studentSocket.markMessageAsRead(msg.id); + expect(res.code).to.eq(Wss.AcknowledgeCodes.MessageNotFound); }); it("should throw forbidden error if the reader is not a member of the messages room.", async () => { @@ -213,15 +200,13 @@ describe("wss message test suite", () => { const tutorSocket = new ClientSocket(tutor.token); const studentSocket = new ClientSocket(student.token); - tutorSocket.sendMessage(roomId, 1, "Lesson will start soon."); + tutorSocket.sendMessage(roomId, "Lesson will start soon."); // wait for message to be saved in db const msg = await tutorSocket.wait(Wss.ServerEvent.RoomMessage); - studentSocket.markMessageAsRead(msg.id) - const res = await studentSocket.wait(Wss.ServerEvent.Revert); - - expect(res.code).to.eq(Wss.RevertErrorCode.NotMember); + const res = await studentSocket.markMessageAsRead(msg.id); + expect(res.code).to.eq(Wss.AcknowledgeCodes.NotMember); }); it("should NOT mark read by the owner.", async () => { @@ -230,14 +215,13 @@ describe("wss message test suite", () => { const tutorSocket = new ClientSocket(tutor.token); const roomId = await rooms.create([tutor.user.id]); - tutorSocket.sendMessage(roomId, 1, "Lesson will start soon."); + tutorSocket.sendMessage(roomId, "Lesson will start soon."); // wait for message to be saved in db const msg = await tutorSocket.wait(Wss.ServerEvent.RoomMessage); - tutorSocket.markMessageAsRead(msg.id) - const res = await tutorSocket.wait(Wss.ServerEvent.Revert); - expect(res.code).to.eq(Wss.RevertErrorCode.Unallowed); + const res = await tutorSocket.markMessageAsRead(msg.id); + expect(res.code).to.eq(Wss.AcknowledgeCodes.Unallowed); }); it("should successfully mark message as read in the database.", async () => { @@ -252,7 +236,7 @@ describe("wss message test suite", () => { const tutorSocket = new ClientSocket(tutor.token); const studentSocket = new ClientSocket(student.token); - tutorSocket.sendMessage(roomId, 1, "Lesson will start soon."); + tutorSocket.sendMessage(roomId, "Lesson will start soon."); // wait for message to be saved in db const msg = await tutorSocket.wait(Wss.ServerEvent.RoomMessage);