2018-10-07 21:44:58 +00:00
|
|
|
import api, { getLinks } from '../api';
|
|
|
|
import {
|
|
|
|
importFetchedAccounts,
|
|
|
|
importFetchedStatuses,
|
|
|
|
importFetchedStatus,
|
|
|
|
} from './importer';
|
|
|
|
|
2018-10-10 23:31:03 +00:00
|
|
|
export const CONVERSATIONS_MOUNT = 'CONVERSATIONS_MOUNT';
|
|
|
|
export const CONVERSATIONS_UNMOUNT = 'CONVERSATIONS_UNMOUNT';
|
|
|
|
|
2018-10-07 21:44:58 +00:00
|
|
|
export const CONVERSATIONS_FETCH_REQUEST = 'CONVERSATIONS_FETCH_REQUEST';
|
|
|
|
export const CONVERSATIONS_FETCH_SUCCESS = 'CONVERSATIONS_FETCH_SUCCESS';
|
|
|
|
export const CONVERSATIONS_FETCH_FAIL = 'CONVERSATIONS_FETCH_FAIL';
|
|
|
|
export const CONVERSATIONS_UPDATE = 'CONVERSATIONS_UPDATE';
|
|
|
|
|
2018-10-18 23:47:29 +00:00
|
|
|
export const CONVERSATIONS_READ = 'CONVERSATIONS_READ';
|
|
|
|
|
2018-10-10 23:31:03 +00:00
|
|
|
export const mountConversations = () => ({
|
|
|
|
type: CONVERSATIONS_MOUNT,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const unmountConversations = () => ({
|
|
|
|
type: CONVERSATIONS_UNMOUNT,
|
|
|
|
});
|
|
|
|
|
2018-10-18 23:47:29 +00:00
|
|
|
export const markConversationRead = conversationId => (dispatch, getState) => {
|
|
|
|
dispatch({
|
|
|
|
type: CONVERSATIONS_READ,
|
|
|
|
id: conversationId,
|
|
|
|
});
|
|
|
|
|
|
|
|
api(getState).post(`/api/v1/conversations/${conversationId}/read`);
|
|
|
|
};
|
|
|
|
|
2018-10-07 21:44:58 +00:00
|
|
|
export const expandConversations = ({ maxId } = {}) => (dispatch, getState) => {
|
|
|
|
dispatch(expandConversationsRequest());
|
|
|
|
|
|
|
|
const params = { max_id: maxId };
|
|
|
|
|
|
|
|
if (!maxId) {
|
2018-12-01 17:36:41 +00:00
|
|
|
params.since_id = getState().getIn(['conversations', 'items', 0, 'last_status']);
|
2018-10-07 21:44:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 18:13:16 +00:00
|
|
|
const isLoadingRecent = !!params.since_id;
|
|
|
|
|
2018-10-07 21:44:58 +00:00
|
|
|
api(getState).get('/api/v1/conversations', { params })
|
|
|
|
.then(response => {
|
|
|
|
const next = getLinks(response).refs.find(link => link.rel === 'next');
|
|
|
|
|
|
|
|
dispatch(importFetchedAccounts(response.data.reduce((aggr, item) => aggr.concat(item.accounts), [])));
|
|
|
|
dispatch(importFetchedStatuses(response.data.map(item => item.last_status).filter(x => !!x)));
|
2019-02-27 18:13:16 +00:00
|
|
|
dispatch(expandConversationsSuccess(response.data, next ? next.uri : null, isLoadingRecent));
|
2018-10-07 21:44:58 +00:00
|
|
|
})
|
|
|
|
.catch(err => dispatch(expandConversationsFail(err)));
|
|
|
|
};
|
|
|
|
|
|
|
|
export const expandConversationsRequest = () => ({
|
|
|
|
type: CONVERSATIONS_FETCH_REQUEST,
|
|
|
|
});
|
|
|
|
|
2019-02-27 18:13:16 +00:00
|
|
|
export const expandConversationsSuccess = (conversations, next, isLoadingRecent) => ({
|
2018-10-07 21:44:58 +00:00
|
|
|
type: CONVERSATIONS_FETCH_SUCCESS,
|
|
|
|
conversations,
|
|
|
|
next,
|
2019-02-27 18:13:16 +00:00
|
|
|
isLoadingRecent,
|
2018-10-07 21:44:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export const expandConversationsFail = error => ({
|
|
|
|
type: CONVERSATIONS_FETCH_FAIL,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const updateConversations = conversation => dispatch => {
|
|
|
|
dispatch(importFetchedAccounts(conversation.accounts));
|
|
|
|
|
|
|
|
if (conversation.last_status) {
|
|
|
|
dispatch(importFetchedStatus(conversation.last_status));
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: CONVERSATIONS_UPDATE,
|
|
|
|
conversation,
|
|
|
|
});
|
|
|
|
};
|