闭社主体 forked from https://github.com/tootsuite/mastodon
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.

93 lines
2.7 KiB

  1. import api, { getLinks } from '../api';
  2. import { importFetchedStatuses } from './importer';
  3. export const FAVOURITED_STATUSES_FETCH_REQUEST = 'FAVOURITED_STATUSES_FETCH_REQUEST';
  4. export const FAVOURITED_STATUSES_FETCH_SUCCESS = 'FAVOURITED_STATUSES_FETCH_SUCCESS';
  5. export const FAVOURITED_STATUSES_FETCH_FAIL = 'FAVOURITED_STATUSES_FETCH_FAIL';
  6. export const FAVOURITED_STATUSES_EXPAND_REQUEST = 'FAVOURITED_STATUSES_EXPAND_REQUEST';
  7. export const FAVOURITED_STATUSES_EXPAND_SUCCESS = 'FAVOURITED_STATUSES_EXPAND_SUCCESS';
  8. export const FAVOURITED_STATUSES_EXPAND_FAIL = 'FAVOURITED_STATUSES_EXPAND_FAIL';
  9. export function fetchFavouritedStatuses() {
  10. return (dispatch, getState) => {
  11. if (getState().getIn(['status_lists', 'favourites', 'isLoading'])) {
  12. return;
  13. }
  14. dispatch(fetchFavouritedStatusesRequest());
  15. api(getState).get('/api/v1/favourites').then(response => {
  16. const next = getLinks(response).refs.find(link => link.rel === 'next');
  17. dispatch(importFetchedStatuses(response.data));
  18. dispatch(fetchFavouritedStatusesSuccess(response.data, next ? next.uri : null));
  19. }).catch(error => {
  20. dispatch(fetchFavouritedStatusesFail(error));
  21. });
  22. };
  23. };
  24. export function fetchFavouritedStatusesRequest() {
  25. return {
  26. type: FAVOURITED_STATUSES_FETCH_REQUEST,
  27. skipLoading: true,
  28. };
  29. };
  30. export function fetchFavouritedStatusesSuccess(statuses, next) {
  31. return {
  32. type: FAVOURITED_STATUSES_FETCH_SUCCESS,
  33. statuses,
  34. next,
  35. skipLoading: true,
  36. };
  37. };
  38. export function fetchFavouritedStatusesFail(error) {
  39. return {
  40. type: FAVOURITED_STATUSES_FETCH_FAIL,
  41. error,
  42. skipLoading: true,
  43. };
  44. };
  45. export function expandFavouritedStatuses() {
  46. return (dispatch, getState) => {
  47. const url = getState().getIn(['status_lists', 'favourites', 'next'], null);
  48. if (url === null || getState().getIn(['status_lists', 'favourites', 'isLoading'])) {
  49. return;
  50. }
  51. dispatch(expandFavouritedStatusesRequest());
  52. api(getState).get(url).then(response => {
  53. const next = getLinks(response).refs.find(link => link.rel === 'next');
  54. dispatch(importFetchedStatuses(response.data));
  55. dispatch(expandFavouritedStatusesSuccess(response.data, next ? next.uri : null));
  56. }).catch(error => {
  57. dispatch(expandFavouritedStatusesFail(error));
  58. });
  59. };
  60. };
  61. export function expandFavouritedStatusesRequest() {
  62. return {
  63. type: FAVOURITED_STATUSES_EXPAND_REQUEST,
  64. };
  65. };
  66. export function expandFavouritedStatusesSuccess(statuses, next) {
  67. return {
  68. type: FAVOURITED_STATUSES_EXPAND_SUCCESS,
  69. statuses,
  70. next,
  71. };
  72. };
  73. export function expandFavouritedStatusesFail(error) {
  74. return {
  75. type: FAVOURITED_STATUSES_EXPAND_FAIL,
  76. error,
  77. };
  78. };