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

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