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.

reducer.ts 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import {
  2. CONFERENCE_FAILED,
  3. CONFERENCE_JOINED,
  4. CONFERENCE_LEFT,
  5. SET_PASSWORD
  6. } from '../base/conference/actionTypes';
  7. import { JitsiConferenceErrors } from '../base/lib-jitsi-meet';
  8. import ReducerRegistry from '../base/redux/ReducerRegistry';
  9. import {
  10. KNOCKING_PARTICIPANT_ARRIVED_OR_UPDATED,
  11. KNOCKING_PARTICIPANT_LEFT,
  12. REMOVE_LOBBY_CHAT_WITH_MODERATOR,
  13. SET_KNOCKING_STATE,
  14. SET_LOBBY_MODE_ENABLED,
  15. SET_LOBBY_PARTICIPANT_CHAT_STATE,
  16. SET_LOBBY_VISIBILITY,
  17. SET_PASSWORD_JOIN_FAILED
  18. } from './actionTypes';
  19. import { IKnockingParticipant } from './types';
  20. const DEFAULT_STATE = {
  21. isDisplayNameRequiredError: false,
  22. knocking: false,
  23. knockingParticipants: [],
  24. lobbyEnabled: false,
  25. lobbyVisible: false,
  26. passwordJoinFailed: false
  27. };
  28. export interface ILobbyState {
  29. /**
  30. * A conference error when we tried to join into a room with no display name
  31. * when lobby is enabled in the room.
  32. */
  33. isDisplayNameRequiredError: boolean;
  34. knocking: boolean;
  35. knockingParticipants: IKnockingParticipant[];
  36. lobbyEnabled: boolean;
  37. lobbyVisible: boolean;
  38. passwordJoinFailed: boolean;
  39. }
  40. /**
  41. * Reduces redux actions which affect the display of notifications.
  42. *
  43. * @param {Object} state - The current redux state.
  44. * @param {Object} action - The redux action to reduce.
  45. * @returns {Object} The next redux state which is the result of reducing the
  46. * specified {@code action}.
  47. */
  48. ReducerRegistry.register<ILobbyState>('features/lobby', (state = DEFAULT_STATE, action): ILobbyState => {
  49. switch (action.type) {
  50. case CONFERENCE_FAILED: {
  51. if (action.error.name === JitsiConferenceErrors.DISPLAY_NAME_REQUIRED) {
  52. return {
  53. ...state,
  54. isDisplayNameRequiredError: true
  55. };
  56. }
  57. return {
  58. ...state,
  59. knocking: false
  60. };
  61. }
  62. case CONFERENCE_JOINED:
  63. case CONFERENCE_LEFT:
  64. return {
  65. ...state,
  66. knocking: false,
  67. passwordJoinFailed: false
  68. };
  69. case KNOCKING_PARTICIPANT_ARRIVED_OR_UPDATED:
  70. return _knockingParticipantArrivedOrUpdated(action.participant, state);
  71. case KNOCKING_PARTICIPANT_LEFT:
  72. return {
  73. ...state,
  74. knockingParticipants: state.knockingParticipants.filter(p => p.id !== action.id)
  75. };
  76. case SET_KNOCKING_STATE:
  77. return {
  78. ...state,
  79. knocking: action.knocking,
  80. passwordJoinFailed: false
  81. };
  82. case SET_LOBBY_MODE_ENABLED:
  83. return {
  84. ...state,
  85. lobbyEnabled: action.enabled
  86. };
  87. case SET_LOBBY_VISIBILITY:
  88. return {
  89. ...state,
  90. lobbyVisible: action.visible
  91. };
  92. case SET_PASSWORD:
  93. return {
  94. ...state,
  95. passwordJoinFailed: false
  96. };
  97. case SET_PASSWORD_JOIN_FAILED:
  98. return {
  99. ...state,
  100. passwordJoinFailed: action.failed
  101. };
  102. case SET_LOBBY_PARTICIPANT_CHAT_STATE:
  103. return {
  104. ...state,
  105. knockingParticipants: state.knockingParticipants.map(participant => {
  106. if (participant.id === action.participant.id) {
  107. return {
  108. ...participant,
  109. chattingWithModerator: action.moderator.id
  110. };
  111. }
  112. return participant;
  113. })
  114. };
  115. case REMOVE_LOBBY_CHAT_WITH_MODERATOR:
  116. return {
  117. ...state,
  118. knockingParticipants: state.knockingParticipants.map(participant => {
  119. if (participant.chattingWithModerator === action.moderatorId) {
  120. return {
  121. ...participant,
  122. chattingWithModerator: undefined
  123. };
  124. }
  125. return participant;
  126. })
  127. };
  128. }
  129. return state;
  130. });
  131. /**
  132. * Stores or updates a knocking participant.
  133. *
  134. * @param {Object} participant - The arrived or updated knocking participant.
  135. * @param {Object} state - The current Redux state of the feature.
  136. * @returns {Object}
  137. */
  138. function _knockingParticipantArrivedOrUpdated(participant: IKnockingParticipant, state: ILobbyState) {
  139. let existingParticipant = state.knockingParticipants.find(p => p.id === participant.id);
  140. existingParticipant = {
  141. ...existingParticipant,
  142. ...participant
  143. };
  144. return {
  145. ...state,
  146. knockingParticipants: [
  147. ...state.knockingParticipants.filter(p => p.id !== participant.id),
  148. existingParticipant
  149. ]
  150. };
  151. }