闭社主体 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.

82 lines
2.1 KiB

  1. import api, { getLinks } from '../api';
  2. import { fetchRelationships } from './accounts';
  3. export const BLOCKS_FETCH_REQUEST = 'BLOCKS_FETCH_REQUEST';
  4. export const BLOCKS_FETCH_SUCCESS = 'BLOCKS_FETCH_SUCCESS';
  5. export const BLOCKS_FETCH_FAIL = 'BLOCKS_FETCH_FAIL';
  6. export const BLOCKS_EXPAND_REQUEST = 'BLOCKS_EXPAND_REQUEST';
  7. export const BLOCKS_EXPAND_SUCCESS = 'BLOCKS_EXPAND_SUCCESS';
  8. export const BLOCKS_EXPAND_FAIL = 'BLOCKS_EXPAND_FAIL';
  9. export function fetchBlocks() {
  10. return (dispatch, getState) => {
  11. dispatch(fetchBlocksRequest());
  12. api(getState).get('/api/v1/blocks').then(response => {
  13. const next = getLinks(response).refs.find(link => link.rel === 'next');
  14. dispatch(fetchBlocksSuccess(response.data, next ? next.uri : null));
  15. dispatch(fetchRelationships(response.data.map(item => item.id)));
  16. }).catch(error => dispatch(fetchBlocksFail(error)));
  17. };
  18. };
  19. export function fetchBlocksRequest() {
  20. return {
  21. type: BLOCKS_FETCH_REQUEST,
  22. };
  23. };
  24. export function fetchBlocksSuccess(accounts, next) {
  25. return {
  26. type: BLOCKS_FETCH_SUCCESS,
  27. accounts,
  28. next,
  29. };
  30. };
  31. export function fetchBlocksFail(error) {
  32. return {
  33. type: BLOCKS_FETCH_FAIL,
  34. error,
  35. };
  36. };
  37. export function expandBlocks() {
  38. return (dispatch, getState) => {
  39. const url = getState().getIn(['user_lists', 'blocks', 'next']);
  40. if (url === null) {
  41. return;
  42. }
  43. dispatch(expandBlocksRequest());
  44. api(getState).get(url).then(response => {
  45. const next = getLinks(response).refs.find(link => link.rel === 'next');
  46. dispatch(expandBlocksSuccess(response.data, next ? next.uri : null));
  47. dispatch(fetchRelationships(response.data.map(item => item.id)));
  48. }).catch(error => dispatch(expandBlocksFail(error)));
  49. };
  50. };
  51. export function expandBlocksRequest() {
  52. return {
  53. type: BLOCKS_EXPAND_REQUEST,
  54. };
  55. };
  56. export function expandBlocksSuccess(accounts, next) {
  57. return {
  58. type: BLOCKS_EXPAND_SUCCESS,
  59. accounts,
  60. next,
  61. };
  62. };
  63. export function expandBlocksFail(error) {
  64. return {
  65. type: BLOCKS_EXPAND_FAIL,
  66. error,
  67. };
  68. };