Chats: move out of importer pipeline, entirely through reducers
This commit is contained in:
parent
b9d7f927a6
commit
da6239c4fc
5 changed files with 30 additions and 43 deletions
|
@ -1,5 +1,4 @@
|
||||||
import api from '../api';
|
import api from '../api';
|
||||||
import { importFetchedChats } from 'soapbox/actions/importer';
|
|
||||||
import { getSettings, changeSetting } from 'soapbox/actions/settings';
|
import { getSettings, changeSetting } from 'soapbox/actions/settings';
|
||||||
import { Map as ImmutableMap } from 'immutable';
|
import { Map as ImmutableMap } from 'immutable';
|
||||||
|
|
||||||
|
@ -19,7 +18,6 @@ export function fetchChats() {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
dispatch({ type: CHATS_FETCH_REQUEST });
|
dispatch({ type: CHATS_FETCH_REQUEST });
|
||||||
return api(getState).get('/api/v1/pleroma/chats').then(({ data }) => {
|
return api(getState).get('/api/v1/pleroma/chats').then(({ data }) => {
|
||||||
dispatch(importFetchedChats(data));
|
|
||||||
dispatch({ type: CHATS_FETCH_SUCCESS, data });
|
dispatch({ type: CHATS_FETCH_SUCCESS, data });
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
dispatch({ type: CHATS_FETCH_FAIL, error });
|
dispatch({ type: CHATS_FETCH_FAIL, error });
|
||||||
|
|
|
@ -3,7 +3,6 @@ import {
|
||||||
normalizeAccount,
|
normalizeAccount,
|
||||||
normalizeStatus,
|
normalizeStatus,
|
||||||
normalizePoll,
|
normalizePoll,
|
||||||
normalizeChat,
|
|
||||||
} from './normalizer';
|
} from './normalizer';
|
||||||
|
|
||||||
export const ACCOUNT_IMPORT = 'ACCOUNT_IMPORT';
|
export const ACCOUNT_IMPORT = 'ACCOUNT_IMPORT';
|
||||||
|
@ -12,8 +11,6 @@ export const STATUS_IMPORT = 'STATUS_IMPORT';
|
||||||
export const STATUSES_IMPORT = 'STATUSES_IMPORT';
|
export const STATUSES_IMPORT = 'STATUSES_IMPORT';
|
||||||
export const POLLS_IMPORT = 'POLLS_IMPORT';
|
export const POLLS_IMPORT = 'POLLS_IMPORT';
|
||||||
export const ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP = 'ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP';
|
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) {
|
function pushUnique(array, object) {
|
||||||
if (array.every(element => element.id !== object.id)) {
|
if (array.every(element => element.id !== object.id)) {
|
||||||
|
@ -41,14 +38,6 @@ export function importPolls(polls) {
|
||||||
return { type: POLLS_IMPORT, 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) {
|
export function importFetchedAccount(account) {
|
||||||
return importFetchedAccounts([account]);
|
return importFetchedAccounts([account]);
|
||||||
}
|
}
|
||||||
|
@ -112,29 +101,3 @@ export function importFetchedPoll(poll) {
|
||||||
export function importErrorWhileFetchingAccountByUsername(username) {
|
export function importErrorWhileFetchingAccountByUsername(username) {
|
||||||
return { type: ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP, 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));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,6 +3,9 @@ import {
|
||||||
ACCOUNTS_IMPORT,
|
ACCOUNTS_IMPORT,
|
||||||
ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP,
|
ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP,
|
||||||
} from '../actions/importer';
|
} 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';
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
||||||
|
|
||||||
const initialState = ImmutableMap();
|
const initialState = ImmutableMap();
|
||||||
|
@ -25,6 +28,14 @@ const normalizeAccounts = (state, accounts) => {
|
||||||
return state;
|
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) {
|
export default function accounts(state = initialState, action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case ACCOUNT_IMPORT:
|
case ACCOUNT_IMPORT:
|
||||||
|
@ -35,6 +46,10 @@ export default function accounts(state = initialState, action) {
|
||||||
return state.set(-1, ImmutableMap({
|
return state.set(-1, ImmutableMap({
|
||||||
username: action.username,
|
username: action.username,
|
||||||
}));
|
}));
|
||||||
|
case CHATS_FETCH_SUCCESS:
|
||||||
|
return importAccountsFromChats(state, action.data);
|
||||||
|
case STREAMING_CHAT_UPDATE:
|
||||||
|
return importAccountsFromChats(state, [action.payload]);
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import {
|
import {
|
||||||
|
CHATS_FETCH_SUCCESS,
|
||||||
CHAT_MESSAGES_FETCH_SUCCESS,
|
CHAT_MESSAGES_FETCH_SUCCESS,
|
||||||
CHAT_MESSAGE_SEND_SUCCESS,
|
CHAT_MESSAGE_SEND_SUCCESS,
|
||||||
} from 'soapbox/actions/chats';
|
} from 'soapbox/actions/chats';
|
||||||
|
@ -23,8 +24,14 @@ const importMessages = (state, chatMessages) => (
|
||||||
importMessage(map, chatMessage)))
|
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) {
|
export default function chatMessageLists(state = initialState, action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
|
case CHATS_FETCH_SUCCESS:
|
||||||
|
return importLastMessages(state, action.data);
|
||||||
case STREAMING_CHAT_UPDATE:
|
case STREAMING_CHAT_UPDATE:
|
||||||
return importMessages(state, [action.payload.last_message]);
|
return importMessages(state, [action.payload.last_message]);
|
||||||
case CHAT_MESSAGES_FETCH_SUCCESS:
|
case CHAT_MESSAGES_FETCH_SUCCESS:
|
||||||
|
|
|
@ -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';
|
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) =>
|
const importChats = (state, chats) =>
|
||||||
state.withMutations(mutable => chats.forEach(chat => importChat(mutable, chat)));
|
state.withMutations(mutable => chats.forEach(chat => importChat(mutable, chat)));
|
||||||
|
@ -10,8 +12,10 @@ const initialState = ImmutableMap();
|
||||||
|
|
||||||
export default function chats(state = initialState, action) {
|
export default function chats(state = initialState, action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case CHATS_IMPORT:
|
case CHATS_FETCH_SUCCESS:
|
||||||
return importChats(state, action.chats);
|
return importChats(state, action.data);
|
||||||
|
case STREAMING_CHAT_UPDATE:
|
||||||
|
return importChats(state, [action.payload]);
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue