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.

71 lines
2.9 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 Immutable from 'immutable';
  20. const initialState = Immutable.Map({
  21. followers: Immutable.Map(),
  22. following: Immutable.Map(),
  23. reblogged_by: Immutable.Map(),
  24. favourited_by: Immutable.Map(),
  25. follow_requests: Immutable.Map(),
  26. blocks: Immutable.Map()
  27. });
  28. const normalizeList = (state, type, id, accounts, next) => {
  29. return state.setIn([type, id], Immutable.Map({
  30. next,
  31. items: Immutable.List(accounts.map(item => item.id))
  32. }));
  33. };
  34. const appendToList = (state, type, id, accounts, next) => {
  35. return state.updateIn([type, id], map => {
  36. return map.set('next', next).update('items', list => list.push(...accounts.map(item => item.id)));
  37. });
  38. };
  39. export default function userLists(state = initialState, action) {
  40. switch(action.type) {
  41. case FOLLOWERS_FETCH_SUCCESS:
  42. return normalizeList(state, 'followers', action.id, action.accounts, action.next);
  43. case FOLLOWERS_EXPAND_SUCCESS:
  44. return appendToList(state, 'followers', action.id, action.accounts, action.next);
  45. case FOLLOWING_FETCH_SUCCESS:
  46. return normalizeList(state, 'following', action.id, action.accounts, action.next);
  47. case FOLLOWING_EXPAND_SUCCESS:
  48. return appendToList(state, 'following', action.id, action.accounts, action.next);
  49. case REBLOGS_FETCH_SUCCESS:
  50. return state.setIn(['reblogged_by', action.id], Immutable.List(action.accounts.map(item => item.id)));
  51. case FAVOURITES_FETCH_SUCCESS:
  52. return state.setIn(['favourited_by', action.id], Immutable.List(action.accounts.map(item => item.id)));
  53. case FOLLOW_REQUESTS_FETCH_SUCCESS:
  54. return state.setIn(['follow_requests', 'items'], Immutable.List(action.accounts.map(item => item.id))).setIn(['follow_requests', 'next'], action.next);
  55. case FOLLOW_REQUESTS_EXPAND_SUCCESS:
  56. return state.updateIn(['follow_requests', 'items'], list => list.push(...action.accounts.map(item => item.id))).setIn(['follow_requests', 'next'], action.next);
  57. case FOLLOW_REQUEST_AUTHORIZE_SUCCESS:
  58. case FOLLOW_REQUEST_REJECT_SUCCESS:
  59. return state.updateIn(['follow_requests', 'items'], list => list.filterNot(item => item === action.id));
  60. case BLOCKS_FETCH_SUCCESS:
  61. return state.setIn(['blocks', 'items'], Immutable.List(action.accounts.map(item => item.id))).setIn(['blocks', 'next'], action.next);
  62. case BLOCKS_EXPAND_SUCCESS:
  63. return state.updateIn(['blocks', 'items'], list => list.push(...action.accounts.map(item => item.id))).setIn(['blocks', 'next'], action.next);
  64. default:
  65. return state;
  66. }
  67. };