您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // @flow
  2. declare var JitsiMeetJS: Object;
  3. import uuid from 'uuid';
  4. import { getDialOutStatusUrl, getDialOutUrl } from '../base/config/functions';
  5. import { createLocalTrack } from '../base/lib-jitsi-meet';
  6. import {
  7. getLocalAudioTrack,
  8. getLocalVideoTrack,
  9. trackAdded,
  10. replaceLocalTrack
  11. } from '../base/tracks';
  12. import { openURLInBrowser } from '../base/util';
  13. import { executeDialOutRequest, executeDialOutStatusRequest, getDialInfoPageURL } from '../invite/functions';
  14. import { showErrorNotification } from '../notifications';
  15. import {
  16. PREJOIN_INITIALIZED,
  17. PREJOIN_START_CONFERENCE,
  18. SET_DEVICE_STATUS,
  19. SET_DIALOUT_COUNTRY,
  20. SET_DIALOUT_NUMBER,
  21. SET_DIALOUT_STATUS,
  22. SET_PREJOIN_DISPLAY_NAME_REQUIRED,
  23. SET_SKIP_PREJOIN,
  24. SET_SKIP_PREJOIN_RELOAD,
  25. SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  26. SET_PRECALL_TEST_RESULTS,
  27. SET_PREJOIN_DEVICE_ERRORS,
  28. SET_PREJOIN_PAGE_VISIBILITY
  29. } from './actionTypes';
  30. import {
  31. getFullDialOutNumber,
  32. getDialOutConferenceUrl,
  33. getDialOutCountry,
  34. isJoinByPhoneDialogVisible
  35. } from './functions';
  36. import logger from './logger';
  37. const dialOutStatusToKeyMap = {
  38. INITIATED: 'presenceStatus.calling',
  39. RINGING: 'presenceStatus.ringing'
  40. };
  41. const DIAL_OUT_STATUS = {
  42. INITIATED: 'INITIATED',
  43. RINGING: 'RINGING',
  44. CONNECTED: 'CONNECTED',
  45. DISCONNECTED: 'DISCONNECTED',
  46. FAILED: 'FAILED'
  47. };
  48. /**
  49. * The time interval used between requests while polling for dial out status.
  50. */
  51. const STATUS_REQ_FREQUENCY = 2000;
  52. /**
  53. * The maximum number of retries while polling for dial out status.
  54. */
  55. const STATUS_REQ_CAP = 45;
  56. /**
  57. * Polls for status change after dial out.
  58. * Changes dialog message based on response, closes the dialog if there is an error,
  59. * joins the meeting when CONNECTED.
  60. *
  61. * @param {string} reqId - The request id used to correlate the dial out request with this one.
  62. * @param {Function} onSuccess - Success handler.
  63. * @param {Function} onFail - Fail handler.
  64. * @param {number} count - The number of retried calls. When it hits STATUS_REQ_CAP it should no longer make requests.
  65. * @returns {Function}
  66. */
  67. function pollForStatus(
  68. reqId: string,
  69. onSuccess: Function,
  70. onFail: Function,
  71. count = 0) {
  72. return async function(dispatch: Function, getState: Function) {
  73. const state = getState();
  74. try {
  75. if (!isJoinByPhoneDialogVisible(state)) {
  76. return;
  77. }
  78. const res = await executeDialOutStatusRequest(getDialOutStatusUrl(state), reqId);
  79. switch (res) {
  80. case DIAL_OUT_STATUS.INITIATED:
  81. case DIAL_OUT_STATUS.RINGING: {
  82. dispatch(setDialOutStatus(dialOutStatusToKeyMap[res]));
  83. if (count < STATUS_REQ_CAP) {
  84. return setTimeout(() => {
  85. dispatch(pollForStatus(reqId, onSuccess, onFail, count + 1));
  86. }, STATUS_REQ_FREQUENCY);
  87. }
  88. return onFail();
  89. }
  90. case DIAL_OUT_STATUS.CONNECTED: {
  91. return onSuccess();
  92. }
  93. case DIAL_OUT_STATUS.DISCONNECTED: {
  94. dispatch(showErrorNotification({
  95. titleKey: 'prejoin.errorDialOutDisconnected'
  96. }));
  97. return onFail();
  98. }
  99. case DIAL_OUT_STATUS.FAILED: {
  100. dispatch(showErrorNotification({
  101. titleKey: 'prejoin.errorDialOutFailed'
  102. }));
  103. return onFail();
  104. }
  105. }
  106. } catch (err) {
  107. dispatch(showErrorNotification({
  108. titleKey: 'prejoin.errorDialOutStatus'
  109. }));
  110. logger.error('Error getting dial out status', err);
  111. onFail();
  112. }
  113. };
  114. }
  115. /**
  116. * Action used for joining the meeting with phone audio.
  117. * A dial out connection is tried and a polling mechanism is used for getting the status.
  118. * If the connection succeeds the `onSuccess` callback is executed.
  119. * If the phone connection fails or the number is invalid the `onFail` callback is executed.
  120. *
  121. * @param {Function} onSuccess - Success handler.
  122. * @param {Function} onFail - Fail handler.
  123. * @returns {Function}
  124. */
  125. export function dialOut(onSuccess: Function, onFail: Function) {
  126. return async function(dispatch: Function, getState: Function) {
  127. const state = getState();
  128. const reqId = uuid.v4();
  129. const url = getDialOutUrl(state);
  130. const conferenceUrl = getDialOutConferenceUrl(state);
  131. const phoneNumber = getFullDialOutNumber(state);
  132. const countryCode = getDialOutCountry(state).code.toUpperCase();
  133. const body = {
  134. conferenceUrl,
  135. countryCode,
  136. name: phoneNumber,
  137. phoneNumber
  138. };
  139. try {
  140. await executeDialOutRequest(url, body, reqId);
  141. dispatch(pollForStatus(reqId, onSuccess, onFail));
  142. } catch (err) {
  143. const notification = {
  144. titleKey: 'prejoin.errorDialOut',
  145. titleArguments: undefined
  146. };
  147. if (err.status) {
  148. if (err.messageKey === 'validation.failed') {
  149. notification.titleKey = 'prejoin.errorValidation';
  150. } else {
  151. notification.titleKey = 'prejoin.errorStatusCode';
  152. notification.titleArguments = { status: err.status };
  153. }
  154. }
  155. dispatch(showErrorNotification(notification));
  156. logger.error('Error dialing out', err);
  157. onFail();
  158. }
  159. };
  160. }
  161. /**
  162. * Adds all the newly created tracks to store on init.
  163. *
  164. * @param {Object[]} tracks - The newly created tracks.
  165. * @param {Object} errors - The errors from creating the tracks.
  166. *
  167. * @returns {Function}
  168. */
  169. export function initPrejoin(tracks: Object[], errors: Object) {
  170. return async function(dispatch: Function) {
  171. dispatch(setPrejoinDeviceErrors(errors));
  172. dispatch(prejoinInitialized());
  173. tracks.forEach(track => dispatch(trackAdded(track)));
  174. };
  175. }
  176. /**
  177. * Action used to start the conference.
  178. *
  179. * @param {Object} options - The config options that override the default ones (if any).
  180. * @returns {Function}
  181. */
  182. export function joinConference(options?: Object) {
  183. return {
  184. type: PREJOIN_START_CONFERENCE,
  185. options
  186. };
  187. }
  188. /**
  189. * Joins the conference without audio.
  190. *
  191. * @returns {Function}
  192. */
  193. export function joinConferenceWithoutAudio() {
  194. return async function(dispatch: Function, getState: Function) {
  195. const tracks = getState()['features/base/tracks'];
  196. const audioTrack = getLocalAudioTrack(tracks)?.jitsiTrack;
  197. if (audioTrack) {
  198. await dispatch(replaceLocalTrack(audioTrack, null));
  199. }
  200. dispatch(joinConference({
  201. startSilent: true
  202. }));
  203. };
  204. }
  205. /**
  206. * Initializes the 'precallTest' and executes one test, storing the results.
  207. *
  208. * @param {Object} conferenceOptions - The conference options.
  209. * @returns {Function}
  210. */
  211. export function makePrecallTest(conferenceOptions: Object) {
  212. return async function(dispatch: Function) {
  213. try {
  214. await JitsiMeetJS.precallTest.init(conferenceOptions);
  215. const results = await JitsiMeetJS.precallTest.execute();
  216. dispatch(setPrecallTestResults(results));
  217. } catch (error) {
  218. logger.debug('Failed to execute pre call test - ', error);
  219. }
  220. };
  221. }
  222. /**
  223. * Opens an external page with all the dial in numbers.
  224. *
  225. * @returns {Function}
  226. */
  227. export function openDialInPage() {
  228. return function(dispatch: Function, getState: Function) {
  229. const dialInPage = getDialInfoPageURL(getState());
  230. openURLInBrowser(dialInPage, true);
  231. };
  232. }
  233. /**
  234. * Action used to signal that the prejoin page has been initialized.
  235. *
  236. * @returns {Object}
  237. */
  238. function prejoinInitialized() {
  239. return {
  240. type: PREJOIN_INITIALIZED
  241. };
  242. }
  243. /**
  244. * Creates a new audio track based on a device id and replaces the current one.
  245. *
  246. * @param {string} deviceId - The deviceId of the microphone.
  247. * @returns {Function}
  248. */
  249. export function replaceAudioTrackById(deviceId: string) {
  250. return async (dispatch: Function, getState: Function) => {
  251. try {
  252. const tracks = getState()['features/base/tracks'];
  253. const newTrack = await createLocalTrack('audio', deviceId);
  254. const oldTrack = getLocalAudioTrack(tracks)?.jitsiTrack;
  255. dispatch(replaceLocalTrack(oldTrack, newTrack));
  256. } catch (err) {
  257. dispatch(setDeviceStatusWarning('prejoin.audioTrackError'));
  258. logger.log('Error replacing audio track', err);
  259. }
  260. };
  261. }
  262. /**
  263. * Creates a new video track based on a device id and replaces the current one.
  264. *
  265. * @param {string} deviceId - The deviceId of the camera.
  266. * @returns {Function}
  267. */
  268. export function replaceVideoTrackById(deviceId: Object) {
  269. return async (dispatch: Function, getState: Function) => {
  270. try {
  271. const tracks = getState()['features/base/tracks'];
  272. const newTrack = await createLocalTrack('video', deviceId);
  273. const oldTrack = getLocalVideoTrack(tracks)?.jitsiTrack;
  274. dispatch(replaceLocalTrack(oldTrack, newTrack));
  275. } catch (err) {
  276. dispatch(setDeviceStatusWarning('prejoin.videoTrackError'));
  277. logger.log('Error replacing video track', err);
  278. }
  279. };
  280. }
  281. /**
  282. * Sets the device status as OK with the corresponding text.
  283. *
  284. * @param {string} deviceStatusText - The text to be set.
  285. * @returns {Object}
  286. */
  287. export function setDeviceStatusOk(deviceStatusText: string) {
  288. return {
  289. type: SET_DEVICE_STATUS,
  290. value: {
  291. deviceStatusText,
  292. deviceStatusType: 'ok'
  293. }
  294. };
  295. }
  296. /**
  297. * Sets the device status as 'warning' with the corresponding text.
  298. *
  299. * @param {string} deviceStatusText - The text to be set.
  300. * @returns {Object}
  301. */
  302. export function setDeviceStatusWarning(deviceStatusText: string) {
  303. return {
  304. type: SET_DEVICE_STATUS,
  305. value: {
  306. deviceStatusText,
  307. deviceStatusType: 'warning'
  308. }
  309. };
  310. }
  311. /**
  312. * Action used to set the dial out status.
  313. *
  314. * @param {string} value - The status.
  315. * @returns {Object}
  316. */
  317. function setDialOutStatus(value: string) {
  318. return {
  319. type: SET_DIALOUT_STATUS,
  320. value
  321. };
  322. }
  323. /**
  324. * Action used to set the dial out country.
  325. *
  326. * @param {{ name: string, dialCode: string, code: string }} value - The country.
  327. * @returns {Object}
  328. */
  329. export function setDialOutCountry(value: Object) {
  330. return {
  331. type: SET_DIALOUT_COUNTRY,
  332. value
  333. };
  334. }
  335. /**
  336. * Action used to set the stance of the display name.
  337. *
  338. * @returns {Object}
  339. */
  340. export function setPrejoinDisplayNameRequired() {
  341. return {
  342. type: SET_PREJOIN_DISPLAY_NAME_REQUIRED
  343. };
  344. }
  345. /**
  346. * Action used to set the dial out number.
  347. *
  348. * @param {string} value - The dial out number.
  349. * @returns {Object}
  350. */
  351. export function setDialOutNumber(value: string) {
  352. return {
  353. type: SET_DIALOUT_NUMBER,
  354. value
  355. };
  356. }
  357. /**
  358. * Sets the visibility of the prejoin page for future uses.
  359. *
  360. * @param {boolean} value - The visibility value.
  361. * @returns {Object}
  362. */
  363. export function setSkipPrejoin(value: boolean) {
  364. return {
  365. type: SET_SKIP_PREJOIN,
  366. value
  367. };
  368. }
  369. /**
  370. * Sets the visibility of the prejoin page when a client reload
  371. * is triggered as a result of call migration initiated by Jicofo.
  372. *
  373. * @param {boolean} value - The visibility value.
  374. * @returns {Object}
  375. */
  376. export function setSkipPrejoinOnReload(value: boolean) {
  377. return {
  378. type: SET_SKIP_PREJOIN_RELOAD,
  379. value
  380. };
  381. }
  382. /**
  383. * Action used to set the visiblitiy of the 'JoinByPhoneDialog'.
  384. *
  385. * @param {boolean} value - The value.
  386. * @returns {Object}
  387. */
  388. export function setJoinByPhoneDialogVisiblity(value: boolean) {
  389. return {
  390. type: SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  391. value
  392. };
  393. }
  394. /**
  395. * Action used to set data from precall test.
  396. *
  397. * @param {Object} value - The precall test results.
  398. * @returns {Object}
  399. */
  400. export function setPrecallTestResults(value: Object) {
  401. return {
  402. type: SET_PRECALL_TEST_RESULTS,
  403. value
  404. };
  405. }
  406. /**
  407. * Action used to set the initial errors after creating the tracks.
  408. *
  409. * @param {Object} value - The track errors.
  410. * @returns {Object}
  411. */
  412. export function setPrejoinDeviceErrors(value: Object) {
  413. return {
  414. type: SET_PREJOIN_DEVICE_ERRORS,
  415. value
  416. };
  417. }
  418. /**
  419. * Action used to set the visibility of the prejoin page.
  420. *
  421. * @param {boolean} value - The value.
  422. * @returns {Object}
  423. */
  424. export function setPrejoinPageVisibility(value: boolean) {
  425. return {
  426. type: SET_PREJOIN_PAGE_VISIBILITY,
  427. value
  428. };
  429. }