2020-08-25 19:31:34 -07:00
|
|
|
import {
|
2020-08-26 15:29:22 -07:00
|
|
|
CHATS_FETCH_SUCCESS,
|
2020-08-25 19:31:34 -07:00
|
|
|
CHAT_MESSAGES_FETCH_SUCCESS,
|
|
|
|
CHAT_MESSAGE_SEND_SUCCESS,
|
|
|
|
} from 'soapbox/actions/chats';
|
2020-08-26 15:29:22 -07:00
|
|
|
import { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming';
|
2020-08-26 13:54:14 -07:00
|
|
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
2020-08-25 18:33:49 -07:00
|
|
|
|
|
|
|
const initialState = ImmutableMap();
|
|
|
|
|
2020-08-26 11:39:38 -07:00
|
|
|
const importMessage = (state, message) => {
|
2020-08-26 13:54:14 -07:00
|
|
|
return state.set(message.get('id'), message);
|
2020-08-26 11:39:38 -07:00
|
|
|
};
|
|
|
|
|
2020-08-26 13:54:14 -07:00
|
|
|
const importMessages = (state, messages) =>
|
|
|
|
state.withMutations(mutable =>
|
|
|
|
messages.forEach(message => importMessage(mutable, message)));
|
|
|
|
|
2020-08-26 11:39:38 -07:00
|
|
|
const importLastMessages = (state, chats) =>
|
|
|
|
state.withMutations(mutable =>
|
|
|
|
chats.forEach(chat => importMessage(mutable, chat.get('last_message'))));
|
|
|
|
|
2020-08-25 18:33:49 -07:00
|
|
|
export default function chatMessages(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
2020-08-26 15:29:22 -07:00
|
|
|
case CHATS_FETCH_SUCCESS:
|
2020-08-26 17:53:54 -07:00
|
|
|
return importLastMessages(state, fromJS(action.chats));
|
2020-08-25 18:33:49 -07:00
|
|
|
case CHAT_MESSAGES_FETCH_SUCCESS:
|
2020-08-26 17:53:54 -07:00
|
|
|
return importMessages(state, fromJS(action.chatMessages));
|
2020-08-26 13:54:14 -07:00
|
|
|
case CHAT_MESSAGE_SEND_SUCCESS:
|
2020-08-26 17:53:54 -07:00
|
|
|
return importMessage(state, fromJS(action.chatMessage));
|
2020-08-26 15:29:22 -07:00
|
|
|
case STREAMING_CHAT_UPDATE:
|
2020-08-26 17:53:54 -07:00
|
|
|
return importLastMessages(state, fromJS([action.chat]));
|
2020-08-25 18:33:49 -07:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|