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.

actions.web.ts 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // @ts-expect-error
  2. import { FEEDBACK_REQUEST_IN_PROGRESS } from '../../../modules/UI/UIErrors';
  3. import { IStore } from '../app/types';
  4. import { IJitsiConference } from '../base/conference/reducer';
  5. import { openDialog } from '../base/dialog/actions';
  6. import { extractFqnFromPath } from '../dynamic-branding/functions.any';
  7. import {
  8. CANCEL_FEEDBACK,
  9. SUBMIT_FEEDBACK_ERROR,
  10. SUBMIT_FEEDBACK_SUCCESS
  11. } from './actionTypes';
  12. import FeedbackDialog from './components/FeedbackDialog.web';
  13. import { sendFeedbackToJaaSRequest, shouldSendJaaSFeedbackMetadata } from './functions.web';
  14. /**
  15. * Caches the passed in feedback in the redux store.
  16. *
  17. * @param {number} score - The quality score given to the conference.
  18. * @param {string} message - A description entered by the participant that
  19. * explains the rating.
  20. * @returns {{
  21. * type: CANCEL_FEEDBACK,
  22. * message: string,
  23. * score: number
  24. * }}
  25. */
  26. export function cancelFeedback(score: number, message: string) {
  27. return {
  28. type: CANCEL_FEEDBACK,
  29. message,
  30. score
  31. };
  32. }
  33. /**
  34. * Potentially open the {@code FeedbackDialog}. It will not be opened if it is
  35. * already open or feedback has already been submitted.
  36. *
  37. * @param {JistiConference} conference - The conference for which the feedback
  38. * would be about. The conference is passed in because feedback can occur after
  39. * a conference has been left, so references to it may no longer exist in redux.
  40. * @param {string} title - The feedback dialog title.
  41. * @returns {Promise} Resolved with value - false if the dialog is enabled and
  42. * resolved with true if the dialog is disabled or the feedback was already
  43. * submitted. Rejected if another dialog is already displayed.
  44. */
  45. export function maybeOpenFeedbackDialog(conference: IJitsiConference, title?: string) {
  46. type R = {
  47. feedbackSubmitted: boolean;
  48. showThankYou: boolean;
  49. wasDialogShown: boolean;
  50. };
  51. return (dispatch: IStore['dispatch'], getState: IStore['getState']): Promise<R> => {
  52. const state = getState();
  53. const { feedbackPercentage = 100 } = state['features/base/config'];
  54. if (config.iAmRecorder) {
  55. // Intentionally fall through the if chain to prevent further action
  56. // from being taken with regards to showing feedback.
  57. } else if (state['features/base/dialog'].component === FeedbackDialog) {
  58. // Feedback is currently being displayed.
  59. return Promise.reject(FEEDBACK_REQUEST_IN_PROGRESS);
  60. } else if (state['features/feedback'].submitted) {
  61. // Feedback has been submitted already.
  62. return Promise.resolve({
  63. feedbackSubmitted: true,
  64. showThankYou: true,
  65. wasDialogShown: false
  66. });
  67. } else if (shouldSendJaaSFeedbackMetadata(state)
  68. && feedbackPercentage > Math.random() * 100) {
  69. return new Promise(resolve => {
  70. dispatch(openFeedbackDialog(conference, title, () => {
  71. const { submitted } = getState()['features/feedback'];
  72. resolve({
  73. feedbackSubmitted: submitted,
  74. showThankYou: false,
  75. wasDialogShown: true
  76. });
  77. }));
  78. });
  79. }
  80. // If the feedback functionality isn't enabled we show a "thank you"
  81. // message. Signaling it (true), so the caller of requestFeedback can
  82. // act on it.
  83. return Promise.resolve({
  84. feedbackSubmitted: false,
  85. showThankYou: true,
  86. wasDialogShown: false
  87. });
  88. };
  89. }
  90. /**
  91. * Opens {@code FeedbackDialog}.
  92. *
  93. * @param {JitsiConference} conference - The JitsiConference that is being
  94. * rated. The conference is passed in because feedback can occur after a
  95. * conference has been left, so references to it may no longer exist in redux.
  96. * @param {string} [title] - The feedback dialog title.
  97. * @param {Function} [onClose] - An optional callback to invoke when the dialog
  98. * is closed.
  99. * @returns {Object}
  100. */
  101. export function openFeedbackDialog(conference?: IJitsiConference, title?: string, onClose?: Function) {
  102. return openDialog(FeedbackDialog, {
  103. conference,
  104. onClose,
  105. title
  106. });
  107. }
  108. /**
  109. * Sends feedback metadata to JaaS endpoint.
  110. *
  111. * @param {JitsiConference} conference - The JitsiConference that is being rated.
  112. * @param {Object} feedback - The feedback message and score.
  113. *
  114. * @returns {Promise}
  115. */
  116. export function sendJaasFeedbackMetadata(conference: IJitsiConference, feedback: Object) {
  117. return (_dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  118. const state = getState();
  119. if (!shouldSendJaaSFeedbackMetadata(state)) {
  120. return Promise.resolve();
  121. }
  122. const { jaasFeedbackMetadataURL } = state['features/base/config'];
  123. const { jwt, user, tenant } = state['features/base/jwt'];
  124. const meetingFqn = extractFqnFromPath();
  125. const feedbackData = {
  126. ...feedback,
  127. sessionId: conference.sessionId,
  128. userId: user?.id,
  129. meetingFqn,
  130. jwt,
  131. tenant
  132. };
  133. return sendFeedbackToJaaSRequest(jaasFeedbackMetadataURL, feedbackData);
  134. };
  135. }
  136. /**
  137. * Send the passed in feedback.
  138. *
  139. * @param {number} score - An integer between 1 and 5 indicating the user
  140. * feedback. The negative integer -1 is used to denote no score was selected.
  141. * @param {string} message - Detailed feedback from the user to explain the
  142. * rating.
  143. * @param {JitsiConference} conference - The JitsiConference for which the
  144. * feedback is being left.
  145. * @returns {Function}
  146. */
  147. export function submitFeedback(
  148. score: number,
  149. message: string,
  150. conference: IJitsiConference) {
  151. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  152. const state = getState();
  153. const promises = [];
  154. if (shouldSendJaaSFeedbackMetadata(state)) {
  155. promises.push(dispatch(sendJaasFeedbackMetadata(conference, {
  156. score,
  157. message
  158. })));
  159. }
  160. return Promise.allSettled(promises)
  161. .then(results => {
  162. const rejected = results.find((result): result is PromiseRejectedResult => result?.status === 'rejected');
  163. if (typeof rejected === 'undefined') {
  164. dispatch({ type: SUBMIT_FEEDBACK_SUCCESS });
  165. return Promise.resolve();
  166. }
  167. const error = rejected.reason;
  168. dispatch({
  169. type: SUBMIT_FEEDBACK_ERROR,
  170. error
  171. });
  172. return Promise.reject(error);
  173. });
  174. };
  175. }