From da6239c4fcacc01e00862ccd086c52f023e368c6 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 26 Aug 2020 18:17:47 -0500 Subject: [PATCH] Chats: move out of importer pipeline, entirely through reducers --- app/soapbox/actions/chats.js | 2 -- app/soapbox/actions/importer/index.js | 37 ---------------------- app/soapbox/reducers/accounts.js | 15 +++++++++ app/soapbox/reducers/chat_message_lists.js | 7 ++++ app/soapbox/reducers/chats.js | 12 ++++--- 5 files changed, 30 insertions(+), 43 deletions(-) diff --git a/app/soapbox/actions/chats.js b/app/soapbox/actions/chats.js index 0391a6e08..e0eb762f4 100644 --- a/app/soapbox/actions/chats.js +++ b/app/soapbox/actions/chats.js @@ -1,5 +1,4 @@ import api from '../api'; -import { importFetchedChats } from 'soapbox/actions/importer'; import { getSettings, changeSetting } from 'soapbox/actions/settings'; import { Map as ImmutableMap } from 'immutable'; @@ -19,7 +18,6 @@ export function fetchChats() { return (dispatch, getState) => { dispatch({ type: CHATS_FETCH_REQUEST }); return api(getState).get('/api/v1/pleroma/chats').then(({ data }) => { - dispatch(importFetchedChats(data)); dispatch({ type: CHATS_FETCH_SUCCESS, data }); }).catch(error => { dispatch({ type: CHATS_FETCH_FAIL, error }); diff --git a/app/soapbox/actions/importer/index.js b/app/soapbox/actions/importer/index.js index 7989b3ebe..0736dd7ce 100644 --- a/app/soapbox/actions/importer/index.js +++ b/app/soapbox/actions/importer/index.js @@ -3,7 +3,6 @@ import { normalizeAccount, normalizeStatus, normalizePoll, - normalizeChat, } from './normalizer'; export const ACCOUNT_IMPORT = 'ACCOUNT_IMPORT'; @@ -12,8 +11,6 @@ export const STATUS_IMPORT = 'STATUS_IMPORT'; export const STATUSES_IMPORT = 'STATUSES_IMPORT'; export const POLLS_IMPORT = 'POLLS_IMPORT'; export const ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP = 'ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP'; -export const CHATS_IMPORT = 'CHATS_IMPORT'; -export const CHAT_MESSAGES_IMPORT = 'CHAT_MESSAGES_IMPORT'; function pushUnique(array, object) { if (array.every(element => element.id !== object.id)) { @@ -41,14 +38,6 @@ export function importPolls(polls) { return { type: POLLS_IMPORT, polls }; } -export function importChats(chats) { - return { type: CHATS_IMPORT, chats }; -} - -export function importChatMessages(chatMessages) { - return { type: CHAT_MESSAGES_IMPORT, chatMessages }; -} - export function importFetchedAccount(account) { return importFetchedAccounts([account]); } @@ -112,29 +101,3 @@ export function importFetchedPoll(poll) { export function importErrorWhileFetchingAccountByUsername(username) { return { type: ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP, username }; }; - -export function importFetchedChat(chat) { - return importFetchedChats([chat]); -} - -export function importFetchedChats(chats) { - return (dispatch, getState) => { - const accounts = []; - const chatMessages = []; - const normalChats = []; - - function processChat(chat) { - const normalOldChat = getState().getIn(['chats', chat.id]); - - pushUnique(normalChats, normalizeChat(chat, normalOldChat)); - pushUnique(accounts, chat.account); - pushUnique(chatMessages, chat.last_message); - } - - chats.forEach(processChat); - - dispatch(importFetchedAccounts(accounts)); - dispatch(importChatMessages(chatMessages)); - dispatch(importChats(normalChats)); - }; -} diff --git a/app/soapbox/reducers/accounts.js b/app/soapbox/reducers/accounts.js index fd915d7ff..64cf96515 100644 --- a/app/soapbox/reducers/accounts.js +++ b/app/soapbox/reducers/accounts.js @@ -3,6 +3,9 @@ import { ACCOUNTS_IMPORT, ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP, } from '../actions/importer'; +import { CHATS_FETCH_SUCCESS } from 'soapbox/actions/chats'; +import { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming'; +import { normalizeAccount as normalizeAccount2 } from 'soapbox/actions/importer/normalizer'; import { Map as ImmutableMap, fromJS } from 'immutable'; const initialState = ImmutableMap(); @@ -25,6 +28,14 @@ const normalizeAccounts = (state, accounts) => { return state; }; +const importAccountFromChat = (state, chat) => + // TODO: Fix this monstrosity + normalizeAccount(state, normalizeAccount2(chat.account)); + +const importAccountsFromChats = (state, chats) => + state.withMutations(mutable => + chats.forEach(chat => importAccountFromChat(mutable, chat))); + export default function accounts(state = initialState, action) { switch(action.type) { case ACCOUNT_IMPORT: @@ -35,6 +46,10 @@ export default function accounts(state = initialState, action) { return state.set(-1, ImmutableMap({ username: action.username, })); + case CHATS_FETCH_SUCCESS: + return importAccountsFromChats(state, action.data); + case STREAMING_CHAT_UPDATE: + return importAccountsFromChats(state, [action.payload]); default: return state; } diff --git a/app/soapbox/reducers/chat_message_lists.js b/app/soapbox/reducers/chat_message_lists.js index e19ee2bdd..535499a16 100644 --- a/app/soapbox/reducers/chat_message_lists.js +++ b/app/soapbox/reducers/chat_message_lists.js @@ -1,4 +1,5 @@ import { + CHATS_FETCH_SUCCESS, CHAT_MESSAGES_FETCH_SUCCESS, CHAT_MESSAGE_SEND_SUCCESS, } from 'soapbox/actions/chats'; @@ -23,8 +24,14 @@ const importMessages = (state, chatMessages) => ( importMessage(map, chatMessage))) ); +const importLastMessages = (state, chats) => + state.withMutations(mutable => + chats.forEach(chat => importMessage(mutable, chat.last_message))); + export default function chatMessageLists(state = initialState, action) { switch(action.type) { + case CHATS_FETCH_SUCCESS: + return importLastMessages(state, action.data); case STREAMING_CHAT_UPDATE: return importMessages(state, [action.payload.last_message]); case CHAT_MESSAGES_FETCH_SUCCESS: diff --git a/app/soapbox/reducers/chats.js b/app/soapbox/reducers/chats.js index 2930af578..912a47c06 100644 --- a/app/soapbox/reducers/chats.js +++ b/app/soapbox/reducers/chats.js @@ -1,7 +1,9 @@ -import { CHATS_IMPORT } from 'soapbox/actions/importer'; +import { CHATS_FETCH_SUCCESS } from 'soapbox/actions/chats'; +import { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming'; +import { normalizeChat } from 'soapbox/actions/importer/normalizer'; import { Map as ImmutableMap, fromJS } from 'immutable'; -const importChat = (state, chat) => state.set(chat.id, fromJS(chat)); +const importChat = (state, chat) => state.set(chat.id, fromJS(normalizeChat(chat))); const importChats = (state, chats) => state.withMutations(mutable => chats.forEach(chat => importChat(mutable, chat))); @@ -10,8 +12,10 @@ const initialState = ImmutableMap(); export default function chats(state = initialState, action) { switch(action.type) { - case CHATS_IMPORT: - return importChats(state, action.chats); + case CHATS_FETCH_SUCCESS: + return importChats(state, action.data); + case STREAMING_CHAT_UPDATE: + return importChats(state, [action.payload]); default: return state; }