relationships.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { isFulfilled } from '@reduxjs/toolkit';
  2. import type { Reducer } from '@reduxjs/toolkit';
  3. import { Map as ImmutableMap } from 'immutable';
  4. import type { ApiRelationshipJSON } from 'mastodon/api_types/relationships';
  5. import type { Account } from 'mastodon/models/account';
  6. import { createRelationship } from 'mastodon/models/relationship';
  7. import type { Relationship } from 'mastodon/models/relationship';
  8. import { submitAccountNote } from '../actions/account_notes';
  9. import {
  10. followAccountSuccess,
  11. unfollowAccountSuccess,
  12. authorizeFollowRequestSuccess,
  13. rejectFollowRequestSuccess,
  14. followAccountRequest,
  15. followAccountFail,
  16. unfollowAccountRequest,
  17. unfollowAccountFail,
  18. blockAccountSuccess,
  19. unblockAccountSuccess,
  20. muteAccountSuccess,
  21. unmuteAccountSuccess,
  22. pinAccountSuccess,
  23. unpinAccountSuccess,
  24. fetchRelationshipsSuccess,
  25. } from '../actions/accounts_typed';
  26. import {
  27. blockDomainSuccess,
  28. unblockDomainSuccess,
  29. } from '../actions/domain_blocks_typed';
  30. import { notificationsUpdate } from '../actions/notifications_typed';
  31. const initialState = ImmutableMap<string, Relationship>();
  32. type State = typeof initialState;
  33. const normalizeRelationship = (
  34. state: State,
  35. relationship: ApiRelationshipJSON,
  36. ) => state.set(relationship.id, createRelationship(relationship));
  37. const normalizeRelationships = (
  38. state: State,
  39. relationships: ApiRelationshipJSON[],
  40. ) => {
  41. relationships.forEach((relationship) => {
  42. state = normalizeRelationship(state, relationship);
  43. });
  44. return state;
  45. };
  46. const setDomainBlocking = (
  47. state: State,
  48. accounts: Account[],
  49. blocking: boolean,
  50. ) => {
  51. return state.withMutations((map) => {
  52. accounts.forEach((id) => {
  53. map.setIn([id, 'domain_blocking'], blocking);
  54. });
  55. });
  56. };
  57. export const relationshipsReducer: Reducer<State> = (
  58. state = initialState,
  59. action,
  60. ) => {
  61. if (authorizeFollowRequestSuccess.match(action))
  62. return state
  63. .setIn([action.payload.id, 'followed_by'], true)
  64. .setIn([action.payload.id, 'requested_by'], false);
  65. else if (rejectFollowRequestSuccess.match(action))
  66. return state
  67. .setIn([action.payload.id, 'followed_by'], false)
  68. .setIn([action.payload.id, 'requested_by'], false);
  69. else if (notificationsUpdate.match(action))
  70. return action.payload.notification.type === 'follow_request'
  71. ? state.setIn(
  72. [action.payload.notification.account.id, 'requested_by'],
  73. true,
  74. )
  75. : state;
  76. else if (followAccountRequest.match(action))
  77. return state.getIn([action.payload.id, 'following'])
  78. ? state
  79. : state.setIn(
  80. [
  81. action.payload.id,
  82. action.payload.locked ? 'requested' : 'following',
  83. ],
  84. true,
  85. );
  86. else if (followAccountFail.match(action))
  87. return state.setIn(
  88. [action.payload.id, action.payload.locked ? 'requested' : 'following'],
  89. false,
  90. );
  91. else if (unfollowAccountRequest.match(action))
  92. return state.setIn([action.payload.id, 'following'], false);
  93. else if (unfollowAccountFail.match(action))
  94. return state.setIn([action.payload.id, 'following'], true);
  95. else if (
  96. followAccountSuccess.match(action) ||
  97. unfollowAccountSuccess.match(action) ||
  98. blockAccountSuccess.match(action) ||
  99. unblockAccountSuccess.match(action) ||
  100. muteAccountSuccess.match(action) ||
  101. unmuteAccountSuccess.match(action) ||
  102. pinAccountSuccess.match(action) ||
  103. unpinAccountSuccess.match(action) ||
  104. isFulfilled(submitAccountNote)(action)
  105. )
  106. return normalizeRelationship(state, action.payload.relationship);
  107. else if (fetchRelationshipsSuccess.match(action))
  108. return normalizeRelationships(state, action.payload.relationships);
  109. else if (blockDomainSuccess.match(action))
  110. return setDomainBlocking(state, action.payload.accounts, true);
  111. else if (unblockDomainSuccess.match(action))
  112. return setDomainBlocking(state, action.payload.accounts, false);
  113. else return state;
  114. };