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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { IStore } from '../app/types';
  2. import { checkIfCanJoin } from '../base/conference/actions';
  3. import { IJitsiConference } from '../base/conference/reducer';
  4. import { hideDialog, openDialog } from '../base/dialog/actions';
  5. import {
  6. LOGIN,
  7. LOGOUT,
  8. SET_TOKEN_AUTH_URL_SUCCESS,
  9. STOP_WAIT_FOR_OWNER,
  10. UPGRADE_ROLE_FINISHED,
  11. UPGRADE_ROLE_STARTED, WAIT_FOR_OWNER
  12. } from './actionTypes';
  13. import { LoginDialog, WaitForOwnerDialog } from './components';
  14. import logger from './logger';
  15. /**
  16. * Initiates authenticating and upgrading the role of the local participant to
  17. * moderator which will allow to create and join a new conference on an XMPP
  18. * password + guest access configuration. Refer to {@link LoginDialog} for more
  19. * info.
  20. *
  21. * @param {string} id - The XMPP user's ID (e.g. {@code user@domain.com}).
  22. * @param {string} password - The XMPP user's password.
  23. * @param {JitsiConference} conference - The conference for which the local
  24. * participant's role will be upgraded.
  25. * @returns {Function}
  26. */
  27. export function authenticateAndUpgradeRole(
  28. id: string,
  29. password: string,
  30. conference: IJitsiConference) {
  31. return (dispatch: IStore['dispatch']) => {
  32. const process
  33. = conference.authenticateAndUpgradeRole({
  34. id,
  35. password,
  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: any) => {
  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: Object,
  75. progressOrError: number | any) {
  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 = 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: Object) {
  114. return {
  115. type: UPGRADE_ROLE_STARTED,
  116. thenableWithCancel
  117. };
  118. }
  119. /**
  120. * Hides an authentication dialog where the local participant
  121. * should authenticate.
  122. *
  123. * @returns {Function}.
  124. */
  125. export function hideLoginDialog() {
  126. return hideDialog(LoginDialog);
  127. }
  128. /**
  129. * Login.
  130. *
  131. * @returns {{
  132. * type: LOGIN
  133. * }}
  134. */
  135. export function login() {
  136. return {
  137. type: LOGIN
  138. };
  139. }
  140. /**
  141. * Logout.
  142. *
  143. * @returns {{
  144. * type: LOGOUT
  145. * }}
  146. */
  147. export function logout() {
  148. return {
  149. type: LOGOUT
  150. };
  151. }
  152. /**
  153. * Opens {@link WaitForOnwerDialog}.
  154. *
  155. * @protected
  156. * @returns {Action}
  157. */
  158. export function openWaitForOwnerDialog() {
  159. return openDialog(WaitForOwnerDialog);
  160. }
  161. /**
  162. * Stops waiting for the conference owner.
  163. *
  164. * @returns {{
  165. * type: STOP_WAIT_FOR_OWNER
  166. * }}
  167. */
  168. export function stopWaitForOwner() {
  169. return {
  170. type: STOP_WAIT_FOR_OWNER
  171. };
  172. }
  173. /**
  174. * Called when Jicofo rejects to create the room for anonymous user. Will
  175. * start the process of "waiting for the owner" by periodically trying to join
  176. * the room every five seconds.
  177. *
  178. * @returns {Function}
  179. */
  180. export function waitForOwner() {
  181. return (dispatch: IStore['dispatch']) =>
  182. dispatch({
  183. type: WAIT_FOR_OWNER,
  184. handler: () => dispatch(checkIfCanJoin()),
  185. timeoutMs: 5000
  186. });
  187. }
  188. /**
  189. * Opens {@link LoginDialog} which will ask to enter username and password
  190. * for the current conference.
  191. *
  192. * @protected
  193. * @returns {Action}
  194. */
  195. export function openLoginDialog() {
  196. return openDialog(LoginDialog);
  197. }
  198. /**
  199. * Updates the config with new options.
  200. *
  201. * @param {boolean} value - The new value.
  202. * @returns {Function}
  203. */
  204. export function setTokenAuthUrlSuccess(value: boolean) {
  205. return {
  206. type: SET_TOKEN_AUTH_URL_SUCCESS,
  207. value
  208. };
  209. }