From 5e8ecc5fee79f3ed5980af2626f6da85f8f96235 Mon Sep 17 00:00:00 2001 From: Lyubo Marinov Date: Mon, 14 May 2018 10:55:40 -0500 Subject: [PATCH] Coding style: naming, comments --- react/features/app/components/App.native.js | 1 - react/features/base/domains/actions.js | 21 ----- react/features/base/domains/constants.js | 15 --- react/features/base/domains/reducer.js | 49 ---------- .../{domains => known-domains}/actionTypes.js | 4 +- react/features/base/known-domains/actions.js | 22 +++++ .../base/{domains => known-domains}/index.js | 0 .../{domains => known-domains}/middleware.js | 31 +++---- react/features/base/known-domains/reducer.js | 70 ++++++++++++++ react/features/calendar-sync/middleware.js | 92 +++++++++---------- 10 files changed, 152 insertions(+), 153 deletions(-) delete mode 100644 react/features/base/domains/actions.js delete mode 100644 react/features/base/domains/constants.js delete mode 100644 react/features/base/domains/reducer.js rename react/features/base/{domains => known-domains}/actionTypes.js (57%) create mode 100644 react/features/base/known-domains/actions.js rename react/features/base/{domains => known-domains}/index.js (100%) rename react/features/base/{domains => known-domains}/middleware.js (50%) create mode 100644 react/features/base/known-domains/reducer.js diff --git a/react/features/app/components/App.native.js b/react/features/app/components/App.native.js index 5488a86b9..198da4b31 100644 --- a/react/features/app/components/App.native.js +++ b/react/features/app/components/App.native.js @@ -6,7 +6,6 @@ import { Linking } from 'react-native'; import '../../analytics'; import '../../authentication'; -import '../../base/domains'; import { Platform } from '../../base/react'; import { AspectRatioDetector, diff --git a/react/features/base/domains/actions.js b/react/features/base/domains/actions.js deleted file mode 100644 index fb8f262fc..000000000 --- a/react/features/base/domains/actions.js +++ /dev/null @@ -1,21 +0,0 @@ -// @flow - -import { ADD_KNOWN_DOMAINS } from './actionTypes'; - -/** - * Sends an action to add one or an array of known domains if not present yet. - * - * @param {string | Array} knownDomains - The new domain as a string or - * an array of domains. - * @returns {{ - * type: ADD_KNOWN_DOMAINS, - * knownDomains: Array - * }} - */ -export function addKnownDomains(knownDomains: string | Array) { - return { - type: ADD_KNOWN_DOMAINS, - knownDomains: typeof knownDomains === 'string' - ? [ knownDomains ] : knownDomains - }; -} diff --git a/react/features/base/domains/constants.js b/react/features/base/domains/constants.js deleted file mode 100644 index 56e7f34e6..000000000 --- a/react/features/base/domains/constants.js +++ /dev/null @@ -1,15 +0,0 @@ -// @flow - -/** - * A list of domains we consider jitsi-enabled domains by default. This is in - * line with the app links on iOS and Android, but stands here as retreiving - * those programatically is not straightforward. - */ -export const JITSI_KNOWN_DOMAINS = [ - 'beta.meet.jit.si', - 'beta.hipchat.me', - 'chaos.hipchat.me', - 'enso.me', - 'hipchat.me', - 'meet.jit.si' -]; diff --git a/react/features/base/domains/reducer.js b/react/features/base/domains/reducer.js deleted file mode 100644 index 43ef03379..000000000 --- a/react/features/base/domains/reducer.js +++ /dev/null @@ -1,49 +0,0 @@ -// @flow - -import { ReducerRegistry } from '../redux'; -import { PersistenceRegistry } from '../storage'; - -import { ADD_KNOWN_DOMAINS } from './actionTypes'; - -const DEFAULT_STATE = []; - -const STORE_NAME = 'features/base/domains'; - -PersistenceRegistry.register(STORE_NAME); - -ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => { - switch (action.type) { - case ADD_KNOWN_DOMAINS: - return _addKnownDomain(state, action); - - default: - return state; - } -}); - -/** - * Adds an array of new domains 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) { - const { knownDomains: knownDomainsToAdd } = action; - const knownDomains = Array.from(state); - - if (Array.isArray(knownDomainsToAdd)) { - for (let knownDomain of knownDomainsToAdd) { - knownDomain = knownDomain.toLowerCase(); - - if (knownDomains.indexOf(knownDomain) === -1) { - knownDomains.push(knownDomain); - } - } - - return knownDomains; - } - - return state; -} diff --git a/react/features/base/domains/actionTypes.js b/react/features/base/known-domains/actionTypes.js similarity index 57% rename from react/features/base/domains/actionTypes.js rename to react/features/base/known-domains/actionTypes.js index 97f5b9e22..aba92ad53 100644 --- a/react/features/base/domains/actionTypes.js +++ b/react/features/base/known-domains/actionTypes.js @@ -1,8 +1,8 @@ // @flow /** - * Action to add new domains to the list of domains known to the feature - * base/domains. + * The type of (redux) action to add known domains to the list of domains known + * to the feature base/known-domains. * * { * type: ADD_KNOWN_DOMAINS, diff --git a/react/features/base/known-domains/actions.js b/react/features/base/known-domains/actions.js new file mode 100644 index 000000000..aac8c5bdf --- /dev/null +++ b/react/features/base/known-domains/actions.js @@ -0,0 +1,22 @@ +// @flow + +import { ADD_KNOWN_DOMAINS } from './actionTypes'; + +/** + * Creates a (redux) action to add known domains to the list of domains known to + * the feature base/known-domains. + * + * @param {string | Array} knownDomains - The known domain(s) to add to + * the list of domains known to the feature base/known-domains. + * @returns {{ + * type: ADD_KNOWN_DOMAINS, + * knownDomains: Array + * }} + */ +export function addKnownDomains(knownDomains: string | Array) { + return { + type: ADD_KNOWN_DOMAINS, + knownDomains: + typeof knownDomains === 'string' ? [ knownDomains ] : knownDomains + }; +} diff --git a/react/features/base/domains/index.js b/react/features/base/known-domains/index.js similarity index 100% rename from react/features/base/domains/index.js rename to react/features/base/known-domains/index.js diff --git a/react/features/base/domains/middleware.js b/react/features/base/known-domains/middleware.js similarity index 50% rename from react/features/base/domains/middleware.js rename to react/features/base/known-domains/middleware.js index bd807f33b..d7904cafb 100644 --- a/react/features/base/domains/middleware.js +++ b/react/features/base/known-domains/middleware.js @@ -6,19 +6,17 @@ import { MiddlewareRegistry } from '../redux'; import { parseURIString } from '../util'; import { addKnownDomains } from './actions'; -import { JITSI_KNOWN_DOMAINS } from './constants'; MiddlewareRegistry.register(store => next => action => { const result = next(action); switch (action.type) { - case APP_WILL_MOUNT: - _ensureDefaultServer(store); + _appWillMount(store); break; case SET_ROOM: - _parseAndAddKnownDomain(store); + _setRoom(store); break; } @@ -26,34 +24,33 @@ MiddlewareRegistry.register(store => next => action => { }); /** - * Ensures presence of the default server in the known domains list. + * Adds the domain of the app's {@code defaultURL} to the list of domains known + * to the feature base/known-domains. * * @param {Object} store - The redux store. * @private * @returns {Promise} */ -function _ensureDefaultServer({ dispatch, getState }) { - const { app } = getState()['features/app']; - const defaultURL = parseURIString(app._getDefaultURL()); +function _appWillMount({ dispatch, getState }) { + const defaultURL + = parseURIString(getState()['features/app'].app._getDefaultURL()); - dispatch(addKnownDomains([ - defaultURL.host, - ...JITSI_KNOWN_DOMAINS - ])); + dispatch(addKnownDomains(defaultURL.host)); } /** - * Retrieves the domain name of a room upon join and stores it in the known - * domain list, if not present yet. + * Adds the domain of {@code locationURL} to the list of domains known to the + * feature base/known-domains. * * @param {Object} store - The redux store. * @private * @returns {Promise} */ -function _parseAndAddKnownDomain({ dispatch, getState }) { +function _setRoom({ dispatch, getState }) { const { locationURL } = getState()['features/base/connection']; + let host; locationURL - && locationURL.host - && dispatch(addKnownDomains(locationURL.host)); + && (host = locationURL.host) + && dispatch(addKnownDomains(host)); } diff --git a/react/features/base/known-domains/reducer.js b/react/features/base/known-domains/reducer.js new file mode 100644 index 000000000..3de77d160 --- /dev/null +++ b/react/features/base/known-domains/reducer.js @@ -0,0 +1,70 @@ +// @flow + +import { APP_WILL_MOUNT } from '../../app'; +import { ReducerRegistry } from '../redux'; +import { PersistenceRegistry } from '../storage'; + +import { ADD_KNOWN_DOMAINS } from './actionTypes'; + +/** + * The default list of domains known to the feature base/known-domains. + * Generally, it should be in sync with the domains associated with the app + * through its manifest (in other words, Universal Links, deep linking). Anyway, + * we need a hardcoded list because it has proven impossible to programmatically + * read the information out of the app's manifests: App Store strips the + * associated domains manifest out of the app so it's never downloaded on the + * client and we did not spend a lot of effort to read the associated domains + * out of the Andorid manifest. + */ +export const DEFAULT_STATE = [ + 'beta.hipchat.me', + 'beta.meet.jit.si', + 'chaos.hipchat.me', + 'enso.me', + 'hipchat.me', + 'meet.jit.si' +]; + +const STORE_NAME = 'features/base/known-domains'; + +PersistenceRegistry.register(STORE_NAME); + +ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => { + switch (action.type) { + case ADD_KNOWN_DOMAINS: + return _addKnownDomains(state, action.knownDomains); + + case APP_WILL_MOUNT: + // In case persistence has deserialized a weird redux state: + return _addKnownDomains(state, DEFAULT_STATE); + + default: + return state; + } +}); + +/** + * Adds an array of known domains to the list of domains known to the feature + * base/known-domains. + * + * @param {Object} state - The redux state. + * @param {Array} knownDomains - The array of known domains to add to + * the list of domains known to the feature base/known-domains. + * @private + * @returns {Object} The next redux state. + */ +function _addKnownDomains(state, knownDomains) { + // In case persistence has deserialized a weird redux state: + let nextState = Array.isArray(state) ? state : []; + + if (Array.isArray(knownDomains)) { + nextState = Array.from(state); + for (let knownDomain of knownDomains) { + knownDomain = knownDomain.toLowerCase(); + nextState.indexOf(knownDomain) === -1 + && nextState.push(knownDomain); + } + } + + return nextState; +} diff --git a/react/features/calendar-sync/middleware.js b/react/features/calendar-sync/middleware.js index 10fc963ab..1407cd10d 100644 --- a/react/features/calendar-sync/middleware.js +++ b/react/features/calendar-sync/middleware.js @@ -3,27 +3,24 @@ import RNCalendarEvents from 'react-native-calendar-events'; import { APP_WILL_MOUNT } from '../app'; -import { ADD_KNOWN_DOMAINS } from '../base/domains'; +import { ADD_KNOWN_DOMAINS } from '../base/known-domains'; import { MiddlewareRegistry } from '../base/redux'; import { APP_LINK_SCHEME, parseURIString } from '../base/util'; import { APP_STATE_CHANGED } from '../mobile/background'; -import { - setCalendarAuthorization, - setCalendarEvents -} from './actions'; +import { setCalendarAuthorization, setCalendarEvents } from './actions'; import { REFRESH_CALENDAR } from './actionTypes'; import { CALENDAR_ENABLED } from './constants'; const logger = require('jitsi-meet-logger').getLogger(__filename); /** - * The no. of days to fetch. + * The number of days to fetch. */ const FETCH_END_DAYS = 10; /** - * The no. of days to go back when fetching. + * The number of days to go back when fetching. */ const FETCH_START_DAYS = -1; @@ -37,15 +34,15 @@ CALENDAR_ENABLED const result = next(action); switch (action.type) { - case APP_STATE_CHANGED: - _maybeClearAccessStatus(store, action); - break; - case ADD_KNOWN_DOMAINS: case APP_WILL_MOUNT: _fetchCalendarEntries(store, false, false); break; + case APP_STATE_CHANGED: + _maybeClearAccessStatus(store, action); + break; + case REFRESH_CALENDAR: _fetchCalendarEntries(store, true, action.forcePermission); break; @@ -54,23 +51,6 @@ CALENDAR_ENABLED return result; }); -/** - * 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 }) { - if (appState === 'background') { - store.dispatch(setCalendarAuthorization(undefined)); - } -} - /** * Ensures calendar access if possible and resolves the promise if it's granted. * @@ -113,13 +93,13 @@ function _ensureCalendarAccess(promptForPermission, dispatch) { * @returns {void} */ function _fetchCalendarEntries( - { dispatch, getState }, + store, maybePromptForPermission, forcePermission) { - const featureState = getState()['features/calendar-sync']; - const knownDomains = getState()['features/base/domains']; + const { dispatch, getState } = store; const promptForPermission - = (maybePromptForPermission && !featureState.authorization) + = (maybePromptForPermission + && !getState()['features/calendar-sync'].authorization) || forcePermission; _ensureCalendarAccess(promptForPermission, dispatch) @@ -135,11 +115,7 @@ function _fetchCalendarEntries( startDate.getTime(), endDate.getTime(), []) - .then(events => - _updateCalendarEntries( - events, - knownDomains, - dispatch)) + .then(_updateCalendarEntries.bind(store)) .catch(error => logger.error('Error fetching calendar.', error)); } else { @@ -192,6 +168,22 @@ function _getURLFromEvent(event, knownDomains) { return null; } +/** + * 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)); +} + /** * Updates the calendar entries in Redux when new list is received. * @@ -231,26 +223,30 @@ function _parseCalendarEntry(event, knownDomains) { } /** - * Updates the calendar entries in Redux when new list is received. + * Updates the calendar entries in redux when new list is received. + * + * XXX The function's {@code this} is the redux store. * * @param {Array} events - The new event list. - * @param {Array} knownDomains - The known domain list. - * @param {Function} dispatch - The Redux dispatch function. * @private * @returns {void} */ -function _updateCalendarEntries(events, knownDomains, dispatch) { +function _updateCalendarEntries(events) { if (events && events.length) { + // eslint-disable-next-line no-invalid-this + const { dispatch, getState } = this; + + const knownDomains = getState()['features/base/known-domains']; const eventList = []; - for (const event of events) { - const calendarEntry - = _parseCalendarEntry(event, knownDomains); - const now = Date.now(); + const now = Date.now(); - if (calendarEntry && calendarEntry.endDate > now) { - eventList.push(calendarEntry); - } + for (const event of events) { + const calendarEntry = _parseCalendarEntry(event, knownDomains); + + calendarEntry + && calendarEntry.endDate > now + && eventList.push(calendarEntry); } dispatch(