Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

actions.js 8.7KB

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