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 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // @flow
  2. import { isMobileBrowser } from '../base/environment/utils';
  3. import { JitsiRecordingConstants, browser } from '../base/lib-jitsi-meet';
  4. import { getLocalParticipant, getRemoteParticipants, isLocalParticipantModerator } from '../base/participants';
  5. import { isInBreakoutRoom } from '../breakout-rooms/functions';
  6. import { isEnabled as isDropboxEnabled } from '../dropbox';
  7. import { extractFqnFromPath } from '../dynamic-branding/functions.any';
  8. import LocalRecordingManager from './components/Recording/LocalRecordingManager';
  9. import { RECORDING_STATUS_PRIORITIES, RECORDING_TYPES } from './constants';
  10. import logger from './logger';
  11. /**
  12. * Searches in the passed in redux state for an active recording session of the
  13. * passed in mode.
  14. *
  15. * @param {Object} state - The redux state to search in.
  16. * @param {string} mode - Find an active recording session of the given mode.
  17. * @returns {Object|undefined}
  18. */
  19. export function getActiveSession(state: Object, mode: string) {
  20. const { sessionDatas } = state['features/recording'];
  21. const { status: statusConstants } = JitsiRecordingConstants;
  22. return sessionDatas.find(sessionData => sessionData.mode === mode
  23. && (sessionData.status === statusConstants.ON
  24. || sessionData.status === statusConstants.PENDING));
  25. }
  26. /**
  27. * Returns an estimated recording duration based on the size of the video file
  28. * in MB. The estimate is calculated under the assumption that 1 min of recorded
  29. * video needs 10MB of storage on average.
  30. *
  31. * @param {number} size - The size in MB of the recorded video.
  32. * @returns {number} - The estimated duration in minutes.
  33. */
  34. export function getRecordingDurationEstimation(size: ?number) {
  35. return Math.floor((size || 0) / 10);
  36. }
  37. /**
  38. * Searches in the passed in redux state for a recording session that matches
  39. * the passed in recording session ID.
  40. *
  41. * @param {Object} state - The redux state to search in.
  42. * @param {string} id - The ID of the recording session to find.
  43. * @returns {Object|undefined}
  44. */
  45. export function getSessionById(state: Object, id: string) {
  46. return state['features/recording'].sessionDatas.find(
  47. sessionData => sessionData.id === id);
  48. }
  49. /**
  50. * Fetches the recording link from the server.
  51. *
  52. * @param {string} url - The base url.
  53. * @param {string} recordingSessionId - The ID of the recording session to find.
  54. * @param {string} region - The meeting region.
  55. * @param {string} tenant - The meeting tenant.
  56. * @returns {Promise<any>}
  57. */
  58. export async function getRecordingLink(url: string, recordingSessionId: string, region: string, tenant: string) {
  59. const fullUrl = `${url}?recordingSessionId=${recordingSessionId}&region=${region}&tenant=${tenant}`;
  60. const res = await fetch(fullUrl, {
  61. headers: {
  62. 'Content-Type': 'application/json'
  63. }
  64. });
  65. const json = await res.json();
  66. return res.ok ? json : Promise.reject(json);
  67. }
  68. /**
  69. * Selector used for determining if recording is saved on dropbox.
  70. *
  71. * @param {Object} state - The redux state to search in.
  72. * @returns {string}
  73. */
  74. export function isSavingRecordingOnDropbox(state: Object) {
  75. return isDropboxEnabled(state)
  76. && state['features/recording'].selectedRecordingService === RECORDING_TYPES.DROPBOX;
  77. }
  78. /**
  79. * Selector used for determining disable state for the meeting highlight button.
  80. *
  81. * @param {Object} state - The redux state to search in.
  82. * @returns {string}
  83. */
  84. export function isHighlightMeetingMomentDisabled(state: Object) {
  85. return state['features/recording'].disableHighlightMeetingMoment;
  86. }
  87. /**
  88. * Returns the recording session status that is to be shown in a label. E.g. If
  89. * there is a session with the status OFF and one with PENDING, then the PENDING
  90. * one will be shown, because that is likely more important for the user to see.
  91. *
  92. * @param {Object} state - The redux state to search in.
  93. * @param {string} mode - The recording mode to get status for.
  94. * @returns {string|undefined}
  95. */
  96. export function getSessionStatusToShow(state: Object, mode: string): ?string {
  97. const recordingSessions = state['features/recording'].sessionDatas;
  98. let status;
  99. if (Array.isArray(recordingSessions)) {
  100. for (const session of recordingSessions) {
  101. if (session.mode === mode
  102. && (!status
  103. || (RECORDING_STATUS_PRIORITIES.indexOf(session.status)
  104. > RECORDING_STATUS_PRIORITIES.indexOf(status)))) {
  105. status = session.status;
  106. }
  107. }
  108. }
  109. if ((!Array.isArray(recordingSessions) || recordingSessions.length === 0)
  110. && mode === JitsiRecordingConstants.mode.FILE
  111. && (LocalRecordingManager.isRecordingLocally() || isRemoteParticipantRecordingLocally(state))) {
  112. status = JitsiRecordingConstants.status.ON;
  113. }
  114. return status;
  115. }
  116. /**
  117. * Check if local recording is supported.
  118. *
  119. * @returns {boolean} - Whether local recording is supported or not.
  120. */
  121. export function supportsLocalRecording() {
  122. return browser.isChromiumBased() && !browser.isElectron() && !isMobileBrowser()
  123. && navigator.product !== 'ReactNative';
  124. }
  125. /**
  126. * Returns the recording button props.
  127. *
  128. * @param {Object} state - The redux state to search in.
  129. *
  130. * @returns {{
  131. * disabled: boolean,
  132. * tooltip: string,
  133. * visible: boolean
  134. * }}
  135. */
  136. export function getRecordButtonProps(state: Object): ?string {
  137. let visible;
  138. // a button can be disabled/enabled if enableFeaturesBasedOnToken
  139. // is on or if the livestreaming is running.
  140. let disabled;
  141. let tooltip = '';
  142. // If the containing component provides the visible prop, that is one
  143. // above all, but if not, the button should be autonomus and decide on
  144. // its own to be visible or not.
  145. const isModerator = isLocalParticipantModerator(state);
  146. const {
  147. enableFeaturesBasedOnToken,
  148. recordingService,
  149. localRecording
  150. } = state['features/base/config'];
  151. const { features = {} } = getLocalParticipant(state);
  152. const localRecordingEnabled = !localRecording?.disable && supportsLocalRecording();
  153. const dropboxEnabled = isDropboxEnabled(state);
  154. visible = isModerator && (recordingService?.enabled || localRecordingEnabled || dropboxEnabled);
  155. if (enableFeaturesBasedOnToken) {
  156. visible = visible && String(features.recording) === 'true';
  157. disabled = String(features.recording) === 'disabled';
  158. if (!visible && !disabled) {
  159. disabled = true;
  160. visible = true;
  161. tooltip = 'dialog.recordingDisabledTooltip';
  162. }
  163. }
  164. // disable the button if the livestreaming is running.
  165. if (getActiveSession(state, JitsiRecordingConstants.mode.STREAM)) {
  166. disabled = true;
  167. tooltip = 'dialog.recordingDisabledBecauseOfActiveLiveStreamingTooltip';
  168. }
  169. // disable the button if we are in a breakout room.
  170. if (isInBreakoutRoom(state)) {
  171. disabled = true;
  172. visible = false;
  173. }
  174. return {
  175. disabled,
  176. tooltip,
  177. visible
  178. };
  179. }
  180. /**
  181. * Returns the resource id.
  182. *
  183. * @param {Object | string} recorder - A participant or it's resource.
  184. * @returns {string|undefined}
  185. */
  186. export function getResourceId(recorder: string | Object) {
  187. if (recorder) {
  188. return typeof recorder === 'string'
  189. ? recorder
  190. : recorder.getId();
  191. }
  192. }
  193. /**
  194. * Sends a meeting highlight to backend.
  195. *
  196. * @param {Object} state - Redux state.
  197. * @returns {boolean} - True if sent, false otherwise.
  198. */
  199. export async function sendMeetingHighlight(state: Object) {
  200. const { webhookProxyUrl: url } = state['features/base/config'];
  201. const { conference } = state['features/base/conference'];
  202. const { jwt } = state['features/base/jwt'];
  203. const { connection } = state['features/base/connection'];
  204. const jid = connection.getJid();
  205. const localParticipant = getLocalParticipant(state);
  206. const headers = {
  207. ...jwt ? { 'Authorization': `Bearer ${jwt}` } : {},
  208. 'Content-Type': 'application/json'
  209. };
  210. const reqBody = {
  211. meetingFqn: extractFqnFromPath(state),
  212. sessionId: conference.getMeetingUniqueId(),
  213. submitted: Date.now(),
  214. participantId: localParticipant.jwtId,
  215. participantName: localParticipant.name,
  216. participantJid: jid
  217. };
  218. if (url) {
  219. try {
  220. const res = await fetch(`${url}/v2/highlights`, {
  221. method: 'POST',
  222. headers,
  223. body: JSON.stringify(reqBody)
  224. });
  225. if (res.ok) {
  226. return true;
  227. }
  228. logger.error('Status error:', res.status);
  229. } catch (err) {
  230. logger.error('Could not send request', err);
  231. }
  232. }
  233. return false;
  234. }
  235. /**
  236. * Whether a remote participant is recording locally or not.
  237. *
  238. * @param {Object} state - Redux state.
  239. * @returns {boolean}
  240. */
  241. function isRemoteParticipantRecordingLocally(state) {
  242. const participants = getRemoteParticipants(state);
  243. // eslint-disable-next-line prefer-const
  244. for (let value of participants.values()) {
  245. if (value.localRecording) {
  246. return true;
  247. }
  248. }
  249. return false;
  250. }