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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // @flow
  2. import Logger from 'jitsi-meet-logger';
  3. import RNCalendarEvents from 'react-native-calendar-events';
  4. import { MiddlewareRegistry } from '../base/redux';
  5. import { APP_WILL_MOUNT } from '../app';
  6. import { updateCalendarEntryList } from './actions';
  7. const FETCH_END_DAYS = 10;
  8. const FETCH_START_DAYS = -1;
  9. const MAX_LIST_LENGTH = 10;
  10. const logger = Logger.getLogger(__filename);
  11. // this is to be dynamic later.
  12. const domainList = [
  13. 'meet.jit.si',
  14. 'beta.meet.jit.si'
  15. ];
  16. MiddlewareRegistry.register(store => next => action => {
  17. const result = next(action);
  18. switch (action.type) {
  19. case APP_WILL_MOUNT:
  20. _fetchCalendarEntries(store);
  21. }
  22. return result;
  23. });
  24. /**
  25. * Ensures calendar access if possible and resolves the promise if it's granted.
  26. *
  27. * @private
  28. * @returns {Promise}
  29. */
  30. function _ensureCalendarAccess() {
  31. return new Promise((resolve, reject) => {
  32. RNCalendarEvents.authorizationStatus()
  33. .then(status => {
  34. if (status === 'authorized') {
  35. resolve();
  36. } else if (status === 'undetermined') {
  37. RNCalendarEvents.authorizeEventStore()
  38. .then(result => {
  39. if (result === 'authorized') {
  40. resolve();
  41. } else {
  42. reject(result);
  43. }
  44. })
  45. .catch(error => {
  46. reject(error);
  47. });
  48. } else {
  49. reject(status);
  50. }
  51. })
  52. .catch(error => {
  53. reject(error);
  54. });
  55. });
  56. }
  57. /**
  58. * Reads the user's calendar and updates the stored entries if need be.
  59. *
  60. * @private
  61. * @param {Object} store - The redux store.
  62. * @returns {void}
  63. */
  64. function _fetchCalendarEntries(store) {
  65. _ensureCalendarAccess()
  66. .then(() => {
  67. const startDate = new Date();
  68. const endDate = new Date();
  69. startDate.setDate(startDate.getDate() + FETCH_START_DAYS);
  70. endDate.setDate(endDate.getDate() + FETCH_END_DAYS);
  71. RNCalendarEvents.fetchAllEvents(
  72. startDate.getTime(),
  73. endDate.getTime(),
  74. []
  75. )
  76. .then(events => {
  77. const eventList = [];
  78. if (events && events.length) {
  79. for (const event of events) {
  80. const jitsiURL = _getURLFromEvent(event);
  81. const now = Date.now();
  82. if (jitsiURL) {
  83. const eventStartDate = Date.parse(event.startDate);
  84. const eventEndDate = Date.parse(event.endDate);
  85. if (isNaN(eventStartDate) || isNaN(eventEndDate)) {
  86. logger.warn(
  87. 'Skipping calendar event due to invalid dates',
  88. event.title,
  89. event.startDate,
  90. event.endDate
  91. );
  92. } else if (eventEndDate > now) {
  93. eventList.push({
  94. endDate: eventEndDate,
  95. id: event.id,
  96. startDate: eventStartDate,
  97. title: event.title,
  98. url: jitsiURL
  99. });
  100. }
  101. }
  102. }
  103. }
  104. // TEST events to check notification popup.
  105. // TODO: Remove this before a PR.
  106. eventList.push({
  107. endDate: Date.now() + (60 * 60 * 1000),
  108. id: -1,
  109. startDate: Date.now() + (80 * 1000),
  110. title: 'ShipIt 41',
  111. url: 'https://meet.jit.si/shipit41'
  112. });
  113. eventList.push({
  114. endDate: Date.now() + (2 * 60 * 60 * 1000),
  115. id: -2,
  116. startDate: Date.now() + (60 * 60 * 1000),
  117. title: 'ShipIt 41 demo',
  118. url: 'https://meet.jit.si/shipit41'
  119. });
  120. store.dispatch(updateCalendarEntryList(eventList.sort((a, b) =>
  121. a.startDate - b.startDate
  122. ).slice(0, MAX_LIST_LENGTH)));
  123. })
  124. .catch(error => {
  125. logger.error('Error fetching calendar.', error);
  126. });
  127. })
  128. .catch(reason => {
  129. logger.error('Error accessing calendar.', reason);
  130. });
  131. }
  132. /**
  133. * Retreives a jitsi URL from an event if present.
  134. *
  135. * @private
  136. * @param {Object} event - The event to parse.
  137. * @returns {string}
  138. *
  139. */
  140. function _getURLFromEvent(event) {
  141. const urlRegExp
  142. = new RegExp(`http(s)?://(${domainList.join('|')})/[^\\s<>$]+`, 'gi');
  143. const fieldsToSearch = [
  144. event.title,
  145. event.url,
  146. event.location,
  147. event.notes,
  148. event.description
  149. ];
  150. let matchArray;
  151. for (const field of fieldsToSearch) {
  152. if (typeof field === 'string') {
  153. if (
  154. (matchArray = urlRegExp.exec(field)) !== null
  155. ) {
  156. return matchArray[0];
  157. }
  158. }
  159. }
  160. return null;
  161. }