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.js 4.7KB

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