jiti-meet/react/features/calendar-sync/middleware.js

201 lines
5.8 KiB
JavaScript
Raw Normal View History

2018-02-08 18:50:19 +00:00
// @flow
import Logger from 'jitsi-meet-logger';
import RNCalendarEvents from 'react-native-calendar-events';
2018-02-14 16:21:52 +00:00
import { SET_ROOM } from '../base/conference';
2018-02-08 18:50:19 +00:00
import { MiddlewareRegistry } from '../base/redux';
2018-02-14 16:21:52 +00:00
import { parseURIString } from '../base/util';
2018-02-08 18:50:19 +00:00
import { APP_WILL_MOUNT } from '../app';
2018-02-14 16:21:52 +00:00
import { maybeAddNewKnownDomain, updateCalendarEntryList } from './actions';
2018-02-14 16:50:48 +00:00
import { REFRESH_CALENDAR_ENTRY_LIST } from './actionTypes';
2018-02-08 18:50:19 +00:00
const FETCH_END_DAYS = 10;
const FETCH_START_DAYS = -1;
const MAX_LIST_LENGTH = 10;
const logger = Logger.getLogger(__filename);
MiddlewareRegistry.register(store => next => action => {
const result = next(action);
switch (action.type) {
case APP_WILL_MOUNT:
2018-02-14 16:21:52 +00:00
_ensureDefaultServer(store);
2018-02-08 18:50:19 +00:00
_fetchCalendarEntries(store);
2018-02-14 16:21:52 +00:00
break;
2018-02-14 16:50:48 +00:00
case REFRESH_CALENDAR_ENTRY_LIST:
_fetchCalendarEntries(store);
break;
2018-02-14 16:21:52 +00:00
case SET_ROOM:
_parseAndAddDomain(store);
2018-02-08 18:50:19 +00:00
}
return result;
});
/**
* Ensures calendar access if possible and resolves the promise if it's granted.
*
* @private
* @returns {Promise}
*/
function _ensureCalendarAccess() {
return new Promise((resolve, reject) => {
RNCalendarEvents.authorizationStatus()
.then(status => {
if (status === 'authorized') {
resolve();
} else if (status === 'undetermined') {
RNCalendarEvents.authorizeEventStore()
.then(result => {
if (result === 'authorized') {
resolve();
} else {
reject(result);
}
})
.catch(error => {
reject(error);
});
} else {
reject(status);
}
})
.catch(error => {
reject(error);
});
});
}
2018-02-14 16:21:52 +00:00
/**
* Ensures presence of the default server in the known domains list.
*
* @private
* @param {Object} store - The redux store.
* @returns {Promise}
*/
function _ensureDefaultServer(store) {
const state = store.getState();
const defaultURL = parseURIString(
state['features/app'].app._getDefaultURL()
);
store.dispatch(maybeAddNewKnownDomain(defaultURL.host));
}
2018-02-08 18:50:19 +00:00
/**
* Reads the user's calendar and updates the stored entries if need be.
*
* @private
* @param {Object} store - The redux store.
* @returns {void}
*/
function _fetchCalendarEntries(store) {
_ensureCalendarAccess()
.then(() => {
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(),
[]
)
.then(events => {
2018-02-14 16:21:52 +00:00
const { knownDomains } = store.getState()['features/calendar-sync'];
2018-02-08 18:50:19 +00:00
const eventList = [];
if (events && events.length) {
for (const event of events) {
2018-02-14 16:21:52 +00:00
const jitsiURL = _getURLFromEvent(event, knownDomains);
2018-02-08 18:50:19 +00:00
const now = Date.now();
if (jitsiURL) {
const eventStartDate = Date.parse(event.startDate);
const eventEndDate = Date.parse(event.endDate);
if (isNaN(eventStartDate) || isNaN(eventEndDate)) {
logger.warn(
'Skipping calendar event due to invalid dates',
event.title,
event.startDate,
event.endDate
);
} else if (eventEndDate > now) {
eventList.push({
endDate: eventEndDate,
id: event.id,
startDate: eventStartDate,
title: event.title,
url: jitsiURL
});
}
}
}
}
store.dispatch(updateCalendarEntryList(eventList.sort((a, b) =>
a.startDate - b.startDate
).slice(0, MAX_LIST_LENGTH)));
})
.catch(error => {
logger.error('Error fetching calendar.', error);
});
})
.catch(reason => {
logger.error('Error accessing calendar.', reason);
});
}
/**
* Retreives a jitsi URL from an event if present.
*
* @private
* @param {Object} event - The event to parse.
2018-02-14 16:21:52 +00:00
* @param {Array<string>} knownDomains - The known domain names.
2018-02-08 18:50:19 +00:00
* @returns {string}
*
*/
2018-02-14 16:21:52 +00:00
function _getURLFromEvent(event, knownDomains) {
2018-02-08 18:50:19 +00:00
const urlRegExp
2018-02-14 16:21:52 +00:00
= new RegExp(`http(s)?://(${knownDomains.join('|')})/[^\\s<>$]+`, 'gi');
2018-02-08 18:50:19 +00:00
const fieldsToSearch = [
event.title,
event.url,
event.location,
event.notes,
event.description
];
let matchArray;
for (const field of fieldsToSearch) {
if (typeof field === 'string') {
if (
(matchArray = urlRegExp.exec(field)) !== null
) {
return matchArray[0];
}
}
}
return null;
}
2018-02-14 16:21:52 +00:00
/**
* Retreives the domain name of a room upon join and stores it
* in the known domain list, if not present yet.
*
* @private
* @param {Object} store - The redux store.
* @returns {Promise}
*/
function _parseAndAddDomain(store) {
const { locationURL } = store.getState()['features/base/connection'];
store.dispatch(maybeAddNewKnownDomain(locationURL.host));
}