mutes.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import api, { getLinks } from '../api'
  2. import { fetchRelationships } from './accounts';
  3. export const MUTES_FETCH_REQUEST = 'MUTES_FETCH_REQUEST';
  4. export const MUTES_FETCH_SUCCESS = 'MUTES_FETCH_SUCCESS';
  5. export const MUTES_FETCH_FAIL = 'MUTES_FETCH_FAIL';
  6. export const MUTES_EXPAND_REQUEST = 'MUTES_EXPAND_REQUEST';
  7. export const MUTES_EXPAND_SUCCESS = 'MUTES_EXPAND_SUCCESS';
  8. export const MUTES_EXPAND_FAIL = 'MUTES_EXPAND_FAIL';
  9. export function fetchMutes() {
  10. return (dispatch, getState) => {
  11. dispatch(fetchMutesRequest());
  12. api(getState).get('/api/v1/mutes').then(response => {
  13. const next = getLinks(response).refs.find(link => link.rel === 'next');
  14. dispatch(fetchMutesSuccess(response.data, next ? next.uri : null));
  15. dispatch(fetchRelationships(response.data.map(item => item.id)));
  16. }).catch(error => dispatch(fetchMutesFail(error)));
  17. };
  18. };
  19. export function fetchMutesRequest() {
  20. return {
  21. type: MUTES_FETCH_REQUEST
  22. };
  23. };
  24. export function fetchMutesSuccess(accounts, next) {
  25. return {
  26. type: MUTES_FETCH_SUCCESS,
  27. accounts,
  28. next
  29. };
  30. };
  31. export function fetchMutesFail(error) {
  32. return {
  33. type: MUTES_FETCH_FAIL,
  34. error
  35. };
  36. };
  37. export function expandMutes() {
  38. return (dispatch, getState) => {
  39. const url = getState().getIn(['user_lists', 'mutes', 'next']);
  40. if (url === null) {
  41. return;
  42. }
  43. dispatch(expandMutesRequest());
  44. api(getState).get(url).then(response => {
  45. const next = getLinks(response).refs.find(link => link.rel === 'next');
  46. dispatch(expandMutesSuccess(response.data, next ? next.uri : null));
  47. dispatch(fetchRelationships(response.data.map(item => item.id)));
  48. }).catch(error => dispatch(expandMutesFail(error)));
  49. };
  50. };
  51. export function expandMutesRequest() {
  52. return {
  53. type: MUTES_EXPAND_REQUEST
  54. };
  55. };
  56. export function expandMutesSuccess(accounts, next) {
  57. return {
  58. type: MUTES_EXPAND_SUCCESS,
  59. accounts,
  60. next
  61. };
  62. };
  63. export function expandMutesFail(error) {
  64. return {
  65. type: MUTES_EXPAND_FAIL,
  66. error
  67. };
  68. };