2022-01-10 14:01:24 -08:00
|
|
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
2020-08-25 19:31:34 -07:00
|
|
|
import {
|
2020-08-26 15:29:22 -07:00
|
|
|
CHATS_FETCH_SUCCESS,
|
2021-12-12 11:45:59 -08:00
|
|
|
CHATS_EXPAND_SUCCESS,
|
2020-08-25 19:31:34 -07:00
|
|
|
CHAT_MESSAGES_FETCH_SUCCESS,
|
2020-08-26 22:06:27 -07:00
|
|
|
CHAT_MESSAGE_SEND_REQUEST,
|
2020-08-25 19:31:34 -07:00
|
|
|
CHAT_MESSAGE_SEND_SUCCESS,
|
2020-09-22 14:20:10 -07:00
|
|
|
CHAT_MESSAGE_DELETE_REQUEST,
|
|
|
|
CHAT_MESSAGE_DELETE_SUCCESS,
|
2020-08-25 19:31:34 -07:00
|
|
|
} from 'soapbox/actions/chats';
|
2020-08-26 15:29:22 -07:00
|
|
|
import { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming';
|
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 =>
|
2020-08-26 18:20:14 -07:00
|
|
|
chats.forEach(chat => {
|
|
|
|
if (chat.get('last_message'))
|
|
|
|
importMessage(mutable, chat.get('last_message'));
|
|
|
|
}));
|
2020-08-26 11:39:38 -07:00
|
|
|
|
2020-08-25 18:33:49 -07:00
|
|
|
export default function chatMessages(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
2020-08-26 22:06:27 -07:00
|
|
|
case CHAT_MESSAGE_SEND_REQUEST:
|
|
|
|
return importMessage(state, fromJS({
|
|
|
|
id: action.uuid, // Make fake message to get overriden later
|
|
|
|
chat_id: action.chatId,
|
|
|
|
account_id: action.me,
|
|
|
|
content: action.params.content,
|
2020-08-28 11:17:19 -07:00
|
|
|
created_at: (new Date()).toISOString(),
|
2020-08-26 22:06:27 -07:00
|
|
|
pending: true,
|
|
|
|
}));
|
2020-08-26 15:29:22 -07:00
|
|
|
case CHATS_FETCH_SUCCESS:
|
2021-12-12 11:45:59 -08:00
|
|
|
case CHATS_EXPAND_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 22:06:27 -07:00
|
|
|
return importMessage(state, fromJS(action.chatMessage)).delete(action.uuid);
|
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-09-22 14:20:10 -07:00
|
|
|
case CHAT_MESSAGE_DELETE_REQUEST:
|
|
|
|
return state.update(action.messageId, chatMessage =>
|
|
|
|
chatMessage.set('pending', true).set('deleting', true));
|
|
|
|
case CHAT_MESSAGE_DELETE_SUCCESS:
|
|
|
|
return state.delete(action.messageId);
|
2020-08-25 18:33:49 -07:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|