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.

70 lines
2.1 KiB

  1. import {
  2. ACCOUNT_FOLLOW_SUCCESS,
  3. ACCOUNT_FOLLOW_REQUEST,
  4. ACCOUNT_FOLLOW_FAIL,
  5. ACCOUNT_UNFOLLOW_SUCCESS,
  6. ACCOUNT_UNFOLLOW_REQUEST,
  7. ACCOUNT_UNFOLLOW_FAIL,
  8. ACCOUNT_BLOCK_SUCCESS,
  9. ACCOUNT_UNBLOCK_SUCCESS,
  10. ACCOUNT_MUTE_SUCCESS,
  11. ACCOUNT_UNMUTE_SUCCESS,
  12. ACCOUNT_PIN_SUCCESS,
  13. ACCOUNT_UNPIN_SUCCESS,
  14. RELATIONSHIPS_FETCH_SUCCESS,
  15. } from '../actions/accounts';
  16. import {
  17. DOMAIN_BLOCK_SUCCESS,
  18. DOMAIN_UNBLOCK_SUCCESS,
  19. } from '../actions/domain_blocks';
  20. import { Map as ImmutableMap, fromJS } from 'immutable';
  21. const normalizeRelationship = (state, relationship) => state.set(relationship.id, fromJS(relationship));
  22. const normalizeRelationships = (state, relationships) => {
  23. relationships.forEach(relationship => {
  24. state = normalizeRelationship(state, relationship);
  25. });
  26. return state;
  27. };
  28. const setDomainBlocking = (state, accounts, blocking) => {
  29. return state.withMutations(map => {
  30. accounts.forEach(id => {
  31. map.setIn([id, 'domain_blocking'], blocking);
  32. });
  33. });
  34. };
  35. const initialState = ImmutableMap();
  36. export default function relationships(state = initialState, action) {
  37. switch(action.type) {
  38. case ACCOUNT_FOLLOW_REQUEST:
  39. return state.setIn([action.id, action.locked ? 'requested' : 'following'], true);
  40. case ACCOUNT_FOLLOW_FAIL:
  41. return state.setIn([action.id, action.locked ? 'requested' : 'following'], false);
  42. case ACCOUNT_UNFOLLOW_REQUEST:
  43. return state.setIn([action.id, 'following'], false);
  44. case ACCOUNT_UNFOLLOW_FAIL:
  45. return state.setIn([action.id, 'following'], true);
  46. case ACCOUNT_FOLLOW_SUCCESS:
  47. case ACCOUNT_UNFOLLOW_SUCCESS:
  48. case ACCOUNT_BLOCK_SUCCESS:
  49. case ACCOUNT_UNBLOCK_SUCCESS:
  50. case ACCOUNT_MUTE_SUCCESS:
  51. case ACCOUNT_UNMUTE_SUCCESS:
  52. case ACCOUNT_PIN_SUCCESS:
  53. case ACCOUNT_UNPIN_SUCCESS:
  54. return normalizeRelationship(state, action.relationship);
  55. case RELATIONSHIPS_FETCH_SUCCESS:
  56. return normalizeRelationships(state, action.relationships);
  57. case DOMAIN_BLOCK_SUCCESS:
  58. return setDomainBlocking(state, action.accounts, true);
  59. case DOMAIN_UNBLOCK_SUCCESS:
  60. return setDomainBlocking(state, action.accounts, false);
  61. default:
  62. return state;
  63. }
  64. };