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.

160 lines
6.4 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 initialState = ImmutableMap({
  56. followers: ImmutableMap(),
  57. following: ImmutableMap(),
  58. reblogged_by: ImmutableMap(),
  59. favourited_by: ImmutableMap(),
  60. follow_requests: ImmutableMap(),
  61. blocks: ImmutableMap(),
  62. mutes: ImmutableMap(),
  63. });
  64. const normalizeList = (state, type, id, accounts, next) => {
  65. return state.setIn([type, id], ImmutableMap({
  66. next,
  67. items: ImmutableList(accounts.map(item => item.id)),
  68. isLoading: false,
  69. }));
  70. };
  71. const appendToList = (state, type, id, accounts, next) => {
  72. return state.updateIn([type, id], map => {
  73. return map.set('next', next).set('isLoading', false).update('items', list => list.concat(accounts.map(item => item.id)));
  74. });
  75. };
  76. const normalizeFollowRequest = (state, notification) => {
  77. return state.updateIn(['follow_requests', 'items'], list => {
  78. return list.filterNot(item => item === notification.account.id).unshift(notification.account.id);
  79. });
  80. };
  81. export default function userLists(state = initialState, action) {
  82. switch(action.type) {
  83. case FOLLOWERS_FETCH_SUCCESS:
  84. return normalizeList(state, 'followers', action.id, action.accounts, action.next);
  85. case FOLLOWERS_EXPAND_SUCCESS:
  86. return appendToList(state, 'followers', action.id, action.accounts, action.next);
  87. case FOLLOWERS_FETCH_REQUEST:
  88. case FOLLOWERS_EXPAND_REQUEST:
  89. return state.setIn(['followers', action.id, 'isLoading'], true);
  90. case FOLLOWERS_FETCH_FAIL:
  91. case FOLLOWERS_EXPAND_FAIL:
  92. return state.setIn(['followers', action.id, 'isLoading'], false);
  93. case FOLLOWING_FETCH_SUCCESS:
  94. return normalizeList(state, 'following', action.id, action.accounts, action.next);
  95. case FOLLOWING_EXPAND_SUCCESS:
  96. return appendToList(state, 'following', action.id, action.accounts, action.next);
  97. case FOLLOWING_FETCH_REQUEST:
  98. case FOLLOWING_EXPAND_REQUEST:
  99. return state.setIn(['following', action.id, 'isLoading'], true);
  100. case FOLLOWING_FETCH_FAIL:
  101. case FOLLOWING_EXPAND_FAIL:
  102. return state.setIn(['following', action.id, 'isLoading'], false);
  103. case REBLOGS_FETCH_SUCCESS:
  104. return state.setIn(['reblogged_by', action.id], ImmutableList(action.accounts.map(item => item.id)));
  105. case FAVOURITES_FETCH_SUCCESS:
  106. return state.setIn(['favourited_by', action.id], ImmutableList(action.accounts.map(item => item.id)));
  107. case NOTIFICATIONS_UPDATE:
  108. return action.notification.type === 'follow_request' ? normalizeFollowRequest(state, action.notification) : state;
  109. case FOLLOW_REQUESTS_FETCH_SUCCESS:
  110. return state.setIn(['follow_requests', 'items'], ImmutableList(action.accounts.map(item => item.id))).setIn(['follow_requests', 'next'], action.next).setIn(['follow_requests', 'isLoading'], false);
  111. case FOLLOW_REQUESTS_EXPAND_SUCCESS:
  112. return state.updateIn(['follow_requests', 'items'], list => list.concat(action.accounts.map(item => item.id))).setIn(['follow_requests', 'next'], action.next).setIn(['follow_requests', 'isLoading'], false);
  113. case FOLLOW_REQUESTS_FETCH_REQUEST:
  114. case FOLLOW_REQUESTS_EXPAND_REQUEST:
  115. return state.setIn(['follow_requests', 'isLoading'], true);
  116. case FOLLOW_REQUESTS_FETCH_FAIL:
  117. case FOLLOW_REQUESTS_EXPAND_FAIL:
  118. return state.setIn(['follow_requests', 'isLoading'], false);
  119. case FOLLOW_REQUEST_AUTHORIZE_SUCCESS:
  120. case FOLLOW_REQUEST_REJECT_SUCCESS:
  121. return state.updateIn(['follow_requests', 'items'], list => list.filterNot(item => item === action.id));
  122. case BLOCKS_FETCH_SUCCESS:
  123. return state.setIn(['blocks', 'items'], ImmutableList(action.accounts.map(item => item.id))).setIn(['blocks', 'next'], action.next);
  124. case BLOCKS_EXPAND_SUCCESS:
  125. return state.updateIn(['blocks', 'items'], list => list.concat(action.accounts.map(item => item.id))).setIn(['blocks', 'next'], action.next);
  126. case BLOCKS_FETCH_REQUEST:
  127. case BLOCKS_EXPAND_REQUEST:
  128. return state.setIn(['blocks', 'isLoading'], true);
  129. case BLOCKS_FETCH_FAIL:
  130. case BLOCKS_EXPAND_FAIL:
  131. return state.setIn(['blocks', 'isLoading'], false);
  132. case MUTES_FETCH_SUCCESS:
  133. return state.setIn(['mutes', 'items'], ImmutableList(action.accounts.map(item => item.id))).setIn(['mutes', 'next'], action.next);
  134. case MUTES_EXPAND_SUCCESS:
  135. return state.updateIn(['mutes', 'items'], list => list.concat(action.accounts.map(item => item.id))).setIn(['mutes', 'next'], action.next);
  136. case MUTES_FETCH_REQUEST:
  137. case MUTES_EXPAND_REQUEST:
  138. return state.setIn(['mutes', 'isLoading'], true);
  139. case MUTES_FETCH_FAIL:
  140. case MUTES_EXPAND_FAIL:
  141. return state.setIn(['mutes', 'isLoading'], false);
  142. case DIRECTORY_FETCH_SUCCESS:
  143. return state.setIn(['directory', 'items'], ImmutableList(action.accounts.map(item => item.id))).setIn(['directory', 'isLoading'], false);
  144. case DIRECTORY_EXPAND_SUCCESS:
  145. return state.updateIn(['directory', 'items'], list => list.concat(action.accounts.map(item => item.id))).setIn(['directory', 'isLoading'], false);
  146. case DIRECTORY_FETCH_REQUEST:
  147. case DIRECTORY_EXPAND_REQUEST:
  148. return state.setIn(['directory', 'isLoading'], true);
  149. case DIRECTORY_FETCH_FAIL:
  150. case DIRECTORY_EXPAND_FAIL:
  151. return state.setIn(['directory', 'isLoading'], false);
  152. default:
  153. return state;
  154. }
  155. };