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.

54 lines
1.5 KiB

  1. import {
  2. ACCOUNT_FOLLOW_SUCCESS,
  3. ACCOUNT_UNFOLLOW_SUCCESS,
  4. ACCOUNT_BLOCK_SUCCESS,
  5. ACCOUNT_UNBLOCK_SUCCESS,
  6. ACCOUNT_MUTE_SUCCESS,
  7. ACCOUNT_UNMUTE_SUCCESS,
  8. RELATIONSHIPS_FETCH_SUCCESS,
  9. } from 'flavours/glitch/actions/accounts';
  10. import {
  11. DOMAIN_BLOCK_SUCCESS,
  12. DOMAIN_UNBLOCK_SUCCESS,
  13. } from 'flavours/glitch/actions/domain_blocks';
  14. import { Map as ImmutableMap, fromJS } from 'immutable';
  15. const normalizeRelationship = (state, relationship) => state.set(relationship.id, fromJS(relationship));
  16. const normalizeRelationships = (state, relationships) => {
  17. relationships.forEach(relationship => {
  18. state = normalizeRelationship(state, relationship);
  19. });
  20. return state;
  21. };
  22. const setDomainBlocking = (state, accounts, blocking) => {
  23. return state.withMutations(map => {
  24. accounts.forEach(id => {
  25. map.setIn([id, 'domain_blocking'], blocking);
  26. });
  27. });
  28. };
  29. const initialState = ImmutableMap();
  30. export default function relationships(state = initialState, action) {
  31. switch(action.type) {
  32. case ACCOUNT_FOLLOW_SUCCESS:
  33. case ACCOUNT_UNFOLLOW_SUCCESS:
  34. case ACCOUNT_BLOCK_SUCCESS:
  35. case ACCOUNT_UNBLOCK_SUCCESS:
  36. case ACCOUNT_MUTE_SUCCESS:
  37. case ACCOUNT_UNMUTE_SUCCESS:
  38. return normalizeRelationship(state, action.relationship);
  39. case RELATIONSHIPS_FETCH_SUCCESS:
  40. return normalizeRelationships(state, action.relationships);
  41. case DOMAIN_BLOCK_SUCCESS:
  42. return setDomainBlocking(state, action.accounts, true);
  43. case DOMAIN_UNBLOCK_SUCCESS:
  44. return setDomainBlocking(state, action.accounts, false);
  45. default:
  46. return state;
  47. }
  48. };