From dbafbbc06557f8ac8e2e75791e340169d115e91e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 26 Aug 2020 13:39:38 -0500 Subject: [PATCH] Chats: make streaming mostly work --- app/soapbox/actions/streaming.js | 4 ++++ app/soapbox/reducers/chat_messages.js | 26 +++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/soapbox/actions/streaming.js b/app/soapbox/actions/streaming.js index 099cbab69..2b7836666 100644 --- a/app/soapbox/actions/streaming.js +++ b/app/soapbox/actions/streaming.js @@ -9,6 +9,7 @@ import { import { updateNotificationsQueue, expandNotifications } from './notifications'; import { updateConversations } from './conversations'; import { fetchFilters } from './filters'; +import { importFetchedChat } from './importer'; import { getSettings } from 'soapbox/actions/settings'; import messages from 'soapbox/locales/messages'; @@ -52,6 +53,9 @@ export function connectTimelineStream(timelineId, path, pollingRefresh = null, a case 'filters_changed': dispatch(fetchFilters()); break; + case 'pleroma:chat_update': + dispatch(importFetchedChat(JSON.parse(data.payload))); + break; } }, }; diff --git a/app/soapbox/reducers/chat_messages.js b/app/soapbox/reducers/chat_messages.js index 7eefb645a..b79ab2820 100644 --- a/app/soapbox/reducers/chat_messages.js +++ b/app/soapbox/reducers/chat_messages.js @@ -2,16 +2,36 @@ import { CHAT_MESSAGES_FETCH_SUCCESS, CHAT_MESSAGE_SEND_SUCCESS, } from 'soapbox/actions/chats'; -import { Map as ImmutableMap, fromJS } from 'immutable'; +import { CHAT_IMPORT, CHATS_IMPORT } from 'soapbox/actions/importer'; +import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable'; const initialState = ImmutableMap(); +const insertMessage = (state, chatId, message) => { + const newMessages = state.get(chatId, ImmutableList()).insert(0, message); + return state.set(chatId, newMessages); +}; + +const importMessage = (state, message) => { + const chatId = message.get('chat_id'); + return insertMessage(state, chatId, message); +}; + +const importLastMessages = (state, chats) => + state.withMutations(mutable => + chats.forEach(chat => importMessage(mutable, chat.get('last_message')))); + export default function chatMessages(state = initialState, action) { switch(action.type) { + case CHAT_IMPORT: + return importMessage(state, fromJS(action.chat.last_message)); + case CHATS_IMPORT: + return importLastMessages(state, fromJS(action.chats)); case CHAT_MESSAGES_FETCH_SUCCESS: return state.set(action.chatId, fromJS(action.data)); - case CHAT_MESSAGE_SEND_SUCCESS: - return state.set(action.chatId, state.get(action.chatId).insert(0, fromJS(action.data))); + // TODO: Prevent conflicts + // case CHAT_MESSAGE_SEND_SUCCESS: + // return insertMessage(state, action.chatId, fromJS(action.data)); default: return state; }