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

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