You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.1 KiB

  1. import api, { getLinks } from '../api';
  2. import {
  3. importFetchedAccounts,
  4. importFetchedStatuses,
  5. importFetchedStatus,
  6. } from './importer';
  7. export const CONVERSATIONS_MOUNT = 'CONVERSATIONS_MOUNT';
  8. export const CONVERSATIONS_UNMOUNT = 'CONVERSATIONS_UNMOUNT';
  9. export const CONVERSATIONS_FETCH_REQUEST = 'CONVERSATIONS_FETCH_REQUEST';
  10. export const CONVERSATIONS_FETCH_SUCCESS = 'CONVERSATIONS_FETCH_SUCCESS';
  11. export const CONVERSATIONS_FETCH_FAIL = 'CONVERSATIONS_FETCH_FAIL';
  12. export const CONVERSATIONS_UPDATE = 'CONVERSATIONS_UPDATE';
  13. export const mountConversations = () => ({
  14. type: CONVERSATIONS_MOUNT,
  15. });
  16. export const unmountConversations = () => ({
  17. type: CONVERSATIONS_UNMOUNT,
  18. });
  19. export const expandConversations = ({ maxId } = {}) => (dispatch, getState) => {
  20. dispatch(expandConversationsRequest());
  21. const params = { max_id: maxId };
  22. if (!maxId) {
  23. params.since_id = getState().getIn(['conversations', 0, 'last_status']);
  24. }
  25. api(getState).get('/api/v1/conversations', { params })
  26. .then(response => {
  27. const next = getLinks(response).refs.find(link => link.rel === 'next');
  28. dispatch(importFetchedAccounts(response.data.reduce((aggr, item) => aggr.concat(item.accounts), [])));
  29. dispatch(importFetchedStatuses(response.data.map(item => item.last_status).filter(x => !!x)));
  30. dispatch(expandConversationsSuccess(response.data, next ? next.uri : null));
  31. })
  32. .catch(err => dispatch(expandConversationsFail(err)));
  33. };
  34. export const expandConversationsRequest = () => ({
  35. type: CONVERSATIONS_FETCH_REQUEST,
  36. });
  37. export const expandConversationsSuccess = (conversations, next) => ({
  38. type: CONVERSATIONS_FETCH_SUCCESS,
  39. conversations,
  40. next,
  41. });
  42. export const expandConversationsFail = error => ({
  43. type: CONVERSATIONS_FETCH_FAIL,
  44. error,
  45. });
  46. export const updateConversations = conversation => dispatch => {
  47. dispatch(importFetchedAccounts(conversation.accounts));
  48. if (conversation.last_status) {
  49. dispatch(importFetchedStatus(conversation.last_status));
  50. }
  51. dispatch({
  52. type: CONVERSATIONS_UPDATE,
  53. conversation,
  54. });
  55. };