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.

98 lines
4.1 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 '../actions/accounts';
  11. import {
  12. REBLOGS_FETCH_SUCCESS,
  13. FAVOURITES_FETCH_SUCCESS,
  14. } from '../actions/interactions';
  15. import {
  16. BLOCKS_FETCH_SUCCESS,
  17. BLOCKS_EXPAND_SUCCESS,
  18. } from '../actions/blocks';
  19. import {
  20. MUTES_FETCH_SUCCESS,
  21. MUTES_EXPAND_SUCCESS,
  22. } from '../actions/mutes';
  23. import {
  24. DIRECTORY_FETCH_REQUEST,
  25. DIRECTORY_FETCH_SUCCESS,
  26. DIRECTORY_FETCH_FAIL,
  27. DIRECTORY_EXPAND_REQUEST,
  28. DIRECTORY_EXPAND_SUCCESS,
  29. DIRECTORY_EXPAND_FAIL,
  30. } from 'mastodon/actions/directory';
  31. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  32. const initialState = ImmutableMap({
  33. followers: ImmutableMap(),
  34. following: ImmutableMap(),
  35. reblogged_by: ImmutableMap(),
  36. favourited_by: ImmutableMap(),
  37. follow_requests: ImmutableMap(),
  38. blocks: ImmutableMap(),
  39. mutes: ImmutableMap(),
  40. });
  41. const normalizeList = (state, type, id, accounts, next) => {
  42. return state.setIn([type, id], ImmutableMap({
  43. next,
  44. items: ImmutableList(accounts.map(item => item.id)),
  45. }));
  46. };
  47. const appendToList = (state, type, id, accounts, next) => {
  48. return state.updateIn([type, id], map => {
  49. return map.set('next', next).update('items', list => list.concat(accounts.map(item => item.id)));
  50. });
  51. };
  52. export default function userLists(state = initialState, action) {
  53. switch(action.type) {
  54. case FOLLOWERS_FETCH_SUCCESS:
  55. return normalizeList(state, 'followers', action.id, action.accounts, action.next);
  56. case FOLLOWERS_EXPAND_SUCCESS:
  57. return appendToList(state, 'followers', action.id, action.accounts, action.next);
  58. case FOLLOWING_FETCH_SUCCESS:
  59. return normalizeList(state, 'following', action.id, action.accounts, action.next);
  60. case FOLLOWING_EXPAND_SUCCESS:
  61. return appendToList(state, 'following', action.id, action.accounts, action.next);
  62. case REBLOGS_FETCH_SUCCESS:
  63. return state.setIn(['reblogged_by', action.id], ImmutableList(action.accounts.map(item => item.id)));
  64. case FAVOURITES_FETCH_SUCCESS:
  65. return state.setIn(['favourited_by', action.id], ImmutableList(action.accounts.map(item => item.id)));
  66. case FOLLOW_REQUESTS_FETCH_SUCCESS:
  67. return state.setIn(['follow_requests', 'items'], ImmutableList(action.accounts.map(item => item.id))).setIn(['follow_requests', 'next'], action.next);
  68. case FOLLOW_REQUESTS_EXPAND_SUCCESS:
  69. return state.updateIn(['follow_requests', 'items'], list => list.concat(action.accounts.map(item => item.id))).setIn(['follow_requests', 'next'], action.next);
  70. case FOLLOW_REQUEST_AUTHORIZE_SUCCESS:
  71. case FOLLOW_REQUEST_REJECT_SUCCESS:
  72. return state.updateIn(['follow_requests', 'items'], list => list.filterNot(item => item === action.id));
  73. case BLOCKS_FETCH_SUCCESS:
  74. return state.setIn(['blocks', 'items'], ImmutableList(action.accounts.map(item => item.id))).setIn(['blocks', 'next'], action.next);
  75. case BLOCKS_EXPAND_SUCCESS:
  76. return state.updateIn(['blocks', 'items'], list => list.concat(action.accounts.map(item => item.id))).setIn(['blocks', 'next'], action.next);
  77. case MUTES_FETCH_SUCCESS:
  78. return state.setIn(['mutes', 'items'], ImmutableList(action.accounts.map(item => item.id))).setIn(['mutes', 'next'], action.next);
  79. case MUTES_EXPAND_SUCCESS:
  80. return state.updateIn(['mutes', 'items'], list => list.concat(action.accounts.map(item => item.id))).setIn(['mutes', 'next'], action.next);
  81. case DIRECTORY_FETCH_SUCCESS:
  82. return state.setIn(['directory', 'items'], ImmutableList(action.accounts.map(item => item.id))).setIn(['directory', 'isLoading'], false);
  83. case DIRECTORY_EXPAND_SUCCESS:
  84. return state.updateIn(['directory', 'items'], list => list.concat(action.accounts.map(item => item.id))).setIn(['directory', 'isLoading'], false);
  85. case DIRECTORY_FETCH_REQUEST:
  86. case DIRECTORY_EXPAND_REQUEST:
  87. return state.setIn(['directory', 'isLoading'], true);
  88. case DIRECTORY_FETCH_FAIL:
  89. case DIRECTORY_EXPAND_FAIL:
  90. return state.setIn(['directory', 'isLoading'], false);
  91. default:
  92. return state;
  93. }
  94. };