mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2025-12-05 20:39:59 -08:00
Adding remove messages to persistence layer (#5066)
This commit is contained in:
@@ -49,5 +49,12 @@ export const Actions = {
|
||||
roomId,
|
||||
field,
|
||||
order
|
||||
})
|
||||
}),
|
||||
|
||||
removeMessages: (roomId, name, amount) => ({
|
||||
type: Types.REMOVE_MESSAGES,
|
||||
roomId,
|
||||
name,
|
||||
amount
|
||||
}),
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user