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

80 lines
3.3 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 Immutable from 'immutable';
  24. const initialState = Immutable.Map({
  25. followers: Immutable.Map(),
  26. following: Immutable.Map(),
  27. reblogged_by: Immutable.Map(),
  28. favourited_by: Immutable.Map(),
  29. follow_requests: Immutable.Map(),
  30. blocks: Immutable.Map(),
  31. mutes: Immutable.Map()
  32. });
  33. const normalizeList = (state, type, id, accounts, next) => {
  34. return state.setIn([type, id], Immutable.Map({
  35. next,
  36. items: Immutable.List(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], Immutable.List(action.accounts.map(item => item.id)));
  56. case FAVOURITES_FETCH_SUCCESS:
  57. return state.setIn(['favourited_by', action.id], Immutable.List(action.accounts.map(item => item.id)));
  58. case FOLLOW_REQUESTS_FETCH_SUCCESS:
  59. return state.setIn(['follow_requests', 'items'], Immutable.List(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'], Immutable.List(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'], Immutable.List(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. };