bigbuffet-rw/app/soapbox/reducers/chats.js

34 lines
1 KiB
JavaScript
Raw Normal View History

2020-08-27 14:09:03 -07:00
import {
CHATS_FETCH_SUCCESS,
CHAT_FETCH_SUCCESS,
CHAT_READ_SUCCESS,
CHAT_READ_REQUEST,
} from 'soapbox/actions/chats';
import { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming';
import { normalizeChat } from 'soapbox/actions/importer/normalizer';
2020-08-24 19:26:42 -07:00
import { Map as ImmutableMap, fromJS } from 'immutable';
const importChat = (state, chat) => state.set(chat.id, fromJS(normalizeChat(chat)));
2020-08-25 10:38:21 -07:00
const importChats = (state, chats) =>
state.withMutations(mutable => chats.forEach(chat => importChat(mutable, chat)));
2020-08-24 19:26:42 -07:00
const initialState = ImmutableMap();
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) {
case CHATS_FETCH_SUCCESS:
2020-08-27 13:07:15 -07:00
return importChats(state, action.chats);
case STREAMING_CHAT_UPDATE:
2020-08-27 13:07:15 -07:00
return importChats(state, [action.chat]);
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
}