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.1KB

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