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

actions.web.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // @flow
  2. import { loadGoogleAPI } from '../google-api';
  3. import { refreshCalendar, setCalendarEvents } from './actions';
  4. import { createCalendarConnectedEvent, sendAnalytics } from '../analytics';
  5. import {
  6. CLEAR_CALENDAR_INTEGRATION,
  7. SET_CALENDAR_AUTH_STATE,
  8. SET_CALENDAR_INTEGRATION,
  9. SET_CALENDAR_PROFILE_EMAIL,
  10. SET_LOADING_CALENDAR_EVENTS
  11. } from './actionTypes';
  12. import { _getCalendarIntegration, isCalendarEnabled } from './functions';
  13. import { generateRoomWithoutSeparator } from '../welcome';
  14. export * from './actions.any';
  15. const logger = require('jitsi-meet-logger').getLogger(__filename);
  16. /**
  17. * Sets the initial state of calendar integration by loading third party APIs
  18. * and filling out any data that needs to be fetched.
  19. *
  20. * @returns {Function}
  21. */
  22. export function bootstrapCalendarIntegration(): Function {
  23. return (dispatch, getState) => {
  24. const {
  25. googleApiApplicationClientID
  26. } = getState()['features/base/config'];
  27. const {
  28. integrationReady,
  29. integrationType
  30. } = getState()['features/calendar-sync'];
  31. if (!isCalendarEnabled()) {
  32. return Promise.reject();
  33. }
  34. return Promise.resolve()
  35. .then(() => {
  36. if (googleApiApplicationClientID) {
  37. return dispatch(
  38. loadGoogleAPI(googleApiApplicationClientID));
  39. }
  40. })
  41. .then(() => {
  42. if (!integrationType || integrationReady) {
  43. return;
  44. }
  45. const integrationToLoad
  46. = _getCalendarIntegration(integrationType);
  47. if (!integrationToLoad) {
  48. dispatch(clearCalendarIntegration());
  49. return;
  50. }
  51. return dispatch(integrationToLoad._isSignedIn())
  52. .then(signedIn => {
  53. if (signedIn) {
  54. dispatch(setIntegrationReady(integrationType));
  55. dispatch(updateProfile(integrationType));
  56. } else {
  57. dispatch(clearCalendarIntegration());
  58. }
  59. });
  60. });
  61. };
  62. }
  63. /**
  64. * Resets the state of calendar integration so stored events and selected
  65. * calendar type are cleared.
  66. *
  67. * @returns {{
  68. * type: CLEAR_CALENDAR_INTEGRATION
  69. * }}
  70. */
  71. export function clearCalendarIntegration() {
  72. return {
  73. type: CLEAR_CALENDAR_INTEGRATION
  74. };
  75. }
  76. /**
  77. * Asks confirmation from the user to add a Jitsi link to the calendar event.
  78. *
  79. * NOTE: Currently there is no confirmation prompted on web, so this is just
  80. * a relaying method to avoid flow problems.
  81. *
  82. * @param {string} eventId - The event id.
  83. * @param {string} calendarId - The calendar id.
  84. * @returns {Function}
  85. */
  86. export function openUpdateCalendarEventDialog(
  87. eventId: string, calendarId: string) {
  88. return updateCalendarEvent(eventId, calendarId);
  89. }
  90. /**
  91. * Sends an action to update the current calendar api auth state in redux.
  92. * This is used only for microsoft implementation to store it auth state.
  93. *
  94. * @param {number} newState - The new state.
  95. * @returns {{
  96. * type: SET_CALENDAR_AUTH_STATE,
  97. * msAuthState: Object
  98. * }}
  99. */
  100. export function setCalendarAPIAuthState(newState: ?Object) {
  101. return {
  102. type: SET_CALENDAR_AUTH_STATE,
  103. msAuthState: newState
  104. };
  105. }
  106. /**
  107. * Sends an action to update the current calendar profile email state in redux.
  108. *
  109. * @param {number} newEmail - The new email.
  110. * @returns {{
  111. * type: SET_CALENDAR_PROFILE_EMAIL,
  112. * email: string
  113. * }}
  114. */
  115. export function setCalendarProfileEmail(newEmail: ?string) {
  116. return {
  117. type: SET_CALENDAR_PROFILE_EMAIL,
  118. email: newEmail
  119. };
  120. }
  121. /**
  122. * Sends an to denote a request in is flight to get calendar events.
  123. *
  124. * @param {boolean} isLoadingEvents - Whether or not calendar events are being
  125. * fetched.
  126. * @returns {{
  127. * type: SET_LOADING_CALENDAR_EVENTS,
  128. * isLoadingEvents: boolean
  129. * }}
  130. */
  131. export function setLoadingCalendarEvents(isLoadingEvents: boolean) {
  132. return {
  133. type: SET_LOADING_CALENDAR_EVENTS,
  134. isLoadingEvents
  135. };
  136. }
  137. /**
  138. * Sets the calendar integration type to be used by web and signals that the
  139. * integration is ready to be used.
  140. *
  141. * @param {string|undefined} integrationType - The calendar type.
  142. * @returns {{
  143. * type: SET_CALENDAR_INTEGRATION,
  144. * integrationReady: boolean,
  145. * integrationType: string
  146. * }}
  147. */
  148. export function setIntegrationReady(integrationType: string) {
  149. return {
  150. type: SET_CALENDAR_INTEGRATION,
  151. integrationReady: true,
  152. integrationType
  153. };
  154. }
  155. /**
  156. * Signals signing in to the specified calendar integration.
  157. *
  158. * @param {string} calendarType - The calendar integration which should be
  159. * signed into.
  160. * @returns {Function}
  161. */
  162. export function signIn(calendarType: string): Function {
  163. return (dispatch: Dispatch<*>) => {
  164. const integration = _getCalendarIntegration(calendarType);
  165. if (!integration) {
  166. return Promise.reject('No supported integration found');
  167. }
  168. return dispatch(integration.load())
  169. .then(() => dispatch(integration.signIn()))
  170. .then(() => dispatch(setIntegrationReady(calendarType)))
  171. .then(() => dispatch(updateProfile(calendarType)))
  172. .then(() => dispatch(refreshCalendar()))
  173. .then(() => sendAnalytics(createCalendarConnectedEvent()))
  174. .catch(error => {
  175. logger.error(
  176. 'Error occurred while signing into calendar integration',
  177. error);
  178. return Promise.reject(error);
  179. });
  180. };
  181. }
  182. /**
  183. * Updates calendar event by generating new invite URL and editing the event
  184. * adding some descriptive text and location.
  185. *
  186. * @param {string} id - The event id.
  187. * @param {string} calendarId - The id of the calendar to use.
  188. * @returns {Function}
  189. */
  190. export function updateCalendarEvent(id: string, calendarId: string): Function {
  191. return (dispatch: Dispatch<*>, getState: Function) => {
  192. const { integrationType } = getState()['features/calendar-sync'];
  193. const integration = _getCalendarIntegration(integrationType);
  194. if (!integration) {
  195. return Promise.reject('No integration found');
  196. }
  197. const { locationURL } = getState()['features/base/connection'];
  198. const newRoomName = generateRoomWithoutSeparator();
  199. let href = locationURL.href;
  200. href.endsWith('/') || (href += '/');
  201. const roomURL = `${href}${newRoomName}`;
  202. return dispatch(integration.updateCalendarEvent(
  203. id, calendarId, roomURL))
  204. .then(() => {
  205. // make a copy of the array
  206. const events
  207. = getState()['features/calendar-sync'].events.slice(0);
  208. const eventIx = events.findIndex(
  209. e => e.id === id && e.calendarId === calendarId);
  210. // clone the event we will modify
  211. const newEvent = Object.assign({}, events[eventIx]);
  212. newEvent.url = roomURL;
  213. events[eventIx] = newEvent;
  214. return dispatch(setCalendarEvents(events));
  215. });
  216. };
  217. }
  218. /**
  219. * Signals to get current profile data linked to the current calendar
  220. * integration that is in use.
  221. *
  222. * @param {string} calendarType - The calendar integration to which the profile
  223. * should be updated.
  224. * @returns {Function}
  225. */
  226. export function updateProfile(calendarType: string): Function {
  227. return (dispatch: Dispatch<*>) => {
  228. const integration = _getCalendarIntegration(calendarType);
  229. if (!integration) {
  230. return Promise.reject('No integration found');
  231. }
  232. return dispatch(integration.getCurrentEmail())
  233. .then(email => {
  234. dispatch(setCalendarProfileEmail(email));
  235. });
  236. };
  237. }