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.

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