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.js 5.0KB

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