2020-08-26 13:54:14 -07:00
|
|
|
import {
|
|
|
|
CHAT_MESSAGES_FETCH_SUCCESS,
|
|
|
|
CHAT_MESSAGE_SEND_SUCCESS,
|
|
|
|
} from 'soapbox/actions/chats';
|
|
|
|
import { CHAT_MESSAGES_IMPORT } from 'soapbox/actions/importer';
|
|
|
|
import { Map as ImmutableMap, OrderedSet as ImmutableOrderedSet } from 'immutable';
|
|
|
|
|
|
|
|
const initialState = ImmutableMap();
|
|
|
|
|
|
|
|
const updateList = (state, chatId, messageIds) => {
|
2020-08-26 14:54:44 -07:00
|
|
|
const ids = state.get(chatId, ImmutableOrderedSet());
|
|
|
|
const newIds = ids.union(messageIds);
|
2020-08-26 13:54:14 -07:00
|
|
|
return state.set(chatId, newIds);
|
|
|
|
};
|
|
|
|
|
|
|
|
const importMessage = (state, chatMessage) => {
|
|
|
|
return updateList(state, chatMessage.chat_id, [chatMessage.id]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const importMessages = (state, chatMessages) => (
|
|
|
|
state.withMutations(map =>
|
|
|
|
chatMessages.forEach(chatMessage =>
|
|
|
|
importMessage(map, chatMessage)))
|
|
|
|
);
|
|
|
|
|
|
|
|
export default function chatMessageLists(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
|
|
|
case CHAT_MESSAGES_IMPORT:
|
|
|
|
return importMessages(state, action.chatMessages);
|
|
|
|
case CHAT_MESSAGES_FETCH_SUCCESS:
|
|
|
|
return updateList(state, action.chatId, action.data.map(chat => chat.id));
|
|
|
|
case CHAT_MESSAGE_SEND_SUCCESS:
|
2020-08-26 14:54:44 -07:00
|
|
|
return updateList(state, action.chatId, [action.data.id]);
|
2020-08-26 13:54:14 -07:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|