|
@@ -3,27 +3,24 @@
|
3
|
3
|
import RNCalendarEvents from 'react-native-calendar-events';
|
4
|
4
|
|
5
|
5
|
import { APP_WILL_MOUNT } from '../app';
|
6
|
|
-import { ADD_KNOWN_DOMAINS } from '../base/domains';
|
|
6
|
+import { ADD_KNOWN_DOMAINS } from '../base/known-domains';
|
7
|
7
|
import { MiddlewareRegistry } from '../base/redux';
|
8
|
8
|
import { APP_LINK_SCHEME, parseURIString } from '../base/util';
|
9
|
9
|
import { APP_STATE_CHANGED } from '../mobile/background';
|
10
|
10
|
|
11
|
|
-import {
|
12
|
|
- setCalendarAuthorization,
|
13
|
|
- setCalendarEvents
|
14
|
|
-} from './actions';
|
|
11
|
+import { setCalendarAuthorization, setCalendarEvents } from './actions';
|
15
|
12
|
import { REFRESH_CALENDAR } from './actionTypes';
|
16
|
13
|
import { CALENDAR_ENABLED } from './constants';
|
17
|
14
|
|
18
|
15
|
const logger = require('jitsi-meet-logger').getLogger(__filename);
|
19
|
16
|
|
20
|
17
|
/**
|
21
|
|
- * The no. of days to fetch.
|
|
18
|
+ * The number of days to fetch.
|
22
|
19
|
*/
|
23
|
20
|
const FETCH_END_DAYS = 10;
|
24
|
21
|
|
25
|
22
|
/**
|
26
|
|
- * The no. of days to go back when fetching.
|
|
23
|
+ * The number of days to go back when fetching.
|
27
|
24
|
*/
|
28
|
25
|
const FETCH_START_DAYS = -1;
|
29
|
26
|
|
|
@@ -37,15 +34,15 @@ CALENDAR_ENABLED
|
37
|
34
|
const result = next(action);
|
38
|
35
|
|
39
|
36
|
switch (action.type) {
|
40
|
|
- case APP_STATE_CHANGED:
|
41
|
|
- _maybeClearAccessStatus(store, action);
|
42
|
|
- break;
|
43
|
|
-
|
44
|
37
|
case ADD_KNOWN_DOMAINS:
|
45
|
38
|
case APP_WILL_MOUNT:
|
46
|
39
|
_fetchCalendarEntries(store, false, false);
|
47
|
40
|
break;
|
48
|
41
|
|
|
42
|
+ case APP_STATE_CHANGED:
|
|
43
|
+ _maybeClearAccessStatus(store, action);
|
|
44
|
+ break;
|
|
45
|
+
|
49
|
46
|
case REFRESH_CALENDAR:
|
50
|
47
|
_fetchCalendarEntries(store, true, action.forcePermission);
|
51
|
48
|
break;
|
|
@@ -54,23 +51,6 @@ CALENDAR_ENABLED
|
54
|
51
|
return result;
|
55
|
52
|
});
|
56
|
53
|
|
57
|
|
-/**
|
58
|
|
- * Clears the calendar access status when the app comes back from the
|
59
|
|
- * background. This is needed as some users may never quit the app, but puts it
|
60
|
|
- * into the background and we need to try to request for a permission as often
|
61
|
|
- * as possible, but not annoyingly often.
|
62
|
|
- *
|
63
|
|
- * @param {Object} store - The redux store.
|
64
|
|
- * @param {Object} action - The Redux action.
|
65
|
|
- * @private
|
66
|
|
- * @returns {void}
|
67
|
|
- */
|
68
|
|
-function _maybeClearAccessStatus(store, { appState }) {
|
69
|
|
- if (appState === 'background') {
|
70
|
|
- store.dispatch(setCalendarAuthorization(undefined));
|
71
|
|
- }
|
72
|
|
-}
|
73
|
|
-
|
74
|
54
|
/**
|
75
|
55
|
* Ensures calendar access if possible and resolves the promise if it's granted.
|
76
|
56
|
*
|
|
@@ -113,13 +93,13 @@ function _ensureCalendarAccess(promptForPermission, dispatch) {
|
113
|
93
|
* @returns {void}
|
114
|
94
|
*/
|
115
|
95
|
function _fetchCalendarEntries(
|
116
|
|
- { dispatch, getState },
|
|
96
|
+ store,
|
117
|
97
|
maybePromptForPermission,
|
118
|
98
|
forcePermission) {
|
119
|
|
- const featureState = getState()['features/calendar-sync'];
|
120
|
|
- const knownDomains = getState()['features/base/domains'];
|
|
99
|
+ const { dispatch, getState } = store;
|
121
|
100
|
const promptForPermission
|
122
|
|
- = (maybePromptForPermission && !featureState.authorization)
|
|
101
|
+ = (maybePromptForPermission
|
|
102
|
+ && !getState()['features/calendar-sync'].authorization)
|
123
|
103
|
|| forcePermission;
|
124
|
104
|
|
125
|
105
|
_ensureCalendarAccess(promptForPermission, dispatch)
|
|
@@ -135,11 +115,7 @@ function _fetchCalendarEntries(
|
135
|
115
|
startDate.getTime(),
|
136
|
116
|
endDate.getTime(),
|
137
|
117
|
[])
|
138
|
|
- .then(events =>
|
139
|
|
- _updateCalendarEntries(
|
140
|
|
- events,
|
141
|
|
- knownDomains,
|
142
|
|
- dispatch))
|
|
118
|
+ .then(_updateCalendarEntries.bind(store))
|
143
|
119
|
.catch(error =>
|
144
|
120
|
logger.error('Error fetching calendar.', error));
|
145
|
121
|
} else {
|
|
@@ -192,6 +168,22 @@ function _getURLFromEvent(event, knownDomains) {
|
192
|
168
|
return null;
|
193
|
169
|
}
|
194
|
170
|
|
|
171
|
+/**
|
|
172
|
+ * Clears the calendar access status when the app comes back from the
|
|
173
|
+ * background. This is needed as some users may never quit the app, but puts it
|
|
174
|
+ * into the background and we need to try to request for a permission as often
|
|
175
|
+ * as possible, but not annoyingly often.
|
|
176
|
+ *
|
|
177
|
+ * @param {Object} store - The redux store.
|
|
178
|
+ * @param {Object} action - The Redux action.
|
|
179
|
+ * @private
|
|
180
|
+ * @returns {void}
|
|
181
|
+ */
|
|
182
|
+function _maybeClearAccessStatus(store, { appState }) {
|
|
183
|
+ appState === 'background'
|
|
184
|
+ && store.dispatch(setCalendarAuthorization(undefined));
|
|
185
|
+}
|
|
186
|
+
|
195
|
187
|
/**
|
196
|
188
|
* Updates the calendar entries in Redux when new list is received.
|
197
|
189
|
*
|
|
@@ -231,26 +223,30 @@ function _parseCalendarEntry(event, knownDomains) {
|
231
|
223
|
}
|
232
|
224
|
|
233
|
225
|
/**
|
234
|
|
- * Updates the calendar entries in Redux when new list is received.
|
|
226
|
+ * Updates the calendar entries in redux when new list is received.
|
|
227
|
+ *
|
|
228
|
+ * XXX The function's {@code this} is the redux store.
|
235
|
229
|
*
|
236
|
230
|
* @param {Array<CalendarEntry>} events - The new event list.
|
237
|
|
- * @param {Array<string>} knownDomains - The known domain list.
|
238
|
|
- * @param {Function} dispatch - The Redux dispatch function.
|
239
|
231
|
* @private
|
240
|
232
|
* @returns {void}
|
241
|
233
|
*/
|
242
|
|
-function _updateCalendarEntries(events, knownDomains, dispatch) {
|
|
234
|
+function _updateCalendarEntries(events) {
|
243
|
235
|
if (events && events.length) {
|
|
236
|
+ // eslint-disable-next-line no-invalid-this
|
|
237
|
+ const { dispatch, getState } = this;
|
|
238
|
+
|
|
239
|
+ const knownDomains = getState()['features/base/known-domains'];
|
244
|
240
|
const eventList = [];
|
245
|
241
|
|
|
242
|
+ const now = Date.now();
|
|
243
|
+
|
246
|
244
|
for (const event of events) {
|
247
|
|
- const calendarEntry
|
248
|
|
- = _parseCalendarEntry(event, knownDomains);
|
249
|
|
- const now = Date.now();
|
|
245
|
+ const calendarEntry = _parseCalendarEntry(event, knownDomains);
|
250
|
246
|
|
251
|
|
- if (calendarEntry && calendarEntry.endDate > now) {
|
252
|
|
- eventList.push(calendarEntry);
|
253
|
|
- }
|
|
247
|
+ calendarEntry
|
|
248
|
+ && calendarEntry.endDate > now
|
|
249
|
+ && eventList.push(calendarEntry);
|
254
|
250
|
}
|
255
|
251
|
|
256
|
252
|
dispatch(
|