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.

actions.js 8.2KB

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