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

functions.any.ts 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import md5 from 'js-md5';
  2. import { APP_LINK_SCHEME, parseURIString } from '../base/util/uri';
  3. import { setCalendarEvents } from './actions';
  4. import { MAX_LIST_LENGTH } from './constants';
  5. const ALLDAY_EVENT_LENGTH = 23 * 60 * 60 * 1000;
  6. /**
  7. * Returns true of the calendar entry is to be displayed in the app, false
  8. * otherwise.
  9. *
  10. * @param {Object} entry - The calendar entry.
  11. * @returns {boolean}
  12. */
  13. function _isDisplayableCalendarEntry(entry: { allDay: boolean; attendees: Object[];
  14. endDate: number; startDate: number; }) {
  15. // Entries are displayable if:
  16. // - Ends in the future (future or ongoing events)
  17. // - Is not an all day event and there is only one attendee (these events
  18. // are usually placeholder events that don't need to be shown.)
  19. return entry.endDate > Date.now()
  20. && !((entry.allDay
  21. || entry.endDate - entry.startDate > ALLDAY_EVENT_LENGTH)
  22. && (!entry.attendees || entry.attendees.length < 2));
  23. }
  24. /**
  25. * Updates the calendar entries in redux when new list is received. The feature
  26. * calendar-sync doesn't display all calendar events, it displays unique
  27. * title, URL, and start time tuples, and it doesn't display subsequent
  28. * occurrences of recurring events, and the repetitions of events coming from
  29. * multiple calendars.
  30. *
  31. * XXX The function's {@code this} is the redux store.
  32. *
  33. * @param {Array<CalendarEntry>} events - The new event list.
  34. * @private
  35. * @returns {void}
  36. */
  37. export function _updateCalendarEntries(events: Array<Object>) {
  38. if (!events || !events.length) {
  39. return;
  40. }
  41. // @ts-ignore
  42. // eslint-disable-next-line @typescript-eslint/no-invalid-this
  43. const { dispatch, getState } = this;
  44. const knownDomains = getState()['features/base/known-domains'];
  45. const entryMap = new Map();
  46. for (const event of events) {
  47. const entry = _parseCalendarEntry(event, knownDomains);
  48. if (entry && _isDisplayableCalendarEntry(entry)) {
  49. // As was stated above, we don't display subsequent occurrences of
  50. // recurring events, and the repetitions of events coming from
  51. // multiple calendars.
  52. const key = md5.hex(JSON.stringify([
  53. // Obviously, we want to display different conference/meetings
  54. // URLs. URLs are the very reason why we implemented the feature
  55. // calendar-sync in the first place.
  56. entry.url,
  57. // We probably want to display one and the same URL to people if
  58. // they have it under different titles in their Calendar.
  59. // Because maybe they remember the title of the meeting, not the
  60. // URL so they expect to see the title without realizing that
  61. // they have the same URL already under a different title.
  62. entry.title,
  63. // XXX Eventually, given that the URL and the title are the
  64. // same, what sets one event apart from another is the start
  65. // time of the day (note the use of toTimeString() below)! The
  66. // day itself is not important because we don't want multiple
  67. // occurrences of a recurring event or repetitions of an even
  68. // from multiple calendars.
  69. new Date(entry.startDate).toTimeString()
  70. ]));
  71. const existingEntry = entryMap.get(key);
  72. // We want only the earliest occurrence (which hasn't ended in the
  73. // past, that is) of a recurring event.
  74. if (!existingEntry || existingEntry.startDate > entry.startDate) {
  75. entryMap.set(key, entry);
  76. }
  77. }
  78. }
  79. dispatch(
  80. setCalendarEvents(
  81. Array.from(entryMap.values())
  82. .sort((a, b) => a.startDate - b.startDate)
  83. .slice(0, MAX_LIST_LENGTH)));
  84. }
  85. /**
  86. * Checks a string against a positive pattern and a negative pattern. Returns
  87. * the string if it matches the positive pattern and doesn't provide any match
  88. * against the negative pattern. Null otherwise.
  89. *
  90. * @param {string} str - The string to check.
  91. * @param {string} positivePattern - The positive pattern.
  92. * @param {string} negativePattern - The negative pattern.
  93. * @returns {string}
  94. */
  95. function _checkPattern(str: string, positivePattern: string, negativePattern: string) {
  96. const positiveRegExp = new RegExp(positivePattern, 'gi');
  97. let positiveMatch = positiveRegExp.exec(str);
  98. while (positiveMatch !== null) {
  99. const url = positiveMatch[0];
  100. if (!new RegExp(negativePattern, 'gi').exec(url)) {
  101. return url;
  102. }
  103. positiveMatch = positiveRegExp.exec(str);
  104. }
  105. }
  106. /**
  107. * Updates the calendar entries in Redux when new list is received.
  108. *
  109. * @param {Object} event - An event returned from the native calendar.
  110. * @param {Array<string>} knownDomains - The known domain list.
  111. * @private
  112. * @returns {CalendarEntry}
  113. */
  114. function _parseCalendarEntry(event: any, knownDomains: string[]) {
  115. if (event) {
  116. const url = _getURLFromEvent(event, knownDomains);
  117. const startDate = Date.parse(event.startDate);
  118. const endDate = Date.parse(event.endDate);
  119. // we want to hide all events that
  120. // - has no start or end date
  121. // - for web, if there is no url and we cannot edit the event (has
  122. // no calendarId)
  123. if (isNaN(startDate)
  124. || isNaN(endDate)
  125. || (navigator.product !== 'ReactNative'
  126. && !url
  127. && !event.calendarId)) {
  128. // Ignore the event.
  129. } else {
  130. return {
  131. allDay: event.allDay,
  132. attendees: event.attendees,
  133. calendarId: event.calendarId,
  134. endDate,
  135. id: event.id,
  136. startDate,
  137. title: event.title,
  138. url
  139. };
  140. }
  141. }
  142. return null;
  143. }
  144. /**
  145. * Retrieves a Jitsi Meet URL from an event if present.
  146. *
  147. * @param {Object} event - The event to parse.
  148. * @param {Array<string>} knownDomains - The known domain names.
  149. * @private
  150. * @returns {string}
  151. */
  152. function _getURLFromEvent(event: { description: string; location: string; notes: string; title: string;
  153. url: string; }, knownDomains: string[]) {
  154. const linkTerminatorPattern = '[^\\s<>$]';
  155. const urlRegExp
  156. = `http(s)?://(${knownDomains.join('|')})/${linkTerminatorPattern}+`;
  157. const schemeRegExp = `${APP_LINK_SCHEME}${linkTerminatorPattern}+`;
  158. const excludePattern = '/static/';
  159. const fieldsToSearch = [
  160. event.title,
  161. event.url,
  162. event.location,
  163. event.notes,
  164. event.description
  165. ];
  166. for (const field of fieldsToSearch) {
  167. if (typeof field === 'string') {
  168. const match
  169. = _checkPattern(field, urlRegExp, excludePattern)
  170. || _checkPattern(field, schemeRegExp, excludePattern);
  171. if (match) {
  172. const url = parseURIString(match);
  173. if (url) {
  174. return url.toString();
  175. }
  176. }
  177. }
  178. }
  179. return null;
  180. }