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.

middleware.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // @flow
  2. import { CONFERENCE_WILL_JOIN } from '../base/conference';
  3. import {
  4. JitsiConferenceEvents,
  5. JitsiSIPVideoGWStatus
  6. } from '../base/lib-jitsi-meet';
  7. import { MiddlewareRegistry } from '../base/redux';
  8. import {
  9. NOTIFICATION_TIMEOUT_TYPE,
  10. showErrorNotification,
  11. showNotification,
  12. showWarningNotification
  13. } from '../notifications';
  14. import {
  15. SIP_GW_AVAILABILITY_CHANGED,
  16. SIP_GW_INVITE_ROOMS
  17. } from './actionTypes';
  18. import logger from './logger';
  19. /**
  20. * Middleware that captures conference video sip gw events and stores
  21. * the global sip gw availability in redux or show appropriate notification
  22. * for sip gw sessions.
  23. * Captures invitation actions that create sip gw sessions or display
  24. * appropriate error/warning notifications.
  25. *
  26. * @param {Store} store - The redux store.
  27. * @returns {Function}
  28. */
  29. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  30. const result = next(action);
  31. switch (action.type) {
  32. case CONFERENCE_WILL_JOIN: {
  33. const conference = getState()['features/base/conference'].joining;
  34. conference.on(
  35. JitsiConferenceEvents.VIDEO_SIP_GW_AVAILABILITY_CHANGED,
  36. (...args) => dispatch(_availabilityChanged(...args)));
  37. conference.on(
  38. JitsiConferenceEvents.VIDEO_SIP_GW_SESSION_STATE_CHANGED,
  39. event => {
  40. const toDispatch = _sessionStateChanged(event);
  41. // sessionStateChanged can decide there is nothing to dispatch
  42. if (toDispatch) {
  43. dispatch(toDispatch);
  44. }
  45. });
  46. break;
  47. }
  48. case SIP_GW_INVITE_ROOMS:
  49. _inviteRooms(action.rooms, action.conference, dispatch);
  50. break;
  51. }
  52. return result;
  53. });
  54. /**
  55. * Signals that sip gw availability had changed.
  56. *
  57. * @param {string} status - The new status of the service.
  58. * @returns {{
  59. * type: SIP_GW_AVAILABILITY_CHANGED,
  60. * status: string
  61. * }}
  62. * @private
  63. */
  64. function _availabilityChanged(status: string) {
  65. return {
  66. type: SIP_GW_AVAILABILITY_CHANGED,
  67. status
  68. };
  69. }
  70. /**
  71. * Processes the action from the actionType {@code SIP_GW_INVITE_ROOMS} by
  72. * inviting rooms into the conference or showing an error message.
  73. *
  74. * @param {Array} rooms - The conference rooms to invite.
  75. * @param {Object} conference - The JitsiConference to invite the rooms to.
  76. * @param {Function} dispatch - The redux dispatch function for emitting state
  77. * changes (queuing error notifications).
  78. * @private
  79. * @returns {void}
  80. */
  81. function _inviteRooms(rooms, conference, dispatch) {
  82. for (const room of rooms) {
  83. const { id: sipAddress, name: displayName } = room;
  84. if (sipAddress && displayName) {
  85. const newSession = conference
  86. .createVideoSIPGWSession(sipAddress, displayName);
  87. if (newSession instanceof Error) {
  88. const e = newSession;
  89. switch (e.message) {
  90. case JitsiSIPVideoGWStatus.ERROR_NO_CONNECTION: {
  91. dispatch(showErrorNotification({
  92. descriptionKey: 'videoSIPGW.errorInvite',
  93. titleKey: 'videoSIPGW.errorInviteTitle'
  94. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  95. return;
  96. }
  97. case JitsiSIPVideoGWStatus.ERROR_SESSION_EXISTS: {
  98. dispatch(showWarningNotification({
  99. titleKey: 'videoSIPGW.errorAlreadyInvited',
  100. titleArguments: { displayName }
  101. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  102. return;
  103. }
  104. }
  105. logger.error(
  106. 'Unknown error trying to create sip videogw session',
  107. e);
  108. return;
  109. }
  110. newSession.start();
  111. } else {
  112. logger.error(`No display name or sip number for ${
  113. JSON.stringify(room)}`);
  114. }
  115. }
  116. }
  117. /**
  118. * Signals that a session we created has a change in its status.
  119. *
  120. * @param {string} event - The event describing the session state change.
  121. * @returns {Object|null} - A notification action.
  122. * @private
  123. */
  124. function _sessionStateChanged(
  125. event: Object) {
  126. switch (event.newState) {
  127. case JitsiSIPVideoGWStatus.STATE_PENDING: {
  128. return showNotification({
  129. titleKey: 'videoSIPGW.pending',
  130. titleArguments: {
  131. displayName: event.displayName
  132. }
  133. }, NOTIFICATION_TIMEOUT_TYPE.SHORT);
  134. }
  135. case JitsiSIPVideoGWStatus.STATE_FAILED: {
  136. return showErrorNotification({
  137. titleKey: 'videoSIPGW.errorInviteFailedTitle',
  138. titleArguments: {
  139. displayName: event.displayName
  140. },
  141. descriptionKey: 'videoSIPGW.errorInviteFailed'
  142. }, NOTIFICATION_TIMEOUT_TYPE.LONG);
  143. }
  144. case JitsiSIPVideoGWStatus.STATE_OFF: {
  145. if (event.failureReason === JitsiSIPVideoGWStatus.STATUS_BUSY) {
  146. return showErrorNotification({
  147. descriptionKey: 'videoSIPGW.busy',
  148. titleKey: 'videoSIPGW.busyTitle'
  149. }, NOTIFICATION_TIMEOUT_TYPE.LONG);
  150. } else if (event.failureReason) {
  151. logger.error(`Unknown sip videogw error ${event.newState} ${
  152. event.failureReason}`);
  153. }
  154. }
  155. }
  156. // nothing to show
  157. return null;
  158. }