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.

functions.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // @flow
  2. import _ from 'lodash';
  3. import { JitsiTrackErrors } from '../lib-jitsi-meet';
  4. import {
  5. getLocalParticipant,
  6. hiddenParticipantJoined,
  7. hiddenParticipantLeft,
  8. participantJoined,
  9. participantLeft
  10. } from '../participants';
  11. import { toState } from '../redux';
  12. import { safeDecodeURIComponent } from '../util';
  13. import {
  14. AVATAR_URL_COMMAND,
  15. EMAIL_COMMAND,
  16. JITSI_CONFERENCE_URL_KEY
  17. } from './constants';
  18. import logger from './logger';
  19. /**
  20. * Attach a set of local tracks to a conference.
  21. *
  22. * @param {JitsiConference} conference - Conference instance.
  23. * @param {JitsiLocalTrack[]} localTracks - List of local media tracks.
  24. * @protected
  25. * @returns {Promise}
  26. */
  27. export function _addLocalTracksToConference(
  28. conference: { addTrack: Function, getLocalTracks: Function },
  29. localTracks: Array<Object>) {
  30. const conferenceLocalTracks = conference.getLocalTracks();
  31. const promises = [];
  32. for (const track of localTracks) {
  33. // XXX The library lib-jitsi-meet may be draconian, for example, when
  34. // adding one and the same video track multiple times.
  35. if (conferenceLocalTracks.indexOf(track) === -1) {
  36. promises.push(
  37. conference.addTrack(track).catch(err => {
  38. _reportError(
  39. 'Failed to add local track to conference',
  40. err);
  41. }));
  42. }
  43. }
  44. return Promise.all(promises);
  45. }
  46. /**
  47. * Logic shared between web and RN which processes the {@code USER_JOINED}
  48. * conference event and dispatches either {@link participantJoined} or
  49. * {@link hiddenParticipantJoined}.
  50. *
  51. * @param {Object} store - The redux store.
  52. * @param {JitsiMeetConference} conference - The conference for which the
  53. * {@code USER_JOINED} event is being processed.
  54. * @param {JitsiParticipant} user - The user who has just joined.
  55. * @returns {void}
  56. */
  57. export function commonUserJoinedHandling(
  58. { dispatch }: Object,
  59. conference: Object,
  60. user: Object) {
  61. const id = user.getId();
  62. const displayName = user.getDisplayName();
  63. if (user.isHidden()) {
  64. dispatch(hiddenParticipantJoined(id, displayName));
  65. } else {
  66. dispatch(participantJoined({
  67. botType: user.getBotType(),
  68. connectionStatus: user.getConnectionStatus(),
  69. conference,
  70. id,
  71. name: displayName,
  72. presence: user.getStatus(),
  73. role: user.getRole()
  74. }));
  75. }
  76. }
  77. /**
  78. * Logic shared between web and RN which processes the {@code USER_LEFT}
  79. * conference event and dispatches either {@link participantLeft} or
  80. * {@link hiddenParticipantLeft}.
  81. *
  82. * @param {Object} store - The redux store.
  83. * @param {JitsiMeetConference} conference - The conference for which the
  84. * {@code USER_LEFT} event is being processed.
  85. * @param {JitsiParticipant} user - The user who has just left.
  86. * @returns {void}
  87. */
  88. export function commonUserLeftHandling(
  89. { dispatch }: Object,
  90. conference: Object,
  91. user: Object) {
  92. const id = user.getId();
  93. if (user.isHidden()) {
  94. dispatch(hiddenParticipantLeft(id));
  95. } else {
  96. dispatch(participantLeft(id, conference));
  97. }
  98. }
  99. /**
  100. * Evaluates a specific predicate for each {@link JitsiConference} known to the
  101. * redux state features/base/conference while it returns {@code true}.
  102. *
  103. * @param {Function | Object} stateful - The redux store, state, or
  104. * {@code getState} function.
  105. * @param {Function} predicate - The predicate to evaluate for each
  106. * {@code JitsiConference} know to the redux state features/base/conference
  107. * while it returns {@code true}.
  108. * @returns {boolean} If the specified {@code predicate} returned {@code true}
  109. * for all {@code JitsiConference} instances known to the redux state
  110. * features/base/conference.
  111. */
  112. export function forEachConference(
  113. stateful: Function | Object,
  114. predicate: (Object, URL) => boolean) {
  115. const state = toState(stateful)['features/base/conference'];
  116. for (const v of Object.values(state)) {
  117. // Does the value of the base/conference's property look like a
  118. // JitsiConference?
  119. if (v && typeof v === 'object') {
  120. // $FlowFixMe
  121. const url: URL = v[JITSI_CONFERENCE_URL_KEY];
  122. // XXX The Web version of Jitsi Meet does not utilize
  123. // JITSI_CONFERENCE_URL_KEY at the time of this writing. An
  124. // alternative is necessary then to recognize JitsiConference
  125. // instances and myUserId is as good as any other property.
  126. if ((url || typeof v.myUserId === 'function')
  127. && !predicate(v, url)) {
  128. return false;
  129. }
  130. }
  131. }
  132. return true;
  133. }
  134. /**
  135. * Returns the display name of the conference.
  136. *
  137. * @param {Function | Object} stateful - Reference that can be resolved to Redux
  138. * state with the {@code toState} function.
  139. * @returns {string}
  140. */
  141. export function getConferenceName(stateful: Function | Object): string {
  142. const state = toState(stateful);
  143. const { callee } = state['features/base/jwt'];
  144. const { callDisplayName } = state['features/base/config'];
  145. const { pendingSubjectChange, room, subject } = state['features/base/conference'];
  146. return pendingSubjectChange
  147. || subject
  148. || callDisplayName
  149. || (callee && callee.name)
  150. || safeStartCase(safeDecodeURIComponent(room));
  151. }
  152. /**
  153. * Returns the name of the conference formatted for the title.
  154. *
  155. * @param {Function | Object} stateful - Reference that can be resolved to Redux state with the {@code toState}
  156. * function.
  157. * @returns {string} - The name of the conference formatted for the title.
  158. */
  159. export function getConferenceNameForTitle(stateful: Function | Object) {
  160. return safeStartCase(safeDecodeURIComponent(toState(stateful)['features/base/conference'].room));
  161. }
  162. /**
  163. * Returns the UTC timestamp when the first participant joined the conference.
  164. *
  165. * @param {Function | Object} stateful - Reference that can be resolved to Redux
  166. * state with the {@code toState} function.
  167. * @returns {number}
  168. */
  169. export function getConferenceTimestamp(stateful: Function | Object): number {
  170. const state = toState(stateful);
  171. const { conferenceTimestamp } = state['features/base/conference'];
  172. return conferenceTimestamp;
  173. }
  174. /**
  175. * Returns the current {@code JitsiConference} which is joining or joined and is
  176. * not leaving. Please note the contrast with merely reading the
  177. * {@code conference} state of the feature base/conference which is not joining
  178. * but may be leaving already.
  179. *
  180. * @param {Function|Object} stateful - The redux store, state, or
  181. * {@code getState} function.
  182. * @returns {JitsiConference|undefined}
  183. */
  184. export function getCurrentConference(stateful: Function | Object) {
  185. const { conference, joining, leaving, membersOnly, passwordRequired }
  186. = toState(stateful)['features/base/conference'];
  187. // There is a precedence
  188. if (conference) {
  189. return conference === leaving ? undefined : conference;
  190. }
  191. return joining || passwordRequired || membersOnly;
  192. }
  193. /**
  194. * Returns the stored room name.
  195. *
  196. * @param {Object} state - The current state of the app.
  197. * @returns {string}
  198. */
  199. export function getRoomName(state: Object): string {
  200. return state['features/base/conference'].room;
  201. }
  202. /**
  203. * Handle an error thrown by the backend (i.e. {@code lib-jitsi-meet}) while
  204. * manipulating a conference participant (e.g. Pin or select participant).
  205. *
  206. * @param {Error} err - The Error which was thrown by the backend while
  207. * manipulating a conference participant and which is to be handled.
  208. * @protected
  209. * @returns {void}
  210. */
  211. export function _handleParticipantError(err: { message: ?string }) {
  212. // XXX DataChannels are initialized at some later point when the conference
  213. // has multiple participants, but code that pins or selects a participant
  214. // might be executed before. So here we're swallowing a particular error.
  215. // TODO Lib-jitsi-meet should be fixed to not throw such an exception in
  216. // these scenarios.
  217. if (err.message !== 'Data channels support is disabled!') {
  218. throw err;
  219. }
  220. }
  221. /**
  222. * Determines whether a specific string is a valid room name.
  223. *
  224. * @param {(string|undefined)} room - The name of the conference room to check
  225. * for validity.
  226. * @returns {boolean} If the specified room name is valid, then true; otherwise,
  227. * false.
  228. */
  229. export function isRoomValid(room: ?string) {
  230. return typeof room === 'string' && room !== '';
  231. }
  232. /**
  233. * Remove a set of local tracks from a conference.
  234. *
  235. * @param {JitsiConference} conference - Conference instance.
  236. * @param {JitsiLocalTrack[]} localTracks - List of local media tracks.
  237. * @protected
  238. * @returns {Promise}
  239. */
  240. export function _removeLocalTracksFromConference(
  241. conference: { removeTrack: Function },
  242. localTracks: Array<Object>) {
  243. return Promise.all(localTracks.map(track =>
  244. conference.removeTrack(track)
  245. .catch(err => {
  246. // Local track might be already disposed by direct
  247. // JitsiTrack#dispose() call. So we should ignore this error
  248. // here.
  249. if (err.name !== JitsiTrackErrors.TRACK_IS_DISPOSED) {
  250. _reportError(
  251. 'Failed to remove local track from conference',
  252. err);
  253. }
  254. })
  255. ));
  256. }
  257. /**
  258. * Reports a specific Error with a specific error message. While the
  259. * implementation merely logs the specified msg and err via the console at the
  260. * time of this writing, the intention of the function is to abstract the
  261. * reporting of errors and facilitate elaborating on it in the future.
  262. *
  263. * @param {string} msg - The error message to report.
  264. * @param {Error} err - The Error to report.
  265. * @private
  266. * @returns {void}
  267. */
  268. function _reportError(msg, err) {
  269. // TODO This is a good point to call some global error handler when we have
  270. // one.
  271. logger.error(msg, err);
  272. }
  273. /**
  274. * Sends a representation of the local participant such as her avatar (URL),
  275. * e-mail address, and display name to (the remote participants of) a specific
  276. * conference.
  277. *
  278. * @param {Function|Object} stateful - The redux store, state, or
  279. * {@code getState} function.
  280. * @param {JitsiConference} conference - The {@code JitsiConference} to which
  281. * the representation of the local participant is to be sent.
  282. * @returns {void}
  283. */
  284. export function sendLocalParticipant(
  285. stateful: Function | Object,
  286. conference: {
  287. sendCommand: Function,
  288. setDisplayName: Function,
  289. setLocalParticipantProperty: Function }) {
  290. const {
  291. avatarURL,
  292. email,
  293. features,
  294. name
  295. } = getLocalParticipant(stateful);
  296. avatarURL && conference.sendCommand(AVATAR_URL_COMMAND, {
  297. value: avatarURL
  298. });
  299. email && conference.sendCommand(EMAIL_COMMAND, {
  300. value: email
  301. });
  302. if (features && features['screen-sharing'] === 'true') {
  303. conference.setLocalParticipantProperty('features_screen-sharing', true);
  304. }
  305. conference.setDisplayName(name);
  306. }
  307. /**
  308. * A safe implementation of lodash#startCase that doesn't deburr the string.
  309. *
  310. * NOTE: According to lodash roadmap, lodash v5 will have this function.
  311. *
  312. * Code based on https://github.com/lodash/lodash/blob/master/startCase.js.
  313. *
  314. * @param {string} s - The string to do start case on.
  315. * @returns {string}
  316. */
  317. function safeStartCase(s = '') {
  318. return _.words(`${s}`.replace(/['\u2019]/g, '')).reduce(
  319. (result, word, index) => result + (index ? ' ' : '') + _.upperFirst(word)
  320. , '');
  321. }