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

actions.js 8.9KB

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