Adding remove messages to persistence layer (#5066)

This commit is contained in:
Joseph Insalaco
2024-06-26 20:44:40 -04:00
committed by GitHub
parent 8687163cca
commit f8bc6cf998
5 changed files with 54 additions and 4 deletions

View File

@@ -49,5 +49,12 @@ export const Actions = {
roomId,
field,
order
})
}),
removeMessages: (roomId, name, amount) => ({
type: Types.REMOVE_MESSAGES,
roomId,
name,
amount
}),
}

View File

@@ -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));
},
}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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) {