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.

109 lines
4.6 KiB

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