Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

middleware.ts 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { IReduxState } from '../app/types';
  2. import {
  3. CONFERENCE_JOINED,
  4. CONFERENCE_WILL_LEAVE,
  5. SET_ROOM
  6. } from '../base/conference/actionTypes';
  7. import { SET_CONFIG } from '../base/config/actionTypes';
  8. import { SET_NETWORK_INFO } from '../base/net-info/actionTypes';
  9. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  10. import {
  11. TRACK_ADDED,
  12. TRACK_REMOVED,
  13. TRACK_UPDATED
  14. } from '../base/tracks/actionTypes';
  15. import {
  16. getLocalAudioTrack,
  17. getLocalVideoTrack
  18. } from '../base/tracks/functions';
  19. import { createLocalTracksDurationEvent, createNetworkInfoEvent } from './AnalyticsEvents';
  20. import { UPDATE_LOCAL_TRACKS_DURATION } from './actionTypes';
  21. import { createHandlers, initAnalytics, resetAnalytics, sendAnalytics } from './functions';
  22. /**
  23. * Calculates the duration of the local tracks.
  24. *
  25. * @param {Object} state - The redux state.
  26. * @returns {Object} - The local tracks duration.
  27. */
  28. function calculateLocalTrackDuration(state: IReduxState) {
  29. const now = Date.now();
  30. const { localTracksDuration } = state['features/analytics'];
  31. const { conference } = state['features/base/conference'];
  32. const { audio, video } = localTracksDuration;
  33. const { camera, desktop } = video;
  34. const tracks = state['features/base/tracks'];
  35. const audioTrack = getLocalAudioTrack(tracks);
  36. const videoTrack = getLocalVideoTrack(tracks);
  37. const newDuration = { ...localTracksDuration };
  38. if (!audioTrack || audioTrack.muted || !conference) {
  39. newDuration.audio = {
  40. startedTime: -1,
  41. value: audio.value + (audio.startedTime === -1 ? 0 : now - audio.startedTime)
  42. };
  43. } else if (audio.startedTime === -1) {
  44. newDuration.audio.startedTime = now;
  45. }
  46. if (!videoTrack || videoTrack.muted || !conference) {
  47. newDuration.video = {
  48. camera: {
  49. startedTime: -1,
  50. value: camera.value + (camera.startedTime === -1 ? 0 : now - camera.startedTime)
  51. },
  52. desktop: {
  53. startedTime: -1,
  54. value: desktop.value + (desktop.startedTime === -1 ? 0 : now - desktop.startedTime)
  55. }
  56. };
  57. } else {
  58. const { videoType } = videoTrack;
  59. if (video[videoType as keyof typeof video].startedTime === -1) {
  60. newDuration.video[videoType as keyof typeof video].startedTime = now;
  61. }
  62. }
  63. return {
  64. ...localTracksDuration,
  65. ...newDuration
  66. };
  67. }
  68. /**
  69. * Middleware which intercepts config actions to handle evaluating analytics
  70. * config based on the config stored in the store.
  71. *
  72. * @param {Store} store - The redux store.
  73. * @returns {Function}
  74. */
  75. MiddlewareRegistry.register(store => next => action => {
  76. switch (action.type) {
  77. case SET_CONFIG:
  78. if (navigator.product === 'ReactNative') {
  79. // Resetting the analytics is currently not needed for web because
  80. // the user will be redirected to another page and new instance of
  81. // Analytics will be created and initialized.
  82. resetAnalytics();
  83. }
  84. break;
  85. case SET_ROOM: {
  86. // createHandlers is called before the SET_ROOM action is executed in order for Amplitude to initialize before
  87. // the deeplinking logic is executed (after the SET_ROOM action) so that the Amplitude device id is available
  88. // if needed.
  89. const createHandlersPromise = createHandlers(store);
  90. const result = next(action);
  91. createHandlersPromise.then(handlers => {
  92. initAnalytics(store, handlers);
  93. });
  94. return result;
  95. }
  96. }
  97. const result = next(action);
  98. switch (action.type) {
  99. case CONFERENCE_JOINED: {
  100. const { dispatch, getState } = store;
  101. const state = getState();
  102. dispatch({
  103. type: UPDATE_LOCAL_TRACKS_DURATION,
  104. localTracksDuration: {
  105. ...calculateLocalTrackDuration(state),
  106. conference: {
  107. startedTime: Date.now(),
  108. value: 0
  109. }
  110. }
  111. });
  112. break;
  113. }
  114. case CONFERENCE_WILL_LEAVE: {
  115. const { dispatch, getState } = store;
  116. const state = getState();
  117. const { localTracksDuration } = state['features/analytics'];
  118. const newLocalTracksDuration = {
  119. ...calculateLocalTrackDuration(state),
  120. conference: {
  121. startedTime: -1,
  122. value: Date.now() - localTracksDuration.conference.startedTime
  123. }
  124. };
  125. sendAnalytics(createLocalTracksDurationEvent(newLocalTracksDuration));
  126. dispatch({
  127. type: UPDATE_LOCAL_TRACKS_DURATION,
  128. localTracksDuration: newLocalTracksDuration
  129. });
  130. break;
  131. }
  132. case SET_NETWORK_INFO:
  133. sendAnalytics(
  134. createNetworkInfoEvent({
  135. isOnline: action.isOnline,
  136. details: action.details,
  137. networkType: action.networkType
  138. }));
  139. break;
  140. case TRACK_ADDED:
  141. case TRACK_REMOVED:
  142. case TRACK_UPDATED: {
  143. const { dispatch, getState } = store;
  144. const state = getState();
  145. const { localTracksDuration } = state['features/analytics'];
  146. if (localTracksDuration.conference.startedTime === -1) {
  147. // We don't want to track the media duration if the conference is not joined yet because otherwise we won't
  148. // be able to compare them with the conference duration (from conference join to conference will leave).
  149. break;
  150. }
  151. dispatch({
  152. type: UPDATE_LOCAL_TRACKS_DURATION,
  153. localTracksDuration: {
  154. ...localTracksDuration,
  155. ...calculateLocalTrackDuration(state)
  156. }
  157. });
  158. break;
  159. }
  160. }
  161. return result;
  162. });