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.

84 lines
2.5 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 CONVERSATIONS_READ = 'CONVERSATIONS_READ';
  14. export const mountConversations = () => ({
  15. type: CONVERSATIONS_MOUNT,
  16. });
  17. export const unmountConversations = () => ({
  18. type: CONVERSATIONS_UNMOUNT,
  19. });
  20. export const markConversationRead = conversationId => (dispatch, getState) => {
  21. dispatch({
  22. type: CONVERSATIONS_READ,
  23. id: conversationId,
  24. });
  25. api(getState).post(`/api/v1/conversations/${conversationId}/read`);
  26. };
  27. export const expandConversations = ({ maxId } = {}) => (dispatch, getState) => {
  28. dispatch(expandConversationsRequest());
  29. const params = { max_id: maxId };
  30. if (!maxId) {
  31. params.since_id = getState().getIn(['conversations', 'items', 0, 'last_status']);
  32. }
  33. const isLoadingRecent = !!params.since_id;
  34. api(getState).get('/api/v1/conversations', { params })
  35. .then(response => {
  36. const next = getLinks(response).refs.find(link => link.rel === 'next');
  37. dispatch(importFetchedAccounts(response.data.reduce((aggr, item) => aggr.concat(item.accounts), [])));
  38. dispatch(importFetchedStatuses(response.data.map(item => item.last_status).filter(x => !!x)));
  39. dispatch(expandConversationsSuccess(response.data, next ? next.uri : null, isLoadingRecent));
  40. })
  41. .catch(err => dispatch(expandConversationsFail(err)));
  42. };
  43. export const expandConversationsRequest = () => ({
  44. type: CONVERSATIONS_FETCH_REQUEST,
  45. });
  46. export const expandConversationsSuccess = (conversations, next, isLoadingRecent) => ({
  47. type: CONVERSATIONS_FETCH_SUCCESS,
  48. conversations,
  49. next,
  50. isLoadingRecent,
  51. });
  52. export const expandConversationsFail = error => ({
  53. type: CONVERSATIONS_FETCH_FAIL,
  54. error,
  55. });
  56. export const updateConversations = conversation => dispatch => {
  57. dispatch(importFetchedAccounts(conversation.accounts));
  58. if (conversation.last_status) {
  59. dispatch(importFetchedStatus(conversation.last_status));
  60. }
  61. dispatch({
  62. type: CONVERSATIONS_UPDATE,
  63. conversation,
  64. });
  65. };