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.any.ts 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import { IStore } from '../app/types';
  2. import { getInviteURL } from '../base/connection/functions';
  3. import { getLocalParticipant, getParticipantCount } from '../base/participants/functions';
  4. import { inviteVideoRooms } from '../videosipgw/actions';
  5. import { getDialInConferenceID, getDialInNumbers } from './_utils';
  6. import {
  7. ADD_PENDING_INVITE_REQUEST,
  8. BEGIN_ADD_PEOPLE,
  9. HIDE_ADD_PEOPLE_DIALOG,
  10. REMOVE_PENDING_INVITE_REQUESTS,
  11. SET_CALLEE_INFO_VISIBLE,
  12. UPDATE_DIAL_IN_NUMBERS_FAILED,
  13. UPDATE_DIAL_IN_NUMBERS_SUCCESS
  14. } from './actionTypes';
  15. import { INVITE_TYPES } from './constants';
  16. import {
  17. invitePeopleAndChatRooms,
  18. inviteSipEndpoints
  19. } from './functions';
  20. import logger from './logger';
  21. import { IInvitee } from './types';
  22. /**
  23. * Creates a (redux) action to signal that a click/tap has been performed on
  24. * {@link InviteButton} and that the execution flow for adding/inviting people
  25. * to the current conference/meeting is to begin.
  26. *
  27. * @returns {{
  28. * type: BEGIN_ADD_PEOPLE
  29. * }}
  30. */
  31. export function beginAddPeople() {
  32. return {
  33. type: BEGIN_ADD_PEOPLE
  34. };
  35. }
  36. /**
  37. * Creates a (redux) action to signal that the {@code AddPeopleDialog}
  38. * should close.
  39. *
  40. * @returns {{
  41. * type: HIDE_ADD_PEOPLE_DIALOG
  42. * }}
  43. */
  44. export function hideAddPeopleDialog() {
  45. return {
  46. type: HIDE_ADD_PEOPLE_DIALOG
  47. };
  48. }
  49. /**
  50. * Invites (i.e. Sends invites to) an array of invitees (which may be a
  51. * combination of users, rooms, phone numbers, and video rooms.
  52. *
  53. * @param {Array<Object>} invitees - The recipients to send invites to.
  54. * @param {Array<Object>} showCalleeInfo - Indicates whether the
  55. * {@code CalleeInfo} should be displayed or not.
  56. * @returns {Promise<Array<Object>>} A {@code Promise} resolving with an array
  57. * of invitees who were not invited (i.e. Invites were not sent to them).
  58. */
  59. export function invite(
  60. invitees: IInvitee[],
  61. showCalleeInfo = false) {
  62. return (
  63. dispatch: IStore['dispatch'],
  64. getState: IStore['getState']): Promise<Array<Object>> => {
  65. const state = getState();
  66. const participantsCount = getParticipantCount(state);
  67. const { calleeInfoVisible } = state['features/invite'];
  68. if (showCalleeInfo
  69. && !calleeInfoVisible
  70. && invitees.length === 1
  71. && invitees[0].type === INVITE_TYPES.USER
  72. && participantsCount === 1) {
  73. dispatch(setCalleeInfoVisible(true, invitees[0]));
  74. }
  75. const { conference, password } = state['features/base/conference'];
  76. if (typeof conference === 'undefined') {
  77. // Invite will fail before CONFERENCE_JOIN. The request will be
  78. // cached in order to be executed on CONFERENCE_JOIN.
  79. return new Promise(resolve => {
  80. dispatch(addPendingInviteRequest({
  81. invitees,
  82. callback: (failedInvitees: any) => resolve(failedInvitees)
  83. }));
  84. });
  85. }
  86. let allInvitePromises: Promise<any>[] = [];
  87. let invitesLeftToSend = [ ...invitees ];
  88. const {
  89. callFlowsEnabled,
  90. inviteServiceUrl,
  91. inviteServiceCallFlowsUrl
  92. } = state['features/base/config'];
  93. const inviteUrl = getInviteURL(state);
  94. const { sipInviteUrl } = state['features/base/config'];
  95. const { locationURL } = state['features/base/connection'];
  96. const { jwt = '' } = state['features/base/jwt'];
  97. const { name: displayName } = getLocalParticipant(state) ?? {};
  98. // First create all promises for dialing out.
  99. const phoneNumbers
  100. = invitesLeftToSend.filter(({ type }) => type === INVITE_TYPES.PHONE);
  101. // For each number, dial out. On success, remove the number from
  102. // {@link invitesLeftToSend}.
  103. const phoneInvitePromises = phoneNumbers.map(item => {
  104. const numberToInvite = item.number;
  105. return conference.dial(numberToInvite)
  106. .then(() => {
  107. invitesLeftToSend
  108. = invitesLeftToSend.filter(
  109. invitee => invitee !== item);
  110. })
  111. .catch((error: Error) =>
  112. logger.error('Error inviting phone number:', error));
  113. });
  114. allInvitePromises = allInvitePromises.concat(phoneInvitePromises);
  115. const usersAndRooms
  116. = invitesLeftToSend.filter(
  117. ({ type }) => [ INVITE_TYPES.USER, INVITE_TYPES.ROOM ].includes(type));
  118. if (usersAndRooms.length) {
  119. // Send a request to invite all the rooms and users. On success,
  120. // filter all rooms and users from {@link invitesLeftToSend}.
  121. const peopleInvitePromise
  122. = invitePeopleAndChatRooms(
  123. (callFlowsEnabled
  124. ? inviteServiceCallFlowsUrl : inviteServiceUrl) ?? '',
  125. inviteUrl,
  126. jwt,
  127. usersAndRooms)
  128. .then(() => {
  129. invitesLeftToSend
  130. = invitesLeftToSend.filter(
  131. ({ type }) => ![ INVITE_TYPES.USER, INVITE_TYPES.ROOM ].includes(type));
  132. })
  133. .catch(error => {
  134. dispatch(setCalleeInfoVisible(false));
  135. logger.error('Error inviting people:', error);
  136. });
  137. allInvitePromises.push(peopleInvitePromise);
  138. }
  139. // Sipgw calls are fire and forget. Invite them to the conference, then
  140. // immediately remove them from invitesLeftToSend.
  141. const vrooms
  142. = invitesLeftToSend.filter(({ type }) => type === INVITE_TYPES.VIDEO_ROOM);
  143. conference
  144. && vrooms.length > 0
  145. && dispatch(inviteVideoRooms(conference, vrooms));
  146. invitesLeftToSend
  147. = invitesLeftToSend.filter(({ type }) => type !== INVITE_TYPES.VIDEO_ROOM);
  148. const sipEndpoints
  149. = invitesLeftToSend.filter(({ type }) => type === INVITE_TYPES.SIP);
  150. conference && inviteSipEndpoints(
  151. sipEndpoints,
  152. // @ts-ignore
  153. locationURL,
  154. sipInviteUrl,
  155. jwt,
  156. conference.options.name,
  157. password,
  158. displayName
  159. );
  160. invitesLeftToSend
  161. = invitesLeftToSend.filter(({ type }) => type !== INVITE_TYPES.SIP);
  162. return (
  163. Promise.all(allInvitePromises)
  164. .then(() => invitesLeftToSend));
  165. };
  166. }
  167. /**
  168. * Sends AJAX requests for dial-in numbers and conference ID.
  169. *
  170. * @returns {Function}
  171. */
  172. export function updateDialInNumbers() {
  173. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  174. const state = getState();
  175. const { dialInConfCodeUrl, dialInNumbersUrl, hosts }
  176. = state['features/base/config'];
  177. const { numbersFetched } = state['features/invite'];
  178. const mucURL = hosts?.muc;
  179. if (numbersFetched || !dialInConfCodeUrl || !dialInNumbersUrl || !mucURL) {
  180. // URLs for fetching dial in numbers not defined
  181. return;
  182. }
  183. const { locationURL = {} } = state['features/base/connection'];
  184. const { room = '' } = state['features/base/conference'];
  185. Promise.all([
  186. getDialInNumbers(dialInNumbersUrl, room, mucURL), // @ts-ignore
  187. getDialInConferenceID(dialInConfCodeUrl, room, mucURL, locationURL)
  188. ])
  189. .then(([ dialInNumbers, { conference, id, message, sipUri } ]) => {
  190. if (!conference || !id) {
  191. return Promise.reject(message);
  192. }
  193. dispatch({
  194. type: UPDATE_DIAL_IN_NUMBERS_SUCCESS,
  195. conferenceID: id,
  196. dialInNumbers,
  197. sipUri
  198. });
  199. })
  200. .catch(error => {
  201. dispatch({
  202. type: UPDATE_DIAL_IN_NUMBERS_FAILED,
  203. error
  204. });
  205. });
  206. };
  207. }
  208. /**
  209. * Sets the visibility of {@code CalleeInfo}.
  210. *
  211. * @param {boolean|undefined} [calleeInfoVisible] - If {@code CalleeInfo} is
  212. * to be displayed/visible, then {@code true}; otherwise, {@code false} or
  213. * {@code undefined}.
  214. * @param {Object|undefined} [initialCalleeInfo] - Callee information.
  215. * @returns {{
  216. * type: SET_CALLEE_INFO_VISIBLE,
  217. * calleeInfoVisible: (boolean|undefined),
  218. * initialCalleeInfo
  219. * }}
  220. */
  221. export function setCalleeInfoVisible(
  222. calleeInfoVisible: boolean,
  223. initialCalleeInfo?: Object) {
  224. return {
  225. type: SET_CALLEE_INFO_VISIBLE,
  226. calleeInfoVisible,
  227. initialCalleeInfo
  228. };
  229. }
  230. /**
  231. * Adds pending invite request.
  232. *
  233. * @param {Object} request - The request.
  234. * @returns {{
  235. * type: ADD_PENDING_INVITE_REQUEST,
  236. * request: Object
  237. * }}
  238. */
  239. export function addPendingInviteRequest(
  240. request: { callback: Function; invitees: Array<Object>; }) {
  241. return {
  242. type: ADD_PENDING_INVITE_REQUEST,
  243. request
  244. };
  245. }
  246. /**
  247. * Removes all pending invite requests.
  248. *
  249. * @returns {{
  250. * type: REMOVE_PENDING_INVITE_REQUEST
  251. * }}
  252. */
  253. export function removePendingInviteRequests() {
  254. return {
  255. type: REMOVE_PENDING_INVITE_REQUESTS
  256. };
  257. }