2022-01-10 14:01:24 -08:00
|
|
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2020-08-27 14:09:03 -07:00
|
|
|
import {
|
|
|
|
CHATS_FETCH_SUCCESS,
|
2021-12-12 11:45:59 -08:00
|
|
|
CHATS_FETCH_REQUEST,
|
|
|
|
CHATS_EXPAND_SUCCESS,
|
|
|
|
CHATS_EXPAND_REQUEST,
|
2020-08-27 14:09:03 -07:00
|
|
|
CHAT_FETCH_SUCCESS,
|
|
|
|
CHAT_READ_SUCCESS,
|
|
|
|
CHAT_READ_REQUEST,
|
|
|
|
} from 'soapbox/actions/chats';
|
2022-01-10 14:17:52 -08:00
|
|
|
import { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming';
|
2020-08-24 19:26:42 -07:00
|
|
|
|
2022-03-10 19:09:12 -08:00
|
|
|
const normalizeChat = (chat, normalOldChat) => {
|
|
|
|
const normalChat = { ...chat };
|
|
|
|
const { account, last_message: lastMessage } = chat;
|
|
|
|
|
|
|
|
if (account) normalChat.account = account.id;
|
|
|
|
if (lastMessage) normalChat.last_message = lastMessage.id;
|
|
|
|
|
|
|
|
return normalChat;
|
|
|
|
};
|
|
|
|
|
2021-12-12 11:45:59 -08:00
|
|
|
const importChat = (state, chat) => state.setIn(['items', chat.id], fromJS(normalizeChat(chat)));
|
2020-08-25 10:38:21 -07:00
|
|
|
|
2021-12-12 11:45:59 -08:00
|
|
|
const importChats = (state, chats, next) =>
|
|
|
|
state.withMutations(mutable => {
|
|
|
|
if (next !== undefined) mutable.set('next', next);
|
|
|
|
chats.forEach(chat => importChat(mutable, chat));
|
|
|
|
mutable.set('loading', false);
|
|
|
|
});
|
2020-08-25 10:38:21 -07:00
|
|
|
|
2021-12-12 11:45:59 -08:00
|
|
|
const initialState = ImmutableMap({
|
|
|
|
next: null,
|
|
|
|
isLoading: false,
|
|
|
|
items: ImmutableMap({}),
|
|
|
|
});
|
2020-08-24 19:26:42 -07:00
|
|
|
|
2020-08-25 18:33:49 -07:00
|
|
|
export default function chats(state = initialState, action) {
|
2020-08-24 19:26:42 -07:00
|
|
|
switch(action.type) {
|
2021-12-12 11:45:59 -08:00
|
|
|
case CHATS_FETCH_REQUEST:
|
|
|
|
case CHATS_EXPAND_REQUEST:
|
|
|
|
return state.set('loading', true);
|
2020-08-26 16:17:47 -07:00
|
|
|
case CHATS_FETCH_SUCCESS:
|
2021-12-12 11:45:59 -08:00
|
|
|
case CHATS_EXPAND_SUCCESS:
|
|
|
|
return importChats(state, action.chats, action.next);
|
2020-08-26 16:17:47 -07:00
|
|
|
case STREAMING_CHAT_UPDATE:
|
2020-08-27 13:07:15 -07:00
|
|
|
return importChats(state, [action.chat]);
|
2020-08-27 11:01:06 -07:00
|
|
|
case CHAT_FETCH_SUCCESS:
|
2020-08-27 13:07:15 -07:00
|
|
|
return importChats(state, [action.chat]);
|
2020-08-27 14:09:03 -07:00
|
|
|
case CHAT_READ_REQUEST:
|
|
|
|
return state.setIn([action.chatId, 'unread'], 0);
|
|
|
|
case CHAT_READ_SUCCESS:
|
|
|
|
return importChats(state, [action.chat]);
|
2020-08-24 19:26:42 -07:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|