2018-02-08 18:50:19 +00:00
|
|
|
// @flow
|
2018-04-16 02:04:57 +00:00
|
|
|
|
2018-02-08 18:50:19 +00:00
|
|
|
import RNCalendarEvents from 'react-native-calendar-events';
|
|
|
|
|
2018-03-20 13:51:51 +00:00
|
|
|
import { APP_WILL_MOUNT } from '../app';
|
2018-05-14 19:05:41 +00:00
|
|
|
import { ADD_KNOWN_DOMAINS, addKnownDomains } from '../base/known-domains';
|
2018-02-08 18:50:19 +00:00
|
|
|
import { MiddlewareRegistry } from '../base/redux';
|
2018-02-14 20:34:33 +00:00
|
|
|
import { APP_LINK_SCHEME, parseURIString } from '../base/util';
|
2018-03-20 13:51:51 +00:00
|
|
|
import { APP_STATE_CHANGED } from '../mobile/background';
|
2018-02-08 18:50:19 +00:00
|
|
|
|
2018-05-14 15:55:40 +00:00
|
|
|
import { setCalendarAuthorization, setCalendarEvents } from './actions';
|
2018-04-16 02:04:57 +00:00
|
|
|
import { REFRESH_CALENDAR } from './actionTypes';
|
2018-04-16 16:39:26 +00:00
|
|
|
import { CALENDAR_ENABLED } from './constants';
|
2018-04-16 02:04:57 +00:00
|
|
|
|
|
|
|
const logger = require('jitsi-meet-logger').getLogger(__filename);
|
2018-02-08 18:50:19 +00:00
|
|
|
|
2018-05-11 15:29:09 +00:00
|
|
|
/**
|
2018-05-14 15:55:40 +00:00
|
|
|
* The number of days to fetch.
|
2018-05-11 15:29:09 +00:00
|
|
|
*/
|
2018-02-08 18:50:19 +00:00
|
|
|
const FETCH_END_DAYS = 10;
|
2018-05-11 15:29:09 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-14 15:55:40 +00:00
|
|
|
* The number of days to go back when fetching.
|
2018-05-11 15:29:09 +00:00
|
|
|
*/
|
2018-02-08 18:50:19 +00:00
|
|
|
const FETCH_START_DAYS = -1;
|
2018-05-11 15:29:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The max number of events to fetch from the calendar.
|
|
|
|
*/
|
2018-02-08 18:50:19 +00:00
|
|
|
const MAX_LIST_LENGTH = 10;
|
|
|
|
|
2018-04-16 16:39:26 +00:00
|
|
|
CALENDAR_ENABLED
|
|
|
|
&& MiddlewareRegistry.register(store => next => action => {
|
|
|
|
switch (action.type) {
|
2018-05-14 19:05:41 +00:00
|
|
|
case ADD_KNOWN_DOMAINS: {
|
|
|
|
// XXX Fetch new calendar entries only when an actual domain has
|
|
|
|
// become known.
|
|
|
|
const { getState } = store;
|
|
|
|
const oldValue = getState()['features/base/known-domains'];
|
|
|
|
const result = next(action);
|
|
|
|
const newValue = getState()['features/base/known-domains'];
|
|
|
|
|
|
|
|
oldValue === newValue || _fetchCalendarEntries(store, false, false);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
case APP_STATE_CHANGED: {
|
|
|
|
const result = next(action);
|
2018-04-16 02:04:57 +00:00
|
|
|
|
2018-05-14 15:55:40 +00:00
|
|
|
_maybeClearAccessStatus(store, action);
|
|
|
|
|
2018-05-14 19:05:41 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
case APP_WILL_MOUNT: {
|
|
|
|
// For legacy purposes, we've allowed the deserialization of
|
|
|
|
// knownDomains and now we're to translate it to base/known-domains.
|
|
|
|
const state = store.getState()['features/calendar-sync'];
|
|
|
|
|
|
|
|
if (state) {
|
|
|
|
const { knownDomains } = state;
|
|
|
|
|
|
|
|
Array.isArray(knownDomains)
|
|
|
|
&& knownDomains.length
|
|
|
|
&& store.dispatch(addKnownDomains(knownDomains));
|
|
|
|
}
|
|
|
|
|
|
|
|
_fetchCalendarEntries(store, false, false);
|
|
|
|
|
2018-05-31 09:00:33 +00:00
|
|
|
return next(action);
|
2018-05-14 19:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case REFRESH_CALENDAR: {
|
|
|
|
const result = next(action);
|
|
|
|
|
2018-04-16 16:39:26 +00:00
|
|
|
_fetchCalendarEntries(store, true, action.forcePermission);
|
2018-05-14 19:05:41 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2018-04-16 16:39:26 +00:00
|
|
|
}
|
2018-02-08 18:50:19 +00:00
|
|
|
|
2018-05-14 19:05:41 +00:00
|
|
|
return next(action);
|
2018-04-16 16:39:26 +00:00
|
|
|
});
|
2018-02-08 18:50:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures calendar access if possible and resolves the promise if it's granted.
|
|
|
|
*
|
2018-03-06 16:30:59 +00:00
|
|
|
* @param {boolean} promptForPermission - Flag to tell the app if it should
|
|
|
|
* prompt for a calendar permission if it wasn't granted yet.
|
2018-03-20 13:51:51 +00:00
|
|
|
* @param {Function} dispatch - The Redux dispatch function.
|
2018-04-16 02:04:57 +00:00
|
|
|
* @private
|
2018-02-08 18:50:19 +00:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2018-03-20 13:51:51 +00:00
|
|
|
function _ensureCalendarAccess(promptForPermission, dispatch) {
|
2018-02-08 18:50:19 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RNCalendarEvents.authorizationStatus()
|
|
|
|
.then(status => {
|
|
|
|
if (status === 'authorized') {
|
2018-03-20 13:51:51 +00:00
|
|
|
resolve(true);
|
2018-03-06 16:30:59 +00:00
|
|
|
} else if (promptForPermission) {
|
2018-02-08 18:50:19 +00:00
|
|
|
RNCalendarEvents.authorizeEventStore()
|
|
|
|
.then(result => {
|
2018-04-16 02:04:57 +00:00
|
|
|
dispatch(setCalendarAuthorization(result));
|
2018-03-20 13:51:51 +00:00
|
|
|
resolve(result === 'authorized');
|
2018-02-08 18:50:19 +00:00
|
|
|
})
|
2018-04-16 02:04:57 +00:00
|
|
|
.catch(reject);
|
2018-02-08 18:50:19 +00:00
|
|
|
} else {
|
2018-03-20 13:51:51 +00:00
|
|
|
resolve(false);
|
2018-02-08 18:50:19 +00:00
|
|
|
}
|
|
|
|
})
|
2018-04-16 02:04:57 +00:00
|
|
|
.catch(reject);
|
2018-02-08 18:50:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the user's calendar and updates the stored entries if need be.
|
|
|
|
*
|
|
|
|
* @param {Object} store - The redux store.
|
2018-03-20 13:51:51 +00:00
|
|
|
* @param {boolean} maybePromptForPermission - Flag to tell the app if it should
|
2018-03-06 16:30:59 +00:00
|
|
|
* prompt for a calendar permission if it wasn't granted yet.
|
2018-04-16 02:04:57 +00:00
|
|
|
* @param {boolean|undefined} forcePermission - Whether to force to re-ask for
|
|
|
|
* the permission or not.
|
|
|
|
* @private
|
2018-02-08 18:50:19 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-03-20 13:51:51 +00:00
|
|
|
function _fetchCalendarEntries(
|
2018-05-14 15:55:40 +00:00
|
|
|
store,
|
2018-03-20 13:51:51 +00:00
|
|
|
maybePromptForPermission,
|
2018-04-16 02:04:57 +00:00
|
|
|
forcePermission) {
|
2018-05-14 15:55:40 +00:00
|
|
|
const { dispatch, getState } = store;
|
2018-03-20 13:51:51 +00:00
|
|
|
const promptForPermission
|
2018-05-14 15:55:40 +00:00
|
|
|
= (maybePromptForPermission
|
|
|
|
&& !getState()['features/calendar-sync'].authorization)
|
2018-04-16 02:04:57 +00:00
|
|
|
|| forcePermission;
|
2018-03-20 13:51:51 +00:00
|
|
|
|
|
|
|
_ensureCalendarAccess(promptForPermission, dispatch)
|
2018-04-16 02:04:57 +00:00
|
|
|
.then(accessGranted => {
|
|
|
|
if (accessGranted) {
|
|
|
|
const startDate = new Date();
|
|
|
|
const endDate = new Date();
|
|
|
|
|
|
|
|
startDate.setDate(startDate.getDate() + FETCH_START_DAYS);
|
|
|
|
endDate.setDate(endDate.getDate() + FETCH_END_DAYS);
|
|
|
|
|
|
|
|
RNCalendarEvents.fetchAllEvents(
|
|
|
|
startDate.getTime(),
|
|
|
|
endDate.getTime(),
|
|
|
|
[])
|
2018-05-14 15:55:40 +00:00
|
|
|
.then(_updateCalendarEntries.bind(store))
|
2018-04-16 02:04:57 +00:00
|
|
|
.catch(error =>
|
|
|
|
logger.error('Error fetching calendar.', error));
|
|
|
|
} else {
|
|
|
|
logger.warn('Calendar access not granted.');
|
|
|
|
}
|
|
|
|
})
|
2018-05-14 19:05:41 +00:00
|
|
|
.catch(reason => logger.error('Error accessing calendar.', reason));
|
2018-02-08 18:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-16 23:07:25 +00:00
|
|
|
* Retrieves a Jitsi Meet URL from an event if present.
|
2018-02-08 18:50:19 +00:00
|
|
|
*
|
|
|
|
* @param {Object} event - The event to parse.
|
2018-02-14 16:21:52 +00:00
|
|
|
* @param {Array<string>} knownDomains - The known domain names.
|
2018-04-16 02:04:57 +00:00
|
|
|
* @private
|
2018-02-08 18:50:19 +00:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2018-02-14 16:21:52 +00:00
|
|
|
function _getURLFromEvent(event, knownDomains) {
|
2018-02-14 20:34:33 +00:00
|
|
|
const linkTerminatorPattern = '[^\\s<>$]';
|
2018-02-08 18:50:19 +00:00
|
|
|
const urlRegExp
|
2018-04-16 02:04:57 +00:00
|
|
|
= new RegExp(
|
|
|
|
`http(s)?://(${knownDomains.join('|')})/${linkTerminatorPattern}+`,
|
|
|
|
'gi');
|
2018-02-14 20:34:33 +00:00
|
|
|
const schemeRegExp
|
|
|
|
= new RegExp(`${APP_LINK_SCHEME}${linkTerminatorPattern}+`, 'gi');
|
2018-02-08 18:50:19 +00:00
|
|
|
const fieldsToSearch = [
|
|
|
|
event.title,
|
|
|
|
event.url,
|
|
|
|
event.location,
|
|
|
|
event.notes,
|
|
|
|
event.description
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const field of fieldsToSearch) {
|
|
|
|
if (typeof field === 'string') {
|
2018-04-16 02:04:57 +00:00
|
|
|
const matches = urlRegExp.exec(field) || schemeRegExp.exec(field);
|
|
|
|
|
|
|
|
if (matches) {
|
2018-04-10 10:44:10 +00:00
|
|
|
const url = parseURIString(matches[0]);
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
return url.toString();
|
|
|
|
}
|
2018-02-08 18:50:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-02-14 16:21:52 +00:00
|
|
|
|
2018-05-14 15:55:40 +00:00
|
|
|
/**
|
|
|
|
* Clears the calendar access status when the app comes back from the
|
|
|
|
* background. This is needed as some users may never quit the app, but puts it
|
|
|
|
* into the background and we need to try to request for a permission as often
|
|
|
|
* as possible, but not annoyingly often.
|
|
|
|
*
|
|
|
|
* @param {Object} store - The redux store.
|
|
|
|
* @param {Object} action - The Redux action.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function _maybeClearAccessStatus(store, { appState }) {
|
|
|
|
appState === 'background'
|
|
|
|
&& store.dispatch(setCalendarAuthorization(undefined));
|
|
|
|
}
|
|
|
|
|
2018-03-20 13:51:51 +00:00
|
|
|
/**
|
|
|
|
* Updates the calendar entries in Redux when new list is received.
|
|
|
|
*
|
|
|
|
* @param {Object} event - An event returned from the native calendar.
|
|
|
|
* @param {Array<string>} knownDomains - The known domain list.
|
2018-04-16 02:04:57 +00:00
|
|
|
* @private
|
2018-03-20 13:51:51 +00:00
|
|
|
* @returns {CalendarEntry}
|
|
|
|
*/
|
|
|
|
function _parseCalendarEntry(event, knownDomains) {
|
|
|
|
if (event) {
|
2018-04-16 02:04:57 +00:00
|
|
|
const url = _getURLFromEvent(event, knownDomains);
|
2018-03-20 13:51:51 +00:00
|
|
|
|
2018-04-16 02:04:57 +00:00
|
|
|
if (url) {
|
|
|
|
const startDate = Date.parse(event.startDate);
|
|
|
|
const endDate = Date.parse(event.endDate);
|
2018-03-20 13:51:51 +00:00
|
|
|
|
2018-04-16 02:04:57 +00:00
|
|
|
if (isNaN(startDate) || isNaN(endDate)) {
|
2018-03-20 13:51:51 +00:00
|
|
|
logger.warn(
|
|
|
|
'Skipping invalid calendar event',
|
|
|
|
event.title,
|
|
|
|
event.startDate,
|
|
|
|
event.endDate
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return {
|
2018-04-16 02:04:57 +00:00
|
|
|
endDate,
|
2018-03-20 13:51:51 +00:00
|
|
|
id: event.id,
|
2018-04-16 02:04:57 +00:00
|
|
|
startDate,
|
2018-03-20 13:51:51 +00:00
|
|
|
title: event.title,
|
2018-04-16 02:04:57 +00:00
|
|
|
url
|
2018-03-20 13:51:51 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-14 15:55:40 +00:00
|
|
|
* Updates the calendar entries in redux when new list is received.
|
|
|
|
*
|
|
|
|
* XXX The function's {@code this} is the redux store.
|
2018-03-20 13:51:51 +00:00
|
|
|
*
|
|
|
|
* @param {Array<CalendarEntry>} events - The new event list.
|
2018-04-16 02:04:57 +00:00
|
|
|
* @private
|
2018-03-20 13:51:51 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-05-14 15:55:40 +00:00
|
|
|
function _updateCalendarEntries(events) {
|
2018-03-20 13:51:51 +00:00
|
|
|
if (events && events.length) {
|
2018-05-14 15:55:40 +00:00
|
|
|
// eslint-disable-next-line no-invalid-this
|
|
|
|
const { dispatch, getState } = this;
|
|
|
|
|
|
|
|
const knownDomains = getState()['features/base/known-domains'];
|
2018-03-20 13:51:51 +00:00
|
|
|
const eventList = [];
|
|
|
|
|
2018-05-14 15:55:40 +00:00
|
|
|
const now = Date.now();
|
|
|
|
|
2018-03-20 13:51:51 +00:00
|
|
|
for (const event of events) {
|
2018-05-14 15:55:40 +00:00
|
|
|
const calendarEntry = _parseCalendarEntry(event, knownDomains);
|
2018-03-20 13:51:51 +00:00
|
|
|
|
2018-05-14 15:55:40 +00:00
|
|
|
calendarEntry
|
|
|
|
&& calendarEntry.endDate > now
|
|
|
|
&& eventList.push(calendarEntry);
|
2018-03-20 13:51:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 02:04:57 +00:00
|
|
|
dispatch(
|
|
|
|
setCalendarEvents(
|
|
|
|
eventList
|
|
|
|
.sort((a, b) => a.startDate - b.startDate)
|
|
|
|
.slice(0, MAX_LIST_LENGTH)));
|
2018-03-20 13:51:51 +00:00
|
|
|
}
|
|
|
|
}
|