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.

80 lines
3.4 KiB

  1. import {
  2. FOLLOWERS_FETCH_SUCCESS,
  3. FOLLOWERS_EXPAND_SUCCESS,
  4. FOLLOWING_FETCH_SUCCESS,
  5. FOLLOWING_EXPAND_SUCCESS,
  6. FOLLOW_REQUESTS_FETCH_SUCCESS,
  7. FOLLOW_REQUESTS_EXPAND_SUCCESS,
  8. FOLLOW_REQUEST_AUTHORIZE_SUCCESS,
  9. FOLLOW_REQUEST_REJECT_SUCCESS,
  10. } from 'flavours/glitch/actions/accounts';
  11. import {
  12. REBLOGS_FETCH_SUCCESS,
  13. FAVOURITES_FETCH_SUCCESS,
  14. } from 'flavours/glitch/actions/interactions';
  15. import {
  16. BLOCKS_FETCH_SUCCESS,
  17. BLOCKS_EXPAND_SUCCESS,
  18. } from 'flavours/glitch/actions/blocks';
  19. import {
  20. MUTES_FETCH_SUCCESS,
  21. MUTES_EXPAND_SUCCESS,
  22. } from 'flavours/glitch/actions/mutes';
  23. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  24. const initialState = ImmutableMap({
  25. followers: ImmutableMap(),
  26. following: ImmutableMap(),
  27. reblogged_by: ImmutableMap(),
  28. favourited_by: ImmutableMap(),
  29. follow_requests: ImmutableMap(),
  30. blocks: ImmutableMap(),
  31. mutes: ImmutableMap(),
  32. });
  33. const normalizeList = (state, type, id, accounts, next) => {
  34. return state.setIn([type, id], ImmutableMap({
  35. next,
  36. items: ImmutableList(accounts.map(item => item.id)),
  37. }));
  38. };
  39. const appendToList = (state, type, id, accounts, next) => {
  40. return state.updateIn([type, id], map => {
  41. return map.set('next', next).update('items', list => list.concat(accounts.map(item => item.id)));
  42. });
  43. };
  44. export default function userLists(state = initialState, action) {
  45. switch(action.type) {
  46. case FOLLOWERS_FETCH_SUCCESS:
  47. return normalizeList(state, 'followers', action.id, action.accounts, action.next);
  48. case FOLLOWERS_EXPAND_SUCCESS:
  49. return appendToList(state, 'followers', action.id, action.accounts, action.next);
  50. case FOLLOWING_FETCH_SUCCESS:
  51. return normalizeList(state, 'following', action.id, action.accounts, action.next);
  52. case FOLLOWING_EXPAND_SUCCESS:
  53. return appendToList(state, 'following', action.id, action.accounts, action.next);
  54. case REBLOGS_FETCH_SUCCESS:
  55. return state.setIn(['reblogged_by', action.id], ImmutableList(action.accounts.map(item => item.id)));
  56. case FAVOURITES_FETCH_SUCCESS:
  57. return state.setIn(['favourited_by', action.id], ImmutableList(action.accounts.map(item => item.id)));
  58. case FOLLOW_REQUESTS_FETCH_SUCCESS:
  59. return state.setIn(['follow_requests', 'items'], ImmutableList(action.accounts.map(item => item.id))).setIn(['follow_requests', 'next'], action.next);
  60. case FOLLOW_REQUESTS_EXPAND_SUCCESS:
  61. return state.updateIn(['follow_requests', 'items'], list => list.concat(action.accounts.map(item => item.id))).setIn(['follow_requests', 'next'], action.next);
  62. case FOLLOW_REQUEST_AUTHORIZE_SUCCESS:
  63. case FOLLOW_REQUEST_REJECT_SUCCESS:
  64. return state.updateIn(['follow_requests', 'items'], list => list.filterNot(item => item === action.id));
  65. case BLOCKS_FETCH_SUCCESS:
  66. return state.setIn(['blocks', 'items'], ImmutableList(action.accounts.map(item => item.id))).setIn(['blocks', 'next'], action.next);
  67. case BLOCKS_EXPAND_SUCCESS:
  68. return state.updateIn(['blocks', 'items'], list => list.concat(action.accounts.map(item => item.id))).setIn(['blocks', 'next'], action.next);
  69. case MUTES_FETCH_SUCCESS:
  70. return state.setIn(['mutes', 'items'], ImmutableList(action.accounts.map(item => item.id))).setIn(['mutes', 'next'], action.next);
  71. case MUTES_EXPAND_SUCCESS:
  72. return state.updateIn(['mutes', 'items'], list => list.concat(action.accounts.map(item => item.id))).setIn(['mutes', 'next'], action.next);
  73. default:
  74. return state;
  75. }
  76. };