From f8bc6cf99833bc22e692689649cc0544f3818f12 Mon Sep 17 00:00:00 2001 From: Joseph Insalaco Date: Wed, 26 Jun 2024 20:44:40 -0400 Subject: [PATCH] Adding remove messages to persistence layer (#5066) --- webclient/src/store/rooms/rooms.actions.tsx | 9 ++++- webclient/src/store/rooms/rooms.dispatch.tsx | 6 ++- webclient/src/store/rooms/rooms.reducer.tsx | 38 +++++++++++++++++++ webclient/src/store/rooms/rooms.types.tsx | 3 +- .../websocket/persistence/RoomPersistence.ts | 2 +- 5 files changed, 54 insertions(+), 4 deletions(-) diff --git a/webclient/src/store/rooms/rooms.actions.tsx b/webclient/src/store/rooms/rooms.actions.tsx index 2b49ba419..c34083375 100644 --- a/webclient/src/store/rooms/rooms.actions.tsx +++ b/webclient/src/store/rooms/rooms.actions.tsx @@ -49,5 +49,12 @@ export const Actions = { roomId, field, order - }) + }), + + removeMessages: (roomId, name, amount) => ({ + type: Types.REMOVE_MESSAGES, + roomId, + name, + amount + }), } diff --git a/webclient/src/store/rooms/rooms.dispatch.tsx b/webclient/src/store/rooms/rooms.dispatch.tsx index 03ed34361..48b643f71 100644 --- a/webclient/src/store/rooms/rooms.dispatch.tsx +++ b/webclient/src/store/rooms/rooms.dispatch.tsx @@ -42,5 +42,9 @@ export const Dispatch = { sortGames: (roomId, field, order) => { store.dispatch(Actions.sortGames(roomId, field, order)); - } + }, + + removeMessages: (roomId, name, amount) => { + store.dispatch(Actions.removeMessages(roomId, name, amount)); + }, } diff --git a/webclient/src/store/rooms/rooms.reducer.tsx b/webclient/src/store/rooms/rooms.reducer.tsx index 7f62bbf0c..f47d608dc 100644 --- a/webclient/src/store/rooms/rooms.reducer.tsx +++ b/webclient/src/store/rooms/rooms.reducer.tsx @@ -28,6 +28,7 @@ export const roomsReducer = (state = initialState, action: any) => { ...initialState }; } + case Types.UPDATE_ROOMS: { const rooms = { ...state.rooms @@ -52,6 +53,7 @@ export const roomsReducer = (state = initialState, action: any) => { return { ...state, rooms }; } + case Types.JOIN_ROOM: { const { roomInfo } = action; const { joined, rooms, sortGamesBy, sortUsersBy } = state; @@ -87,6 +89,7 @@ export const roomsReducer = (state = initialState, action: any) => { }, } } + case Types.LEAVE_ROOM: { const { roomId } = action; const { joined, messages } = state; @@ -109,6 +112,7 @@ export const roomsReducer = (state = initialState, action: any) => { messages: _messages, } } + case Types.ADD_MESSAGE: { const { roomId, message } = action; const { messages } = state; @@ -134,6 +138,7 @@ export const roomsReducer = (state = initialState, action: any) => { } } // @TODO improve this reducer, likely by improving the store model + case Types.UPDATE_GAMES: { const { roomId, games } = action; const { rooms, sortGamesBy } = state; @@ -196,6 +201,7 @@ export const roomsReducer = (state = initialState, action: any) => { } } } + case Types.USER_JOINED: { const { roomId, user } = action; const { rooms, sortUsersBy } = state; @@ -220,6 +226,7 @@ export const roomsReducer = (state = initialState, action: any) => { } }; } + case Types.USER_LEFT: { const { roomId, name } = action; const { rooms } = state; @@ -238,6 +245,7 @@ export const roomsReducer = (state = initialState, action: any) => { } }; } + case Types.SORT_GAMES: { const { field, order, roomId } = action; const { rooms } = state; @@ -264,6 +272,36 @@ export const roomsReducer = (state = initialState, action: any) => { sortGamesBy } } + + case Types.REMOVE_MESSAGES: { + const { name, amount, roomId } = action; + const { messages } = state; + let amountRemoved = 0; + + return { + ...state, + messages: { + ...messages, + [roomId]: messages[roomId] + .reverse() + .filter(({ message }) => { + if (amount === amountRemoved) { + return true; + } + + const keep = message.indexOf(`${name}:`) !== 0; + + if (!keep) { + amountRemoved++; + } + + return keep; + }) + .reverse() + } + } + } + default: return state; } diff --git a/webclient/src/store/rooms/rooms.types.tsx b/webclient/src/store/rooms/rooms.types.tsx index e7c836359..892e1205f 100644 --- a/webclient/src/store/rooms/rooms.types.tsx +++ b/webclient/src/store/rooms/rooms.types.tsx @@ -7,7 +7,8 @@ export const Types = { UPDATE_GAMES: '[Rooms] Update Games', USER_JOINED: '[Rooms] User Joined', USER_LEFT: '[Rooms] User Left', - SORT_GAMES: '[Rooms] Sort Games' + SORT_GAMES: '[Rooms] Sort Games', + REMOVE_MESSAGES: '[Rooms] Remove Messages', }; export const MAX_ROOM_MESSAGES = 1000; diff --git a/webclient/src/websocket/persistence/RoomPersistence.ts b/webclient/src/websocket/persistence/RoomPersistence.ts index 9e664d9c5..3320b0c88 100644 --- a/webclient/src/websocket/persistence/RoomPersistence.ts +++ b/webclient/src/websocket/persistence/RoomPersistence.ts @@ -50,7 +50,7 @@ export class RoomPersistence { } static removeMessages(roomId: number, name: string, amount: number): void { - console.log('removeMessages', roomId, name, amount); + RoomsDispatch.removeMessages(roomId, name, amount); }; static gameCreated(roomId: number) {