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.

166 lines
5.9 KiB

  1. import {
  2. NOTIFICATIONS_UPDATE,
  3. } from '../actions/notifications';
  4. import {
  5. FOLLOWERS_FETCH_REQUEST,
  6. FOLLOWERS_FETCH_SUCCESS,
  7. FOLLOWERS_FETCH_FAIL,
  8. FOLLOWERS_EXPAND_REQUEST,
  9. FOLLOWERS_EXPAND_SUCCESS,
  10. FOLLOWERS_EXPAND_FAIL,
  11. FOLLOWING_FETCH_REQUEST,
  12. FOLLOWING_FETCH_SUCCESS,
  13. FOLLOWING_FETCH_FAIL,
  14. FOLLOWING_EXPAND_REQUEST,
  15. FOLLOWING_EXPAND_SUCCESS,
  16. FOLLOWING_EXPAND_FAIL,
  17. FOLLOW_REQUESTS_FETCH_REQUEST,
  18. FOLLOW_REQUESTS_FETCH_SUCCESS,
  19. FOLLOW_REQUESTS_FETCH_FAIL,
  20. FOLLOW_REQUESTS_EXPAND_REQUEST,
  21. FOLLOW_REQUESTS_EXPAND_SUCCESS,
  22. FOLLOW_REQUESTS_EXPAND_FAIL,
  23. FOLLOW_REQUEST_AUTHORIZE_SUCCESS,
  24. FOLLOW_REQUEST_REJECT_SUCCESS,
  25. } from '../actions/accounts';
  26. import {
  27. REBLOGS_FETCH_SUCCESS,
  28. FAVOURITES_FETCH_SUCCESS,
  29. } from '../actions/interactions';
  30. import {
  31. BLOCKS_FETCH_REQUEST,
  32. BLOCKS_FETCH_SUCCESS,
  33. BLOCKS_FETCH_FAIL,
  34. BLOCKS_EXPAND_REQUEST,
  35. BLOCKS_EXPAND_SUCCESS,
  36. BLOCKS_EXPAND_FAIL,
  37. } from '../actions/blocks';
  38. import {
  39. MUTES_FETCH_REQUEST,
  40. MUTES_FETCH_SUCCESS,
  41. MUTES_FETCH_FAIL,
  42. MUTES_EXPAND_REQUEST,
  43. MUTES_EXPAND_SUCCESS,
  44. MUTES_EXPAND_FAIL,
  45. } from '../actions/mutes';
  46. import {
  47. DIRECTORY_FETCH_REQUEST,
  48. DIRECTORY_FETCH_SUCCESS,
  49. DIRECTORY_FETCH_FAIL,
  50. DIRECTORY_EXPAND_REQUEST,
  51. DIRECTORY_EXPAND_SUCCESS,
  52. DIRECTORY_EXPAND_FAIL,
  53. } from 'mastodon/actions/directory';
  54. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  55. const initialListState = ImmutableMap({
  56. next: null,
  57. isLoading: false,
  58. items: ImmutableList(),
  59. });
  60. const initialState = ImmutableMap({
  61. followers: initialListState,
  62. following: initialListState,
  63. reblogged_by: initialListState,
  64. favourited_by: initialListState,
  65. follow_requests: initialListState,
  66. blocks: initialListState,
  67. mutes: initialListState,
  68. });
  69. const normalizeList = (state, path, accounts, next) => {
  70. return state.setIn(path, ImmutableMap({
  71. next,
  72. items: ImmutableList(accounts.map(item => item.id)),
  73. isLoading: false,
  74. }));
  75. };
  76. const appendToList = (state, path, accounts, next) => {
  77. return state.updateIn(path, map => {
  78. return map.set('next', next).set('isLoading', false).update('items', list => list.concat(accounts.map(item => item.id)));
  79. });
  80. };
  81. const normalizeFollowRequest = (state, notification) => {
  82. return state.updateIn(['follow_requests', 'items'], list => {
  83. return list.filterNot(item => item === notification.account.id).unshift(notification.account.id);
  84. });
  85. };
  86. export default function userLists(state = initialState, action) {
  87. switch(action.type) {
  88. case FOLLOWERS_FETCH_SUCCESS:
  89. return normalizeList(state, ['followers', action.id], action.accounts, action.next);
  90. case FOLLOWERS_EXPAND_SUCCESS:
  91. return appendToList(state, ['followers', action.id], action.accounts, action.next);
  92. case FOLLOWERS_FETCH_REQUEST:
  93. case FOLLOWERS_EXPAND_REQUEST:
  94. return state.setIn(['followers', action.id, 'isLoading'], true);
  95. case FOLLOWERS_FETCH_FAIL:
  96. case FOLLOWERS_EXPAND_FAIL:
  97. return state.setIn(['followers', action.id, 'isLoading'], false);
  98. case FOLLOWING_FETCH_SUCCESS:
  99. return normalizeList(state, ['following', action.id], action.accounts, action.next);
  100. case FOLLOWING_EXPAND_SUCCESS:
  101. return appendToList(state, ['following', action.id], action.accounts, action.next);
  102. case FOLLOWING_FETCH_REQUEST:
  103. case FOLLOWING_EXPAND_REQUEST:
  104. return state.setIn(['following', action.id, 'isLoading'], true);
  105. case FOLLOWING_FETCH_FAIL:
  106. case FOLLOWING_EXPAND_FAIL:
  107. return state.setIn(['following', action.id, 'isLoading'], false);
  108. case REBLOGS_FETCH_SUCCESS:
  109. return state.setIn(['reblogged_by', action.id], ImmutableList(action.accounts.map(item => item.id)));
  110. case FAVOURITES_FETCH_SUCCESS:
  111. return state.setIn(['favourited_by', action.id], ImmutableList(action.accounts.map(item => item.id)));
  112. case NOTIFICATIONS_UPDATE:
  113. return action.notification.type === 'follow_request' ? normalizeFollowRequest(state, action.notification) : state;
  114. case FOLLOW_REQUESTS_FETCH_SUCCESS:
  115. return normalizeList(state, ['follow_requests'], action.accounts, action.next);
  116. case FOLLOW_REQUESTS_EXPAND_SUCCESS:
  117. return appendToList(state, ['follow_requests'], action.accounts, action.next);
  118. case FOLLOW_REQUESTS_FETCH_REQUEST:
  119. case FOLLOW_REQUESTS_EXPAND_REQUEST:
  120. return state.setIn(['follow_requests', 'isLoading'], true);
  121. case FOLLOW_REQUESTS_FETCH_FAIL:
  122. case FOLLOW_REQUESTS_EXPAND_FAIL:
  123. return state.setIn(['follow_requests', 'isLoading'], false);
  124. case FOLLOW_REQUEST_AUTHORIZE_SUCCESS:
  125. case FOLLOW_REQUEST_REJECT_SUCCESS:
  126. return state.updateIn(['follow_requests', 'items'], list => list.filterNot(item => item === action.id));
  127. case BLOCKS_FETCH_SUCCESS:
  128. return normalizeList(state, ['blocks'], action.accounts, action.next);
  129. case BLOCKS_EXPAND_SUCCESS:
  130. return appendToList(state, ['blocks'], action.accounts, action.next);
  131. case BLOCKS_FETCH_REQUEST:
  132. case BLOCKS_EXPAND_REQUEST:
  133. return state.setIn(['blocks', 'isLoading'], true);
  134. case BLOCKS_FETCH_FAIL:
  135. case BLOCKS_EXPAND_FAIL:
  136. return state.setIn(['blocks', 'isLoading'], false);
  137. case MUTES_FETCH_SUCCESS:
  138. return normalizeList(state, ['mutes'], action.accounts, action.next);
  139. case MUTES_EXPAND_SUCCESS:
  140. return appendToList(state, ['mutes'], action.accounts, action.next);
  141. case MUTES_FETCH_REQUEST:
  142. case MUTES_EXPAND_REQUEST:
  143. return state.setIn(['mutes', 'isLoading'], true);
  144. case MUTES_FETCH_FAIL:
  145. case MUTES_EXPAND_FAIL:
  146. return state.setIn(['mutes', 'isLoading'], false);
  147. case DIRECTORY_FETCH_SUCCESS:
  148. return normalizeList(state, ['directory'], action.accounts, action.next);
  149. case DIRECTORY_EXPAND_SUCCESS:
  150. return appendToList(state, ['directory'], action.accounts, action.next);
  151. case DIRECTORY_FETCH_REQUEST:
  152. case DIRECTORY_EXPAND_REQUEST:
  153. return state.setIn(['directory', 'isLoading'], true);
  154. case DIRECTORY_FETCH_FAIL:
  155. case DIRECTORY_EXPAND_FAIL:
  156. return state.setIn(['directory', 'isLoading'], false);
  157. default:
  158. return state;
  159. }
  160. };