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

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