Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* @flow */
  2. import Logger from 'jitsi-meet-logger';
  3. import { CONFERENCE_WILL_JOIN } from '../base/conference';
  4. import {
  5. SIP_GW_AVAILABILITY_CHANGED,
  6. SIP_GW_INVITE_ROOMS
  7. } from './actionTypes';
  8. import {
  9. JitsiConferenceEvents,
  10. JitsiSIPVideoGWStatus
  11. } from '../base/lib-jitsi-meet';
  12. import { MiddlewareRegistry } from '../base/redux';
  13. import {
  14. showErrorNotification,
  15. showNotification,
  16. showWarningNotification
  17. } from '../notifications';
  18. const logger = Logger.getLogger(__filename);
  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. const { status } = getState()['features/videosipgw'];
  50. if (status === JitsiSIPVideoGWStatus.STATUS_UNDEFINED) {
  51. dispatch(showErrorNotification({
  52. descriptionKey: 'recording.unavailable',
  53. descriptionArguments: {
  54. serviceName: '$t(videoSIPGW.serviceName)'
  55. },
  56. titleKey: 'videoSIPGW.unavailableTitle'
  57. }));
  58. return;
  59. } else if (status === JitsiSIPVideoGWStatus.STATUS_BUSY) {
  60. dispatch(showWarningNotification({
  61. descriptionKey: 'videoSIPGW.busy',
  62. titleKey: 'videoSIPGW.busyTitle'
  63. }));
  64. return;
  65. } else if (status !== JitsiSIPVideoGWStatus.STATUS_AVAILABLE) {
  66. logger.error(`Unknown sip videogw status ${status}`);
  67. return;
  68. }
  69. for (const room of action.rooms) {
  70. const { id: sipAddress, name: displayName } = room;
  71. if (sipAddress && displayName) {
  72. const newSession = action.conference
  73. .createVideoSIPGWSession(sipAddress, displayName);
  74. if (newSession instanceof Error) {
  75. const e = newSession;
  76. if (e) {
  77. switch (e.message) {
  78. case JitsiSIPVideoGWStatus.ERROR_NO_CONNECTION: {
  79. dispatch(showErrorNotification({
  80. descriptionKey: 'videoSIPGW.errorInvite',
  81. titleKey: 'videoSIPGW.errorInviteTitle'
  82. }));
  83. return;
  84. }
  85. case JitsiSIPVideoGWStatus.ERROR_SESSION_EXISTS: {
  86. dispatch(showWarningNotification({
  87. titleKey: 'videoSIPGW.errorAlreadyInvited',
  88. titleArguments: { displayName }
  89. }));
  90. return;
  91. }
  92. }
  93. }
  94. logger.error(
  95. 'Unknown error trying to create sip videogw session',
  96. e);
  97. return;
  98. }
  99. newSession.start();
  100. } else {
  101. logger.error(`No display name or sip number for ${
  102. JSON.stringify(room)}`);
  103. }
  104. }
  105. }
  106. }
  107. return result;
  108. });
  109. /**
  110. * Signals that sip gw availability had changed.
  111. *
  112. * @param {string} status - The new status of the service.
  113. * @returns {{
  114. * type: SIP_GW_AVAILABILITY_CHANGED,
  115. * status: string
  116. * }}
  117. * @private
  118. */
  119. function _availabilityChanged(status: string) {
  120. return {
  121. type: SIP_GW_AVAILABILITY_CHANGED,
  122. status
  123. };
  124. }
  125. /**
  126. * Signals that a session we created has a change in its status.
  127. *
  128. * @param {string} event - The event describing the session state change.
  129. * @returns {{
  130. * type: SHOW_NOTIFICATION
  131. * }}|null
  132. * @private
  133. */
  134. function _sessionStateChanged(
  135. event: Object) {
  136. switch (event.newState) {
  137. case JitsiSIPVideoGWStatus.STATE_PENDING: {
  138. return showNotification({
  139. titleKey: 'videoSIPGW.pending',
  140. titleArguments: {
  141. displayName: event.displayName
  142. }
  143. }, 2000);
  144. }
  145. case JitsiSIPVideoGWStatus.STATE_FAILED: {
  146. return showErrorNotification({
  147. titleKey: 'videoSIPGW.errorInviteFailedTitle',
  148. titleArguments: {
  149. displayName: event.displayName
  150. },
  151. descriptionKey: 'videoSIPGW.errorInviteFailed'
  152. });
  153. }
  154. }
  155. // nothing to show
  156. return null;
  157. }