123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- // @flow
-
- import { loadGoogleAPI } from '../google-api';
-
- import {
- CLEAR_CALENDAR_INTEGRATION,
- REFRESH_CALENDAR,
- SET_CALENDAR_AUTH_STATE,
- SET_CALENDAR_AUTHORIZATION,
- SET_CALENDAR_EVENTS,
- SET_CALENDAR_INTEGRATION,
- SET_CALENDAR_PROFILE_EMAIL
- } from './actionTypes';
- import { _getCalendarIntegration, isCalendarEnabled } from './functions';
-
- const logger = require('jitsi-meet-logger').getLogger(__filename);
-
- /**
- * Sets the initial state of calendar integration by loading third party APIs
- * and filling out any data that needs to be fetched.
- *
- * @returns {Function}
- */
- export function bootstrapCalendarIntegration(): Function {
- return (dispatch, getState) => {
- const {
- googleApiApplicationClientID
- } = getState()['features/base/config'];
- const {
- integrationReady,
- integrationType
- } = getState()['features/calendar-sync'];
-
- if (!isCalendarEnabled()) {
- return Promise.reject();
- }
-
- return Promise.resolve()
- .then(() => {
- if (googleApiApplicationClientID) {
- return dispatch(
- loadGoogleAPI(googleApiApplicationClientID));
- }
- })
- .then(() => {
- if (!integrationType || integrationReady) {
- return;
- }
-
- const integrationToLoad
- = _getCalendarIntegration(integrationType);
-
- if (!integrationToLoad) {
- dispatch(clearCalendarIntegration());
-
- return;
- }
-
- return dispatch(integrationToLoad._isSignedIn())
- .then(signedIn => {
- if (signedIn) {
- dispatch(setIntegrationReady(integrationType));
- dispatch(updateProfile(integrationType));
- } else {
- dispatch(clearCalendarIntegration());
- }
- });
- });
- };
- }
-
- /**
- * Resets the state of calendar integration so stored events and selected
- * calendar type are cleared.
- *
- * @returns {{
- * type: CLEAR_CALENDAR_INTEGRATION
- * }}
- */
- export function clearCalendarIntegration() {
- return {
- type: CLEAR_CALENDAR_INTEGRATION
- };
- }
-
- /**
- * Sends an action to refresh the entry list (fetches new data).
- *
- * @param {boolean} forcePermission - Whether to force to re-ask for
- * the permission or not.
- * @param {boolean} isInteractive - If true this refresh was caused by
- * direct user interaction, false otherwise.
- * @returns {{
- * type: REFRESH_CALENDAR,
- * forcePermission: boolean,
- * isInteractive: boolean
- * }}
- */
- export function refreshCalendar(
- forcePermission: boolean = false, isInteractive: boolean = true) {
- return {
- type: REFRESH_CALENDAR,
- forcePermission,
- isInteractive
- };
- }
-
- /**
- * Sends an action to update the current calendar api auth state in redux.
- * This is used only for microsoft implementation to store it auth state.
- *
- * @param {number} newState - The new state.
- * @returns {{
- * type: SET_CALENDAR_AUTH_STATE,
- * msAuthState: Object
- * }}
- */
- export function setCalendarAPIAuthState(newState: ?Object) {
- return {
- type: SET_CALENDAR_AUTH_STATE,
- msAuthState: newState
- };
- }
-
- /**
- * Sends an action to signal that a calendar access has been requested. For more
- * info, see {@link SET_CALENDAR_AUTHORIZATION}.
- *
- * @param {string | undefined} authorization - The result of the last calendar
- * authorization request.
- * @returns {{
- * type: SET_CALENDAR_AUTHORIZATION,
- * authorization: ?string
- * }}
- */
- export function setCalendarAuthorization(authorization: ?string) {
- return {
- type: SET_CALENDAR_AUTHORIZATION,
- authorization
- };
- }
-
- /**
- * Sends an action to update the current calendar list in redux.
- *
- * @param {Array<Object>} events - The new list.
- * @returns {{
- * type: SET_CALENDAR_EVENTS,
- * events: Array<Object>
- * }}
- */
- export function setCalendarEvents(events: Array<Object>) {
- return {
- type: SET_CALENDAR_EVENTS,
- events
- };
- }
-
- /**
- * Sends an action to update the current calendar profile email state in redux.
- *
- * @param {number} newEmail - The new email.
- * @returns {{
- * type: SET_CALENDAR_PROFILE_EMAIL,
- * email: string
- * }}
- */
- export function setCalendarProfileEmail(newEmail: ?string) {
- return {
- type: SET_CALENDAR_PROFILE_EMAIL,
- email: newEmail
- };
- }
-
- /**
- * Sets the calendar integration type to be used by web and signals that the
- * integration is ready to be used.
- *
- * @param {string|undefined} integrationType - The calendar type.
- * @returns {{
- * type: SET_CALENDAR_INTEGRATION,
- * integrationReady: boolean,
- * integrationType: string
- * }}
- */
- export function setIntegrationReady(integrationType: string) {
- return {
- type: SET_CALENDAR_INTEGRATION,
- integrationReady: true,
- integrationType
- };
- }
-
- /**
- * Signals signing in to the specified calendar integration.
- *
- * @param {string} calendarType - The calendar integration which should be
- * signed into.
- * @returns {Function}
- */
- export function signIn(calendarType: string): Function {
- return (dispatch: Dispatch<*>) => {
- const integration = _getCalendarIntegration(calendarType);
-
- if (!integration) {
- return Promise.reject('No supported integration found');
- }
-
- return dispatch(integration.load())
- .then(() => dispatch(integration.signIn()))
- .then(() => dispatch(setIntegrationReady(calendarType)))
- .then(() => dispatch(updateProfile(calendarType)))
- .catch(error => {
- logger.error(
- 'Error occurred while signing into calendar integration',
- error);
-
- return Promise.reject(error);
- });
- };
- }
-
- /**
- * Signals to get current profile data linked to the current calendar
- * integration that is in use.
- *
- * @param {string} calendarType - The calendar integration to which the profile
- * should be updated.
- * @returns {Function}
- */
- export function updateProfile(calendarType: string): Function {
- return (dispatch: Dispatch<*>) => {
- const integration = _getCalendarIntegration(calendarType);
-
- if (!integration) {
- return Promise.reject('No integration found');
- }
-
- return dispatch(integration.getCurrentEmail())
- .then(email => {
- dispatch(setCalendarProfileEmail(email));
- });
- };
- }
|