Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

actions.js 13KB

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