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.

functions.any.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // @flow
  2. import md5 from 'js-md5';
  3. import { setCalendarEvents } from './actions';
  4. import { APP_LINK_SCHEME, parseURIString } from '../base/util';
  5. import { MAX_LIST_LENGTH } from './constants';
  6. const logger = require('jitsi-meet-logger').getLogger(__filename);
  7. /**
  8. * Updates the calendar entries in redux when new list is received. The feature
  9. * calendar-sync doesn't display all calendar events, it displays unique
  10. * title, URL, and start time tuples i.e. it doesn't display subsequent
  11. * occurrences of recurring events, and the repetitions of events coming from
  12. * multiple calendars.
  13. *
  14. * XXX The function's {@code this} is the redux store.
  15. *
  16. * @param {Array<CalendarEntry>} events - The new event list.
  17. * @private
  18. * @returns {void}
  19. */
  20. export function _updateCalendarEntries(events: Array<Object>) {
  21. if (!events || !events.length) {
  22. return;
  23. }
  24. // eslint-disable-next-line no-invalid-this
  25. const { dispatch, getState } = this;
  26. const knownDomains = getState()['features/base/known-domains'];
  27. const now = Date.now();
  28. const entryMap = new Map();
  29. for (const event of events) {
  30. const entry = _parseCalendarEntry(event, knownDomains);
  31. if (entry && entry.endDate > now) {
  32. // As was stated above, we don't display subsequent occurrences of
  33. // recurring events, and the repetitions of events coming from
  34. // multiple calendars.
  35. const key = md5.hex(JSON.stringify([
  36. // Obviously, we want to display different conference/meetings
  37. // URLs. URLs are the very reason why we implemented the feature
  38. // calendar-sync in the first place.
  39. entry.url,
  40. // We probably want to display one and the same URL to people if
  41. // they have it under different titles in their Calendar.
  42. // Because maybe they remember the title of the meeting, not the
  43. // URL so they expect to see the title without realizing that
  44. // they have the same URL already under a different title.
  45. entry.title,
  46. // XXX Eventually, given that the URL and the title are the
  47. // same, what sets one event apart from another is the start
  48. // time of the day (note the use of toTimeString() bellow)! The
  49. // day itself is not important because we don't want multiple
  50. // occurrences of a recurring event or repetitions of an even
  51. // from multiple calendars.
  52. new Date(entry.startDate).toTimeString()
  53. ]));
  54. const existingEntry = entryMap.get(key);
  55. // We want only the earliest occurrence (which hasn't ended in the
  56. // past, that is) of a recurring event.
  57. if (!existingEntry || existingEntry.startDate > entry.startDate) {
  58. entryMap.set(key, entry);
  59. }
  60. }
  61. }
  62. dispatch(
  63. setCalendarEvents(
  64. Array.from(entryMap.values())
  65. .sort((a, b) => a.startDate - b.startDate)
  66. .slice(0, MAX_LIST_LENGTH)));
  67. }
  68. /**
  69. * Updates the calendar entries in Redux when new list is received.
  70. *
  71. * @param {Object} event - An event returned from the native calendar.
  72. * @param {Array<string>} knownDomains - The known domain list.
  73. * @private
  74. * @returns {CalendarEntry}
  75. */
  76. function _parseCalendarEntry(event, knownDomains) {
  77. if (event) {
  78. const url = _getURLFromEvent(event, knownDomains);
  79. const startDate = Date.parse(event.startDate);
  80. const endDate = Date.parse(event.endDate);
  81. // we want to hide all events that
  82. // - has no start or end date
  83. // - for web, if there is no url and we cannot edit the event (has
  84. // no calendarId)
  85. if (isNaN(startDate)
  86. || isNaN(endDate)
  87. || (navigator.product !== 'ReactNative'
  88. && !url
  89. && !event.calendarId)) {
  90. logger.debug(
  91. 'Skipping invalid calendar event',
  92. event.title,
  93. event.startDate,
  94. event.endDate,
  95. url,
  96. event.calendarId
  97. );
  98. } else {
  99. return {
  100. calendarId: event.calendarId,
  101. endDate,
  102. id: event.id,
  103. startDate,
  104. title: event.title,
  105. url
  106. };
  107. }
  108. }
  109. return null;
  110. }
  111. /**
  112. * Retrieves a Jitsi Meet URL from an event if present.
  113. *
  114. * @param {Object} event - The event to parse.
  115. * @param {Array<string>} knownDomains - The known domain names.
  116. * @private
  117. * @returns {string}
  118. */
  119. function _getURLFromEvent(event, knownDomains) {
  120. const linkTerminatorPattern = '[^\\s<>$]';
  121. const urlRegExp
  122. = new RegExp(
  123. `http(s)?://(${knownDomains.join('|')})/${linkTerminatorPattern}+`,
  124. 'gi');
  125. const schemeRegExp
  126. = new RegExp(`${APP_LINK_SCHEME}${linkTerminatorPattern}+`, 'gi');
  127. const fieldsToSearch = [
  128. event.title,
  129. event.url,
  130. event.location,
  131. event.notes,
  132. event.description
  133. ];
  134. for (const field of fieldsToSearch) {
  135. if (typeof field === 'string') {
  136. const matches = urlRegExp.exec(field) || schemeRegExp.exec(field);
  137. if (matches) {
  138. const url = parseURIString(matches[0]);
  139. if (url) {
  140. return url.toString();
  141. }
  142. }
  143. }
  144. }
  145. return null;
  146. }