Chats: Import and normalize chats

loading-indicator-on-tls^2
Alex Gleason 2020-08-25 12:38:21 -05:00
rodzic 7693fb87cc
commit b98f06e3d3
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
6 zmienionych plików z 84 dodań i 10 usunięć

Wyświetl plik

@ -1,4 +1,5 @@
import api from '../api'; import api from '../api';
import { importFetchedChats } from 'soapbox/actions/importer';
export const CHATS_FETCH_REQUEST = 'CHATS_FETCH_REQUEST'; export const CHATS_FETCH_REQUEST = 'CHATS_FETCH_REQUEST';
export const CHATS_FETCH_SUCCESS = 'CHATS_FETCH_SUCCESS'; export const CHATS_FETCH_SUCCESS = 'CHATS_FETCH_SUCCESS';
@ -8,6 +9,7 @@ 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 });

Wyświetl plik

@ -1,5 +1,10 @@
import { getSettings } from '../settings'; import { getSettings } from '../settings';
import { normalizeAccount, normalizeStatus, normalizePoll } from './normalizer'; import {
normalizeAccount,
normalizeStatus,
normalizePoll,
normalizeChat,
} from './normalizer';
export const ACCOUNT_IMPORT = 'ACCOUNT_IMPORT'; export const ACCOUNT_IMPORT = 'ACCOUNT_IMPORT';
export const ACCOUNTS_IMPORT = 'ACCOUNTS_IMPORT'; export const ACCOUNTS_IMPORT = 'ACCOUNTS_IMPORT';
@ -7,6 +12,8 @@ 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 CHAT_IMPORT = 'CHAT_IMPORT';
export const CHATS_IMPORT = 'CHATS_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)) {
@ -34,6 +41,14 @@ export function importPolls(polls) {
return { type: POLLS_IMPORT, polls }; return { type: POLLS_IMPORT, polls };
} }
export function importChat(chat) {
return { type: CHAT_IMPORT, chat };
}
export function importChats(chats) {
return { type: CHATS_IMPORT, chats };
}
export function importFetchedAccount(account) { export function importFetchedAccount(account) {
return importFetchedAccounts([account]); return importFetchedAccounts([account]);
} }
@ -97,3 +112,26 @@ 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 normalChats = [];
function processChat(chat) {
const normalOldChat = getState().getIn(['chats', chat.id]);
pushUnique(normalChats, normalizeChat(chat, normalOldChat));
pushUnique(accounts, chat.account);
}
chats.forEach(processChat);
dispatch(importFetchedAccounts(accounts));
dispatch(importChats(normalChats));
};
}

Wyświetl plik

@ -80,3 +80,11 @@ export function normalizePoll(poll) {
return normalPoll; return normalPoll;
} }
export function normalizeChat(chat, normalOldChat) {
const normalChat = { ...chat };
normalChat.account = chat.account.id;
return normalChat;
}

Wyświetl plik

@ -5,10 +5,14 @@ import { injectIntl, FormattedMessage } from 'react-intl';
import ImmutablePureComponent from 'react-immutable-pure-component'; import ImmutablePureComponent from 'react-immutable-pure-component';
import { fetchChats } from 'soapbox/actions/chats'; import { fetchChats } from 'soapbox/actions/chats';
import ChatListAccount from './chat_list_account'; import ChatListAccount from './chat_list_account';
import { makeGetChat } from 'soapbox/selectors';
const mapStateToProps = state => ({ const mapStateToProps = state => {
chats: state.get('chats'), const getChat = makeGetChat();
}); return {
chats: state.get('chats').map(chat => getChat(state, chat.toJS())),
};
};
export default @connect(mapStateToProps) export default @connect(mapStateToProps)
@injectIntl @injectIntl
@ -37,7 +41,7 @@ class ChatList extends ImmutablePureComponent {
</div> </div>
<div className='chat-list__content'> <div className='chat-list__content'>
{chats.toList().map(chat => ( {chats.toList().map(chat => (
<div className='chat-list-item'> <div key={chat.get('id')} className='chat-list-item'>
<ChatListAccount <ChatListAccount
account={chat.get('account')} account={chat.get('account')}
onClick={this.handleClickChat} onClick={this.handleClickChat}

Wyświetl plik

@ -1,14 +1,19 @@
import { CHATS_FETCH_SUCCESS } from '../actions/chats'; import { CHAT_IMPORT, CHATS_IMPORT } from 'soapbox/actions/importer';
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 importChats = (state, chats) =>
state.withMutations(mutable => chats.forEach(chat => importChat(mutable, chat)));
const initialState = ImmutableMap(); const initialState = ImmutableMap();
export default function admin(state = initialState, action) { export default function admin(state = initialState, action) {
switch(action.type) { switch(action.type) {
case CHATS_FETCH_SUCCESS: case CHAT_IMPORT:
return state.merge(fromJS(action.data).reduce((acc, curr) => ( return importChat(state, action.chat);
acc.set(curr.get('id'), curr) case CHATS_IMPORT:
), ImmutableMap())); return importChats(state, action.chats);
default: default:
return state; return state;
} }

Wyświetl plik

@ -157,3 +157,20 @@ export const getAccountGallery = createSelector([
.map(media => media.merge({ status, account }))); .map(media => media.merge({ status, account })));
}, ImmutableList()); }, ImmutableList());
}); });
export const makeGetChat = () => {
return createSelector(
[
(state, { id }) => state.getIn(['chats', id]),
(state, { id }) => state.getIn(['accounts', state.getIn(['chats', id, 'account'])]),
],
(chat, account) => {
if (!chat) return null;
return chat.withMutations(map => {
map.set('account', account);
});
}
);
};