Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

actions.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // @flow
  2. import { loadGoogleAPI } from '../google-api';
  3. import {
  4. CLEAR_CALENDAR_INTEGRATION,
  5. REFRESH_CALENDAR,
  6. SET_CALENDAR_AUTH_STATE,
  7. SET_CALENDAR_AUTHORIZATION,
  8. SET_CALENDAR_EVENTS,
  9. SET_CALENDAR_INTEGRATION,
  10. SET_CALENDAR_PROFILE_EMAIL
  11. } from './actionTypes';
  12. import { _getCalendarIntegration, isCalendarEnabled } from './functions';
  13. const logger = require('jitsi-meet-logger').getLogger(__filename);
  14. /**
  15. * Sets the initial state of calendar integration by loading third party APIs
  16. * and filling out any data that needs to be fetched.
  17. *
  18. * @returns {Function}
  19. */
  20. export function bootstrapCalendarIntegration(): Function {
  21. return (dispatch, getState) => {
  22. const {
  23. googleApiApplicationClientID
  24. } = getState()['features/base/config'];
  25. const {
  26. integrationReady,
  27. integrationType
  28. } = getState()['features/calendar-sync'];
  29. if (!isCalendarEnabled()) {
  30. return Promise.reject();
  31. }
  32. return Promise.resolve()
  33. .then(() => {
  34. if (googleApiApplicationClientID) {
  35. return dispatch(
  36. loadGoogleAPI(googleApiApplicationClientID));
  37. }
  38. })
  39. .then(() => {
  40. if (!integrationType || integrationReady) {
  41. return;
  42. }
  43. const integrationToLoad
  44. = _getCalendarIntegration(integrationType);
  45. if (!integrationToLoad) {
  46. dispatch(clearCalendarIntegration());
  47. return;
  48. }
  49. return dispatch(integrationToLoad._isSignedIn())
  50. .then(signedIn => {
  51. if (signedIn) {
  52. dispatch(setIntegrationReady(integrationType));
  53. dispatch(updateProfile(integrationType));
  54. } else {
  55. dispatch(clearCalendarIntegration());
  56. }
  57. });
  58. });
  59. };
  60. }
  61. /**
  62. * Resets the state of calendar integration so stored events and selected
  63. * calendar type are cleared.
  64. *
  65. * @returns {{
  66. * type: CLEAR_CALENDAR_INTEGRATION
  67. * }}
  68. */
  69. export function clearCalendarIntegration() {
  70. return {
  71. type: CLEAR_CALENDAR_INTEGRATION
  72. };
  73. }
  74. /**
  75. * Sends an action to refresh the entry list (fetches new data).
  76. *
  77. * @param {boolean} forcePermission - Whether to force to re-ask for
  78. * the permission or not.
  79. * @param {boolean} isInteractive - If true this refresh was caused by
  80. * direct user interaction, false otherwise.
  81. * @returns {{
  82. * type: REFRESH_CALENDAR,
  83. * forcePermission: boolean,
  84. * isInteractive: boolean
  85. * }}
  86. */
  87. export function refreshCalendar(
  88. forcePermission: boolean = false, isInteractive: boolean = true) {
  89. return {
  90. type: REFRESH_CALENDAR,
  91. forcePermission,
  92. isInteractive
  93. };
  94. }
  95. /**
  96. * Sends an action to update the current calendar api auth state in redux.
  97. * This is used only for microsoft implementation to store it auth state.
  98. *
  99. * @param {number} newState - The new state.
  100. * @returns {{
  101. * type: SET_CALENDAR_AUTH_STATE,
  102. * msAuthState: Object
  103. * }}
  104. */
  105. export function setCalendarAPIAuthState(newState: ?Object) {
  106. return {
  107. type: SET_CALENDAR_AUTH_STATE,
  108. msAuthState: newState
  109. };
  110. }
  111. /**
  112. * Sends an action to signal that a calendar access has been requested. For more
  113. * info, see {@link SET_CALENDAR_AUTHORIZATION}.
  114. *
  115. * @param {string | undefined} authorization - The result of the last calendar
  116. * authorization request.
  117. * @returns {{
  118. * type: SET_CALENDAR_AUTHORIZATION,
  119. * authorization: ?string
  120. * }}
  121. */
  122. export function setCalendarAuthorization(authorization: ?string) {
  123. return {
  124. type: SET_CALENDAR_AUTHORIZATION,
  125. authorization
  126. };
  127. }
  128. /**
  129. * Sends an action to update the current calendar list in redux.
  130. *
  131. * @param {Array<Object>} events - The new list.
  132. * @returns {{
  133. * type: SET_CALENDAR_EVENTS,
  134. * events: Array<Object>
  135. * }}
  136. */
  137. export function setCalendarEvents(events: Array<Object>) {
  138. return {
  139. type: SET_CALENDAR_EVENTS,
  140. events
  141. };
  142. }
  143. /**
  144. * Sends an action to update the current calendar profile email state in redux.
  145. *
  146. * @param {number} newEmail - The new email.
  147. * @returns {{
  148. * type: SET_CALENDAR_PROFILE_EMAIL,
  149. * email: string
  150. * }}
  151. */
  152. export function setCalendarProfileEmail(newEmail: ?string) {
  153. return {
  154. type: SET_CALENDAR_PROFILE_EMAIL,
  155. email: newEmail
  156. };
  157. }
  158. /**
  159. * Sets the calendar integration type to be used by web and signals that the
  160. * integration is ready to be used.
  161. *
  162. * @param {string|undefined} integrationType - The calendar type.
  163. * @returns {{
  164. * type: SET_CALENDAR_INTEGRATION,
  165. * integrationReady: boolean,
  166. * integrationType: string
  167. * }}
  168. */
  169. export function setIntegrationReady(integrationType: string) {
  170. return {
  171. type: SET_CALENDAR_INTEGRATION,
  172. integrationReady: true,
  173. integrationType
  174. };
  175. }
  176. /**
  177. * Signals signing in to the specified calendar integration.
  178. *
  179. * @param {string} calendarType - The calendar integration which should be
  180. * signed into.
  181. * @returns {Function}
  182. */
  183. export function signIn(calendarType: string): Function {
  184. return (dispatch: Dispatch<*>) => {
  185. const integration = _getCalendarIntegration(calendarType);
  186. if (!integration) {
  187. return Promise.reject('No supported integration found');
  188. }
  189. return dispatch(integration.load())
  190. .then(() => dispatch(integration.signIn()))
  191. .then(() => dispatch(setIntegrationReady(calendarType)))
  192. .then(() => dispatch(updateProfile(calendarType)))
  193. .catch(error => {
  194. logger.error(
  195. 'Error occurred while signing into calendar integration',
  196. error);
  197. return Promise.reject(error);
  198. });
  199. };
  200. }
  201. /**
  202. * Signals to get current profile data linked to the current calendar
  203. * integration that is in use.
  204. *
  205. * @param {string} calendarType - The calendar integration to which the profile
  206. * should be updated.
  207. * @returns {Function}
  208. */
  209. export function updateProfile(calendarType: string): Function {
  210. return (dispatch: Dispatch<*>) => {
  211. const integration = _getCalendarIntegration(calendarType);
  212. if (!integration) {
  213. return Promise.reject('No integration found');
  214. }
  215. return dispatch(integration.getCurrentEmail())
  216. .then(email => {
  217. dispatch(setCalendarProfileEmail(email));
  218. });
  219. };
  220. }