您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.any.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. if (url) {
  80. const startDate = Date.parse(event.startDate);
  81. const endDate = Date.parse(event.endDate);
  82. if (isNaN(startDate) || isNaN(endDate)) {
  83. logger.warn(
  84. 'Skipping invalid calendar event',
  85. event.title,
  86. event.startDate,
  87. event.endDate
  88. );
  89. } else {
  90. return {
  91. endDate,
  92. id: event.id,
  93. startDate,
  94. title: event.title,
  95. url
  96. };
  97. }
  98. }
  99. }
  100. return null;
  101. }
  102. /**
  103. * Retrieves a Jitsi Meet URL from an event if present.
  104. *
  105. * @param {Object} event - The event to parse.
  106. * @param {Array<string>} knownDomains - The known domain names.
  107. * @private
  108. * @returns {string}
  109. */
  110. function _getURLFromEvent(event, knownDomains) {
  111. const linkTerminatorPattern = '[^\\s<>$]';
  112. const urlRegExp
  113. = new RegExp(
  114. `http(s)?://(${knownDomains.join('|')})/${linkTerminatorPattern}+`,
  115. 'gi');
  116. const schemeRegExp
  117. = new RegExp(`${APP_LINK_SCHEME}${linkTerminatorPattern}+`, 'gi');
  118. const fieldsToSearch = [
  119. event.title,
  120. event.url,
  121. event.location,
  122. event.notes,
  123. event.description
  124. ];
  125. for (const field of fieldsToSearch) {
  126. if (typeof field === 'string') {
  127. const matches = urlRegExp.exec(field) || schemeRegExp.exec(field);
  128. if (matches) {
  129. const url = parseURIString(matches[0]);
  130. if (url) {
  131. return url.toString();
  132. }
  133. }
  134. }
  135. }
  136. return null;
  137. }