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

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