您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.ts 5.9KB

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