You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // @flow
  2. import Logger from 'jitsi-meet-logger';
  3. import RNCalendarEvents from 'react-native-calendar-events';
  4. import { SET_ROOM } from '../base/conference';
  5. import { MiddlewareRegistry } from '../base/redux';
  6. import { APP_LINK_SCHEME, parseURIString } from '../base/util';
  7. import { APP_WILL_MOUNT } from '../app';
  8. import { maybeAddNewKnownDomain, updateCalendarEntryList } from './actions';
  9. import { REFRESH_CALENDAR_ENTRY_LIST } from './actionTypes';
  10. const FETCH_END_DAYS = 10;
  11. const FETCH_START_DAYS = -1;
  12. const MAX_LIST_LENGTH = 10;
  13. const logger = Logger.getLogger(__filename);
  14. MiddlewareRegistry.register(store => next => action => {
  15. const result = next(action);
  16. switch (action.type) {
  17. case APP_WILL_MOUNT:
  18. _ensureDefaultServer(store);
  19. _fetchCalendarEntries(store);
  20. break;
  21. case REFRESH_CALENDAR_ENTRY_LIST:
  22. _fetchCalendarEntries(store);
  23. break;
  24. case SET_ROOM:
  25. _parseAndAddDomain(store);
  26. }
  27. return result;
  28. });
  29. /**
  30. * Ensures calendar access if possible and resolves the promise if it's granted.
  31. *
  32. * @private
  33. * @returns {Promise}
  34. */
  35. function _ensureCalendarAccess() {
  36. return new Promise((resolve, reject) => {
  37. RNCalendarEvents.authorizationStatus()
  38. .then(status => {
  39. if (status === 'authorized') {
  40. resolve();
  41. } else if (status === 'undetermined') {
  42. RNCalendarEvents.authorizeEventStore()
  43. .then(result => {
  44. if (result === 'authorized') {
  45. resolve();
  46. } else {
  47. reject(result);
  48. }
  49. })
  50. .catch(error => {
  51. reject(error);
  52. });
  53. } else {
  54. reject(status);
  55. }
  56. })
  57. .catch(error => {
  58. reject(error);
  59. });
  60. });
  61. }
  62. /**
  63. * Ensures presence of the default server in the known domains list.
  64. *
  65. * @private
  66. * @param {Object} store - The redux store.
  67. * @returns {Promise}
  68. */
  69. function _ensureDefaultServer(store) {
  70. const state = store.getState();
  71. const defaultURL = parseURIString(
  72. state['features/app'].app._getDefaultURL()
  73. );
  74. store.dispatch(maybeAddNewKnownDomain(defaultURL.host));
  75. }
  76. /**
  77. * Reads the user's calendar and updates the stored entries if need be.
  78. *
  79. * @private
  80. * @param {Object} store - The redux store.
  81. * @returns {void}
  82. */
  83. function _fetchCalendarEntries(store) {
  84. _ensureCalendarAccess()
  85. .then(() => {
  86. const startDate = new Date();
  87. const endDate = new Date();
  88. startDate.setDate(startDate.getDate() + FETCH_START_DAYS);
  89. endDate.setDate(endDate.getDate() + FETCH_END_DAYS);
  90. RNCalendarEvents.fetchAllEvents(
  91. startDate.getTime(),
  92. endDate.getTime(),
  93. []
  94. )
  95. .then(events => {
  96. const { knownDomains } = store.getState()['features/calendar-sync'];
  97. const eventList = [];
  98. if (events && events.length) {
  99. for (const event of events) {
  100. const jitsiURL = _getURLFromEvent(event, knownDomains);
  101. const now = Date.now();
  102. if (jitsiURL) {
  103. const eventStartDate = Date.parse(event.startDate);
  104. const eventEndDate = Date.parse(event.endDate);
  105. if (isNaN(eventStartDate) || isNaN(eventEndDate)) {
  106. logger.warn(
  107. 'Skipping calendar event due to invalid dates',
  108. event.title,
  109. event.startDate,
  110. event.endDate
  111. );
  112. } else if (eventEndDate > now) {
  113. eventList.push({
  114. endDate: eventEndDate,
  115. id: event.id,
  116. startDate: eventStartDate,
  117. title: event.title,
  118. url: jitsiURL
  119. });
  120. }
  121. }
  122. }
  123. }
  124. store.dispatch(updateCalendarEntryList(eventList.sort((a, b) =>
  125. a.startDate - b.startDate
  126. ).slice(0, MAX_LIST_LENGTH)));
  127. })
  128. .catch(error => {
  129. logger.error('Error fetching calendar.', error);
  130. });
  131. })
  132. .catch(reason => {
  133. logger.error('Error accessing calendar.', reason);
  134. });
  135. }
  136. /**
  137. * Retreives a jitsi URL from an event if present.
  138. *
  139. * @private
  140. * @param {Object} event - The event to parse.
  141. * @param {Array<string>} knownDomains - The known domain names.
  142. * @returns {string}
  143. *
  144. */
  145. function _getURLFromEvent(event, knownDomains) {
  146. const linkTerminatorPattern = '[^\\s<>$]';
  147. /* eslint-disable max-len */
  148. const urlRegExp
  149. = new RegExp(`http(s)?://(${knownDomains.join('|')})/${linkTerminatorPattern}+`, 'gi');
  150. /* eslint-enable max-len */
  151. const schemeRegExp
  152. = new RegExp(`${APP_LINK_SCHEME}${linkTerminatorPattern}+`, 'gi');
  153. const fieldsToSearch = [
  154. event.title,
  155. event.url,
  156. event.location,
  157. event.notes,
  158. event.description
  159. ];
  160. let matchArray;
  161. for (const field of fieldsToSearch) {
  162. if (typeof field === 'string') {
  163. if (
  164. (matchArray
  165. = urlRegExp.exec(field) || schemeRegExp.exec(field))
  166. !== null
  167. ) {
  168. return matchArray[0];
  169. }
  170. }
  171. }
  172. return null;
  173. }
  174. /**
  175. * Retreives the domain name of a room upon join and stores it
  176. * in the known domain list, if not present yet.
  177. *
  178. * @private
  179. * @param {Object} store - The redux store.
  180. * @returns {Promise}
  181. */
  182. function _parseAndAddDomain(store) {
  183. const { locationURL } = store.getState()['features/base/connection'];
  184. store.dispatch(maybeAddNewKnownDomain(locationURL.host));
  185. }