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.

74 lines
2.3 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 {
  21. ACCOUNT_NOTE_SUBMIT_SUCCESS,
  22. } from '../actions/account_notes';
  23. import { Map as ImmutableMap, fromJS } from 'immutable';
  24. const normalizeRelationship = (state, relationship) => state.set(relationship.id, fromJS(relationship));
  25. const normalizeRelationships = (state, relationships) => {
  26. relationships.forEach(relationship => {
  27. state = normalizeRelationship(state, relationship);
  28. });
  29. return state;
  30. };
  31. const setDomainBlocking = (state, accounts, blocking) => {
  32. return state.withMutations(map => {
  33. accounts.forEach(id => {
  34. map.setIn([id, 'domain_blocking'], blocking);
  35. });
  36. });
  37. };
  38. const initialState = ImmutableMap();
  39. export default function relationships(state = initialState, action) {
  40. switch(action.type) {
  41. case ACCOUNT_FOLLOW_REQUEST:
  42. return state.getIn([action.id, 'following']) ? state : state.setIn([action.id, action.locked ? 'requested' : 'following'], true);
  43. case ACCOUNT_FOLLOW_FAIL:
  44. return state.setIn([action.id, action.locked ? 'requested' : 'following'], false);
  45. case ACCOUNT_UNFOLLOW_REQUEST:
  46. return state.setIn([action.id, 'following'], false);
  47. case ACCOUNT_UNFOLLOW_FAIL:
  48. return state.setIn([action.id, 'following'], true);
  49. case ACCOUNT_FOLLOW_SUCCESS:
  50. case ACCOUNT_UNFOLLOW_SUCCESS:
  51. case ACCOUNT_BLOCK_SUCCESS:
  52. case ACCOUNT_UNBLOCK_SUCCESS:
  53. case ACCOUNT_MUTE_SUCCESS:
  54. case ACCOUNT_UNMUTE_SUCCESS:
  55. case ACCOUNT_PIN_SUCCESS:
  56. case ACCOUNT_UNPIN_SUCCESS:
  57. case ACCOUNT_NOTE_SUBMIT_SUCCESS:
  58. return normalizeRelationship(state, action.relationship);
  59. case RELATIONSHIPS_FETCH_SUCCESS:
  60. return normalizeRelationships(state, action.relationships);
  61. case DOMAIN_BLOCK_SUCCESS:
  62. return setDomainBlocking(state, action.accounts, true);
  63. case DOMAIN_UNBLOCK_SUCCESS:
  64. return setDomainBlocking(state, action.accounts, false);
  65. default:
  66. return state;
  67. }
  68. };