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_SKIP_PREJOIN,
  22. SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  23. SET_PREJOIN_DEVICE_ERRORS,
  24. SET_PREJOIN_PAGE_VISIBILITY
  25. } from './actionTypes';
  26. import {
  27. getFullDialOutNumber,
  28. getDialOutConferenceUrl,
  29. getDialOutCountry,
  30. isJoinByPhoneDialogVisible
  31. } from './functions';
  32. import logger from './logger';
  33. const dialOutStatusToKeyMap = {
  34. INITIATED: 'presenceStatus.calling',
  35. RINGING: 'presenceStatus.ringing'
  36. };
  37. const DIAL_OUT_STATUS = {
  38. INITIATED: 'INITIATED',
  39. RINGING: 'RINGING',
  40. CONNECTED: 'CONNECTED',
  41. DISCONNECTED: 'DISCONNECTED',
  42. FAILED: 'FAILED'
  43. };
  44. /**
  45. * The time interval used between requests while polling for dial out status.
  46. */
  47. const STATUS_REQ_FREQUENCY = 2000;
  48. /**
  49. * The maximum number of retries while polling for dial out status.
  50. */
  51. const STATUS_REQ_CAP = 45;
  52. /**
  53. * Polls for status change after dial out.
  54. * Changes dialog message based on response, closes the dialog if there is an error,
  55. * joins the meeting when CONNECTED.
  56. *
  57. * @param {string} reqId - The request id used to correlate the dial out request with this one.
  58. * @param {Function} onSuccess - Success handler.
  59. * @param {Function} onFail - Fail handler.
  60. * @param {number} count - The number of retried calls. When it hits STATUS_REQ_CAP it should no longer make requests.
  61. * @returns {Function}
  62. */
  63. function pollForStatus(
  64. reqId: string,
  65. onSuccess: Function,
  66. onFail: Function,
  67. count = 0) {
  68. return async function(dispatch: Function, getState: Function) {
  69. const state = getState();
  70. try {
  71. if (!isJoinByPhoneDialogVisible(state)) {
  72. return;
  73. }
  74. const res = await executeDialOutStatusRequest(getDialOutStatusUrl(state), reqId);
  75. switch (res) {
  76. case DIAL_OUT_STATUS.INITIATED:
  77. case DIAL_OUT_STATUS.RINGING: {
  78. dispatch(setDialOutStatus(dialOutStatusToKeyMap[res]));
  79. if (count < STATUS_REQ_CAP) {
  80. return setTimeout(() => {
  81. dispatch(pollForStatus(reqId, onSuccess, onFail, count + 1));
  82. }, STATUS_REQ_FREQUENCY);
  83. }
  84. return onFail();
  85. }
  86. case DIAL_OUT_STATUS.CONNECTED: {
  87. return onSuccess();
  88. }
  89. case DIAL_OUT_STATUS.DISCONNECTED: {
  90. dispatch(showErrorNotification({
  91. titleKey: 'prejoin.errorDialOutDisconnected'
  92. }));
  93. return onFail();
  94. }
  95. case DIAL_OUT_STATUS.FAILED: {
  96. dispatch(showErrorNotification({
  97. titleKey: 'prejoin.errorDialOutFailed'
  98. }));
  99. return onFail();
  100. }
  101. }
  102. } catch (err) {
  103. dispatch(showErrorNotification({
  104. titleKey: 'prejoin.errorDialOutStatus'
  105. }));
  106. logger.error('Error getting dial out status', err);
  107. onFail();
  108. }
  109. };
  110. }
  111. /**
  112. * Action used for joining the meeting with phone audio.
  113. * A dial out connection is tried and a polling mechanism is used for getting the status.
  114. * If the connection succeeds the `onSuccess` callback is executed.
  115. * If the phone connection fails or the number is invalid the `onFail` callback is executed.
  116. *
  117. * @param {Function} onSuccess - Success handler.
  118. * @param {Function} onFail - Fail handler.
  119. * @returns {Function}
  120. */
  121. export function dialOut(onSuccess: Function, onFail: Function) {
  122. return async function(dispatch: Function, getState: Function) {
  123. const state = getState();
  124. const reqId = uuid.v4();
  125. const url = getDialOutUrl(state);
  126. const conferenceUrl = getDialOutConferenceUrl(state);
  127. const phoneNumber = getFullDialOutNumber(state);
  128. const countryCode = getDialOutCountry(state).code.toUpperCase();
  129. const body = {
  130. conferenceUrl,
  131. countryCode,
  132. name: phoneNumber,
  133. phoneNumber
  134. };
  135. try {
  136. await executeDialOutRequest(url, body, reqId);
  137. dispatch(pollForStatus(reqId, onSuccess, onFail));
  138. } catch (err) {
  139. const notification = {
  140. titleKey: 'prejoin.errorDialOut',
  141. titleArguments: undefined
  142. };
  143. if (err.status) {
  144. if (err.messageKey === 'validation.failed') {
  145. notification.titleKey = 'prejoin.errorValidation';
  146. } else {
  147. notification.titleKey = 'prejoin.errorStatusCode';
  148. notification.titleArguments = { status: err.status };
  149. }
  150. }
  151. dispatch(showErrorNotification(notification));
  152. logger.error('Error dialing out', err);
  153. onFail();
  154. }
  155. };
  156. }
  157. /**
  158. * Adds all the newly created tracks to store on init.
  159. *
  160. * @param {Object[]} tracks - The newly created tracks.
  161. * @param {Object} errors - The errors from creating the tracks.
  162. *
  163. * @returns {Function}
  164. */
  165. export function initPrejoin(tracks: Object[], errors: Object) {
  166. return async function(dispatch: Function) {
  167. dispatch(setPrejoinDeviceErrors(errors));
  168. tracks.forEach(track => dispatch(trackAdded(track)));
  169. };
  170. }
  171. /**
  172. * Joins the conference.
  173. *
  174. * @returns {Function}
  175. */
  176. export function joinConference() {
  177. return function(dispatch: Function) {
  178. dispatch(setPrejoinPageVisibility(false));
  179. dispatch(startConference());
  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 dial out number.
  305. *
  306. * @param {string} value - The dial out number.
  307. * @returns {Object}
  308. */
  309. export function setDialOutNumber(value: string) {
  310. return {
  311. type: SET_DIALOUT_NUMBER,
  312. value
  313. };
  314. }
  315. /**
  316. * Sets the visibility of the prejoin page for future uses.
  317. *
  318. * @param {boolean} value - The visibility value.
  319. * @returns {Object}
  320. */
  321. export function setSkipPrejoin(value: boolean) {
  322. return {
  323. type: SET_SKIP_PREJOIN,
  324. value
  325. };
  326. }
  327. /**
  328. * Action used to set the visiblitiy of the 'JoinByPhoneDialog'.
  329. *
  330. * @param {boolean} value - The value.
  331. * @returns {Object}
  332. */
  333. export function setJoinByPhoneDialogVisiblity(value: boolean) {
  334. return {
  335. type: SET_JOIN_BY_PHONE_DIALOG_VISIBLITY,
  336. value
  337. };
  338. }
  339. /**
  340. * Action used to set the initial errors after creating the tracks.
  341. *
  342. * @param {Object} value - The track errors.
  343. * @returns {Object}
  344. */
  345. export function setPrejoinDeviceErrors(value: Object) {
  346. return {
  347. type: SET_PREJOIN_DEVICE_ERRORS,
  348. value
  349. };
  350. }
  351. /**
  352. * Action used to set the visiblity of the prejoin page.
  353. *
  354. * @param {boolean} value - The value.
  355. * @returns {Object}
  356. */
  357. export function setPrejoinPageVisibility(value: boolean) {
  358. return {
  359. type: SET_PREJOIN_PAGE_VISIBILITY,
  360. value
  361. };
  362. }
  363. /**
  364. * Action used to mark the start of the conference.
  365. *
  366. * @returns {Object}
  367. */
  368. function startConference() {
  369. return {
  370. type: PREJOIN_START_CONFERENCE
  371. };
  372. }