[RN] If calendar-sync knows a domain, then the app knows it

This commit is contained in:
Zoltan Bettenbuk 2018-05-11 17:29:09 +02:00 committed by Lyubo Marinov
parent ee94d79ee6
commit 46bc63b79e
4 changed files with 18 additions and 117 deletions

View File

@ -1,16 +1,5 @@
// @flow
/**
* Action to add a new domain to the list of domain known to the feature
* calendar-sync.
*
* {
* type: ADD_KNOWN_DOMAIN,
* knownDomain: string
* }
*/
export const ADD_KNOWN_DOMAIN = Symbol('ADD_KNOWN_DOMAIN');
/**
* Action to refresh (re-fetch) the entry list.
*

View File

@ -3,26 +3,9 @@
import {
SET_CALENDAR_AUTHORIZATION,
SET_CALENDAR_EVENTS,
ADD_KNOWN_DOMAIN,
REFRESH_CALENDAR
} from './actionTypes';
/**
* Sends an action to add a new known domain if not present yet.
*
* @param {string} knownDomain - The new domain.
* @returns {{
* type: ADD_KNOWN_DOMAIN,
* knownDomain: string
* }}
*/
export function addKnownDomain(knownDomain: string) {
return {
type: ADD_KNOWN_DOMAIN,
knownDomain
};
}
/**
* Sends an action to refresh the entry list (fetches new data).
*

View File

@ -3,13 +3,12 @@
import RNCalendarEvents from 'react-native-calendar-events';
import { APP_WILL_MOUNT } from '../app';
import { SET_ROOM } from '../base/conference';
import { ADD_KNOWN_DOMAINS } from '../base/domains';
import { MiddlewareRegistry } from '../base/redux';
import { APP_LINK_SCHEME, parseURIString } from '../base/util';
import { APP_STATE_CHANGED } from '../mobile/background';
import {
addKnownDomain,
setCalendarAuthorization,
setCalendarEvents
} from './actions';
@ -18,8 +17,19 @@ import { CALENDAR_ENABLED } from './constants';
const logger = require('jitsi-meet-logger').getLogger(__filename);
/**
* The no. of days to fetch.
*/
const FETCH_END_DAYS = 10;
/**
* The no. of days to go back when fetching.
*/
const FETCH_START_DAYS = -1;
/**
* The max number of events to fetch from the calendar.
*/
const MAX_LIST_LENGTH = 10;
CALENDAR_ENABLED
@ -31,18 +41,14 @@ CALENDAR_ENABLED
_maybeClearAccessStatus(store, action);
break;
case ADD_KNOWN_DOMAINS:
case APP_WILL_MOUNT:
_ensureDefaultServer(store);
_fetchCalendarEntries(store, false, false);
break;
case REFRESH_CALENDAR:
_fetchCalendarEntries(store, true, action.forcePermission);
break;
case SET_ROOM:
_parseAndAddKnownDomain(store);
break;
}
return result;
@ -95,20 +101,6 @@ function _ensureCalendarAccess(promptForPermission, dispatch) {
});
}
/**
* Ensures presence of the default server in the known domains list.
*
* @param {Object} store - The redux store.
* @private
* @returns {Promise}
*/
function _ensureDefaultServer({ dispatch, getState }) {
const defaultURL
= parseURIString(getState()['features/app'].app._getDefaultURL());
dispatch(addKnownDomain(defaultURL.host));
}
/**
* Reads the user's calendar and updates the stored entries if need be.
*
@ -124,9 +116,10 @@ function _fetchCalendarEntries(
{ dispatch, getState },
maybePromptForPermission,
forcePermission) {
const state = getState()['features/calendar-sync'];
const featureState = getState()['features/calendar-sync'];
const knownDomains = getState()['features/base/domains'];
const promptForPermission
= (maybePromptForPermission && !state.authorization)
= (maybePromptForPermission && !featureState.authorization)
|| forcePermission;
_ensureCalendarAccess(promptForPermission, dispatch)
@ -145,7 +138,7 @@ function _fetchCalendarEntries(
.then(events =>
_updateCalendarEntries(
events,
state.knownDomains,
knownDomains,
dispatch))
.catch(error =>
logger.error('Error fetching calendar.', error));
@ -199,20 +192,6 @@ function _getURLFromEvent(event, knownDomains) {
return null;
}
/**
* Retrieves the domain name of a room upon join and stores it in the known
* domain list, if not present yet.
*
* @param {Object} store - The redux store.
* @private
* @returns {Promise}
*/
function _parseAndAddKnownDomain({ dispatch, getState }) {
const { locationURL } = getState()['features/base/connection'];
dispatch(addKnownDomain(locationURL.host));
}
/**
* Updates the calendar entries in Redux when new list is received.
*

View File

@ -1,10 +1,8 @@
// @flow
import { ReducerRegistry } from '../base/redux';
import { PersistenceRegistry } from '../base/storage';
import {
ADD_KNOWN_DOMAIN,
SET_CALENDAR_AUTHORIZATION,
SET_CALENDAR_EVENTS
} from './actionTypes';
@ -17,24 +15,14 @@ const DEFAULT_STATE = {
* need to re-request the calendar permission from the user.
*/
authorization: undefined,
events: [],
knownDomains: []
events: []
};
const MAX_DOMAIN_LIST_SIZE = 10;
const STORE_NAME = 'features/calendar-sync';
CALENDAR_ENABLED
&& PersistenceRegistry.register(STORE_NAME, {
knownDomains: true
});
CALENDAR_ENABLED
&& ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
switch (action.type) {
case ADD_KNOWN_DOMAIN:
return _addKnownDomain(state, action);
case SET_CALENDAR_AUTHORIZATION:
return {
@ -52,41 +40,3 @@ CALENDAR_ENABLED
return state;
}
});
/**
* Adds a new domain to the known domain list if not present yet.
*
* @param {Object} state - The redux state.
* @param {Object} action - The redux action.
* @private
* @returns {Object}
*/
function _addKnownDomain(state, action) {
let { knownDomain } = action;
if (knownDomain) {
knownDomain = knownDomain.toLowerCase();
let { knownDomains } = state;
if (knownDomains.indexOf(knownDomain) === -1) {
// Add the specified known domain and at the same time avoid
// modifying the knownDomains Array instance referenced by the
// current redux state.
knownDomains = [
...state.knownDomains,
knownDomain
];
// Ensure the list doesn't exceed a/the maximum size.
knownDomains.splice(0, knownDomains.length - MAX_DOMAIN_LIST_SIZE);
return {
...state,
knownDomains
};
}
}
return state;
}