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.native.ts 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { NativeModules, Platform } from 'react-native';
  2. import RNCalendarEvents from 'react-native-calendar-events';
  3. import { IReduxState, IStore } from '../app/types';
  4. import { IStateful } from '../base/app/types';
  5. import { CALENDAR_ENABLED } from '../base/flags/constants';
  6. import { getFeatureFlag } from '../base/flags/functions';
  7. // eslint-disable-next-line lines-around-comment
  8. // @ts-ignore
  9. import { getShareInfoText } from '../invite';
  10. import { setCalendarAuthorization } from './actions.native';
  11. import { FETCH_END_DAYS, FETCH_START_DAYS } from './constants';
  12. import { _updateCalendarEntries } from './functions.native';
  13. import logger from './logger';
  14. export * from './functions.any';
  15. /**
  16. * Adds a Jitsi link to a calendar entry.
  17. *
  18. * @param {Object} state - The Redux state.
  19. * @param {string} id - The ID of the calendar entry.
  20. * @param {string} link - The link to add info with.
  21. * @returns {Promise<*>}
  22. */
  23. export function addLinkToCalendarEntry(
  24. state: IReduxState, id: string, link: string): Promise<any> {
  25. return new Promise((resolve, reject) => {
  26. getShareInfoText(state, link, true).then((shareInfoText: string) => {
  27. RNCalendarEvents.findEventById(id).then((event: any) => {
  28. const updateText
  29. = event.description
  30. ? `${event.description}\n\n${shareInfoText}`
  31. : shareInfoText;
  32. const updateObject = {
  33. id: event.id,
  34. ...Platform.select({
  35. ios: {
  36. notes: updateText
  37. },
  38. android: {
  39. description: updateText
  40. }
  41. })
  42. };
  43. // @ts-ignore
  44. RNCalendarEvents.saveEvent(event.title, updateObject)
  45. .then(resolve, reject);
  46. }, reject);
  47. }, reject);
  48. });
  49. }
  50. /**
  51. * Determines whether the calendar feature is enabled by the app. For
  52. * example, Apple through its App Store requires
  53. * {@code NSCalendarsUsageDescription} in the app's Info.plist or App Store
  54. * rejects the app. It could also be disabled with a feature flag.
  55. *
  56. * @param {Function|Object} stateful - The redux store or {@code getState}
  57. * function.
  58. * @returns {boolean} If the app has enabled the calendar feature, {@code true};
  59. * otherwise, {@code false}.
  60. */
  61. export function isCalendarEnabled(stateful: IStateful) {
  62. const flag = getFeatureFlag(stateful, CALENDAR_ENABLED);
  63. if (typeof flag !== 'undefined') {
  64. return flag;
  65. }
  66. const { calendarEnabled = true } = NativeModules.AppInfo;
  67. return calendarEnabled;
  68. }
  69. /**
  70. * Reads the user's calendar and updates the stored entries if need be.
  71. *
  72. * @param {Object} store - The redux store.
  73. * @param {boolean} maybePromptForPermission - Flag to tell the app if it should
  74. * prompt for a calendar permission if it wasn't granted yet.
  75. * @param {boolean|undefined} forcePermission - Whether to force to re-ask for
  76. * the permission or not.
  77. * @private
  78. * @returns {void}
  79. */
  80. export function _fetchCalendarEntries(
  81. store: IStore,
  82. maybePromptForPermission: boolean,
  83. forcePermission?: boolean) {
  84. const { dispatch, getState } = store;
  85. const promptForPermission
  86. = (maybePromptForPermission
  87. && !getState()['features/calendar-sync'].authorization)
  88. || forcePermission;
  89. _ensureCalendarAccess(promptForPermission, dispatch)
  90. .then(accessGranted => {
  91. if (accessGranted) {
  92. const startDate = new Date();
  93. const endDate = new Date();
  94. startDate.setDate(startDate.getDate() + FETCH_START_DAYS);
  95. endDate.setDate(endDate.getDate() + FETCH_END_DAYS);
  96. RNCalendarEvents.fetchAllEvents(
  97. // @ts-ignore
  98. startDate.getTime(),
  99. endDate.getTime(),
  100. [])
  101. .then(_updateCalendarEntries.bind(store))
  102. .catch(error =>
  103. logger.error('Error fetching calendar.', error));
  104. } else {
  105. logger.warn('Calendar access not granted.');
  106. }
  107. })
  108. .catch(reason => logger.error('Error accessing calendar.', reason));
  109. }
  110. /**
  111. * Ensures calendar access if possible and resolves the promise if it's granted.
  112. *
  113. * @param {boolean} promptForPermission - Flag to tell the app if it should
  114. * prompt for a calendar permission if it wasn't granted yet.
  115. * @param {Function} dispatch - The Redux dispatch function.
  116. * @private
  117. * @returns {Promise}
  118. */
  119. function _ensureCalendarAccess(promptForPermission: boolean | undefined, dispatch: IStore['dispatch']) {
  120. return new Promise((resolve, reject) => {
  121. RNCalendarEvents.checkPermissions()
  122. .then(status => {
  123. if (status === 'authorized') {
  124. resolve(true);
  125. } else if (promptForPermission) {
  126. RNCalendarEvents.requestPermissions()
  127. .then(result => {
  128. dispatch(setCalendarAuthorization(result));
  129. resolve(result === 'authorized');
  130. })
  131. .catch(reject);
  132. } else {
  133. resolve(false);
  134. }
  135. })
  136. .catch(reject);
  137. });
  138. }