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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. * @returns {Function}
  176. */
  177. export function joinConference() {
  178. return {
  179. type: PREJOIN_START_CONFERENCE
  180. };
  181. }
  182. /**
  183. * Joins the conference without audio.
  184. *
  185. * @returns {Function}
  186. */
  187. export function joinConferenceWithoutAudio() {
  188. return async function(dispatch: Function, getState: Function) {
  189. const tracks = getState()['features/base/tracks'];
  190. const audioTrack = getLocalAudioTrack(tracks)?.jitsiTrack;
  191. if (audioTrack) {
  192. await dispatch(replaceLocalTrack(audioTrack, null));
  193. }
  194. dispatch(joinConference());
  195. };
  196. }
  197. /**
  198. * Opens an external page with all the dial in numbers.
  199. *
  200. * @returns {Function}
  201. */
  202. export function openDialInPage() {
  203. return function(dispatch: Function, getState: Function) {
  204. const state = getState();
  205. const locationURL = state['features/base/connection'].locationURL;
  206. const roomName = getRoomName(state);
  207. const dialInPage = getDialInfoPageURL(roomName, locationURL);
  208. openURLInBrowser(dialInPage, true);
  209. };
  210. }
  211. /**
  212. * Creates a new audio track based on a device id and replaces the current one.
  213. *
  214. * @param {string} deviceId - The deviceId of the microphone.
  215. * @returns {Function}
  216. */
  217. export function replaceAudioTrackById(deviceId: string) {
  218. return async (dispatch: Function, getState: Function) => {
  219. try {
  220. const tracks = getState()['features/base/tracks'];
  221. const newTrack = await createLocalTrack('audio', deviceId);
  222. const oldTrack = getLocalAudioTrack(tracks)?.jitsiTrack;
  223. dispatch(replaceLocalTrack(oldTrack, newTrack));
  224. } catch (err) {
  225. dispatch(setDeviceStatusWarning('prejoin.audioTrackError'));
  226. logger.log('Error replacing audio track', err);
  227. }
  228. };
  229. }
  230. /**
  231. * Creates a new video track based on a device id and replaces the current one.
  232. *
  233. * @param {string} deviceId - The deviceId of the camera.
  234. * @returns {Function}
  235. */
  236. export function replaceVideoTrackById(deviceId: Object) {
  237. return async (dispatch: Function, getState: Function) => {
  238. try {
  239. const tracks = getState()['features/base/tracks'];
  240. const newTrack = await createLocalTrack('video', deviceId);
  241. const oldTrack = getLocalVideoTrack(tracks)?.jitsiTrack;
  242. dispatch(replaceLocalTrack(oldTrack, newTrack));
  243. } catch (err) {
  244. dispatch(setDeviceStatusWarning('prejoin.videoTrackError'));
  245. logger.log('Error replacing video track', err);
  246. }
  247. };
  248. }
  249. /**
  250. * Sets the device status as OK with the corresponding text.
  251. *
  252. * @param {string} deviceStatusText - The text to be set.
  253. * @returns {Object}
  254. */
  255. export function setDeviceStatusOk(deviceStatusText: string) {
  256. return {
  257. type: SET_DEVICE_STATUS,
  258. value: {
  259. deviceStatusText,
  260. deviceStatusType: 'ok'
  261. }
  262. };
  263. }
  264. /**
  265. * Sets the device status as 'warning' with the corresponding text.
  266. *
  267. * @param {string} deviceStatusText - The text to be set.
  268. * @returns {Object}
  269. */
  270. export function setDeviceStatusWarning(deviceStatusText: string) {
  271. return {
  272. type: SET_DEVICE_STATUS,
  273. value: {
  274. deviceStatusText,
  275. deviceStatusType: 'warning'
  276. }
  277. };
  278. }
  279. /**
  280. * Action used to set the dial out status.
  281. *
  282. * @param {string} value - The status.
  283. * @returns {Object}
  284. */
  285. function setDialOutStatus(value: string) {
  286. return {
  287. type: SET_DIALOUT_STATUS,
  288. value
  289. };
  290. }
  291. /**
  292. * Action used to set the dial out country.
  293. *
  294. * @param {{ name: string, dialCode: string, code: string }} value - The country.
  295. * @returns {Object}
  296. */
  297. export function setDialOutCountry(value: Object) {
  298. return {
  299. type: SET_DIALOUT_COUNTRY,
  300. value
  301. };
  302. }
  303. /**
  304. * Action used to set the stance of the display name.
  305. *
  306. * @returns {Object}
  307. */
  308. export function setPrejoinDisplayNameRequired() {
  309. return {
  310. type: SET_PREJOIN_DISPLAY_NAME_REQUIRED
  311. };
  312. }
  313. /**
  314. * Action used to set the dial out number.
  315. *
  316. * @param {string} value - The dial out number.
  317. * @returns {Object}
  318. */
  319. export function setDialOutNumber(value: string) {
  320. return {
  321. type: SET_DIALOUT_NUMBER,
  322. value
  323. };
  324. }
  325. /**
  326. * Sets the visibility of the prejoin page for future uses.
  327. *
  328. * @param {boolean} value - The visibility value.
  329. * @returns {Object}
  330. */
  331. export function setSkipPrejoin(value: boolean) {
  332. return {
  333. type: SET_SKIP_PREJOIN,
  334. value
  335. };
  336. }
  337. /**
  338. * Action used to set the visiblitiy of the 'JoinByPhoneDialog'.
  339. *
  340. * @param {boolean} value - The value.
  341. * @returns {Object}
  342. */
  343. export function setJoinByPhoneDialogVisiblity(value: boolean) {
  344. return {
  345. type: SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  346. value
  347. };
  348. }
  349. /**
  350. * Action used to set the initial errors after creating the tracks.
  351. *
  352. * @param {Object} value - The track errors.
  353. * @returns {Object}
  354. */
  355. export function setPrejoinDeviceErrors(value: Object) {
  356. return {
  357. type: SET_PREJOIN_DEVICE_ERRORS,
  358. value
  359. };
  360. }
  361. /**
  362. * Action used to set the visiblity of the prejoin page.
  363. *
  364. * @param {boolean} value - The value.
  365. * @returns {Object}
  366. */
  367. export function setPrejoinPageVisibility(value: boolean) {
  368. return {
  369. type: SET_PREJOIN_PAGE_VISIBILITY,
  370. value
  371. };
  372. }