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.js 8.3KB

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