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.

actions.any.ts 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { IStore } from '../app/types';
  2. import { checkIfCanJoin } from '../base/conference/actions';
  3. import { IJitsiConference } from '../base/conference/reducer';
  4. import { openDialog } from '../base/dialog/actions';
  5. import {
  6. STOP_WAIT_FOR_OWNER,
  7. UPGRADE_ROLE_FINISHED,
  8. UPGRADE_ROLE_STARTED, WAIT_FOR_OWNER
  9. } from './actionTypes';
  10. import { LoginDialog, WaitForOwnerDialog } from './components';
  11. import logger from './logger';
  12. /**
  13. * Initiates authenticating and upgrading the role of the local participant to
  14. * moderator which will allow to create and join a new conference on an XMPP
  15. * password + guest access configuration. Refer to {@link LoginDialog} for more
  16. * info.
  17. *
  18. * @param {string} id - The XMPP user's ID (e.g. {@code user@domain.com}).
  19. * @param {string} password - The XMPP user's password.
  20. * @param {JitsiConference} conference - The conference for which the local
  21. * participant's role will be upgraded.
  22. * @returns {Function}
  23. */
  24. export function authenticateAndUpgradeRole(
  25. id: string,
  26. password: string,
  27. conference: IJitsiConference) {
  28. return (dispatch: IStore['dispatch']) => {
  29. const process
  30. = conference.authenticateAndUpgradeRole({
  31. id,
  32. password,
  33. onLoginSuccessful() {
  34. // When the login succeeds, the process has completed half
  35. // of its job (i.e. 0.5).
  36. return dispatch(_upgradeRoleFinished(process, 0.5));
  37. }
  38. });
  39. dispatch(_upgradeRoleStarted(process));
  40. process.then(
  41. /* onFulfilled */ () => dispatch(_upgradeRoleFinished(process, 1)),
  42. /* onRejected */ (error: any) => {
  43. // The lack of an error signals a cancellation.
  44. if (error.authenticationError || error.connectionError) {
  45. logger.error('authenticateAndUpgradeRole failed', error);
  46. }
  47. dispatch(_upgradeRoleFinished(process, error));
  48. });
  49. return process;
  50. };
  51. }
  52. /**
  53. * Signals that the process of authenticating and upgrading the local
  54. * participant's role has finished either with success or with a specific error.
  55. *
  56. * @param {Object} thenableWithCancel - The process of authenticating and
  57. * upgrading the local participant's role.
  58. * @param {Object} progressOrError - If the value is a {@code number}, then the
  59. * process of authenticating and upgrading the local participant's role has
  60. * succeeded in one of its two/multiple steps; otherwise, it has failed with the
  61. * specified error. Refer to {@link JitsiConference#authenticateAndUpgradeRole}
  62. * in lib-jitsi-meet for the error details.
  63. * @private
  64. * @returns {{
  65. * type: UPGRADE_ROLE_FINISHED,
  66. * error: ?Object,
  67. * progress: number
  68. * }}
  69. */
  70. function _upgradeRoleFinished(
  71. thenableWithCancel: Object,
  72. progressOrError: number | any) {
  73. let error;
  74. let progress;
  75. if (typeof progressOrError === 'number') {
  76. progress = progressOrError;
  77. } else {
  78. // Make the specified error object resemble an Error instance (to the
  79. // extent that jitsi-meet needs it).
  80. const {
  81. authenticationError,
  82. connectionError,
  83. ...other
  84. } = progressOrError;
  85. error = {
  86. name: authenticationError || connectionError,
  87. ...other
  88. };
  89. progress = 0;
  90. }
  91. return {
  92. type: UPGRADE_ROLE_FINISHED,
  93. error,
  94. progress,
  95. thenableWithCancel
  96. };
  97. }
  98. /**
  99. * Signals that a process of authenticating and upgrading the local
  100. * participant's role has started.
  101. *
  102. * @param {Object} thenableWithCancel - The process of authenticating and
  103. * upgrading the local participant's role.
  104. * @private
  105. * @returns {{
  106. * type: UPGRADE_ROLE_STARTED,
  107. * thenableWithCancel: Object
  108. * }}
  109. */
  110. function _upgradeRoleStarted(thenableWithCancel: Object) {
  111. return {
  112. type: UPGRADE_ROLE_STARTED,
  113. thenableWithCancel
  114. };
  115. }
  116. /**
  117. * Opens {@link WaitForOnwerDialog}.
  118. *
  119. * @protected
  120. * @returns {Action}
  121. */
  122. export function openWaitForOwnerDialog() {
  123. return openDialog(WaitForOwnerDialog);
  124. }
  125. /**
  126. * Stops waiting for the conference owner.
  127. *
  128. * @returns {{
  129. * type: STOP_WAIT_FOR_OWNER
  130. * }}
  131. */
  132. export function stopWaitForOwner() {
  133. return {
  134. type: STOP_WAIT_FOR_OWNER
  135. };
  136. }
  137. /**
  138. * Called when Jicofo rejects to create the room for anonymous user. Will
  139. * start the process of "waiting for the owner" by periodically trying to join
  140. * the room every five seconds.
  141. *
  142. * @returns {Function}
  143. */
  144. export function waitForOwner() {
  145. return (dispatch: IStore['dispatch']) =>
  146. dispatch({
  147. type: WAIT_FOR_OWNER,
  148. handler: () => dispatch(checkIfCanJoin()),
  149. timeoutMs: 5000
  150. });
  151. }
  152. /**
  153. * Opens {@link LoginDialog} which will ask to enter username and password
  154. * for the current conference.
  155. *
  156. * @protected
  157. * @returns {Action}
  158. */
  159. export function openLoginDialog() {
  160. return openDialog(LoginDialog);
  161. }