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.6KB

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