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.

googleApi.web.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. import {
  2. API_URL_BROADCAST_STREAMS,
  3. API_URL_LIVE_BROADCASTS,
  4. DISCOVERY_DOCS,
  5. GOOGLE_SCOPE_CALENDAR,
  6. GOOGLE_SCOPE_YOUTUBE
  7. } from './constants';
  8. const GOOGLE_API_CLIENT_LIBRARY_URL = 'https://apis.google.com/js/api.js';
  9. /**
  10. * A promise for dynamically loading the Google API Client Library.
  11. *
  12. * @private
  13. * @type {Promise}
  14. */
  15. let googleClientLoadPromise;
  16. /**
  17. * A singleton for loading and interacting with the Google API.
  18. */
  19. const googleApi = {
  20. /**
  21. * Obtains Google API Client Library, loading the library dynamically if
  22. * needed.
  23. *
  24. * @returns {Promise}
  25. */
  26. get() {
  27. const globalGoogleApi = this._getGoogleApiClient();
  28. if (!globalGoogleApi) {
  29. return this.load();
  30. }
  31. return Promise.resolve(globalGoogleApi);
  32. },
  33. /**
  34. * Gets the profile for the user signed in to the Google API Client Library.
  35. *
  36. * @returns {Promise}
  37. */
  38. getCurrentUserProfile() {
  39. return this.get()
  40. .then(() => this.isSignedIn())
  41. .then(isSignedIn => {
  42. if (!isSignedIn) {
  43. return null;
  44. }
  45. return this._getGoogleApiClient()
  46. .auth2.getAuthInstance()
  47. .currentUser.get()
  48. .getBasicProfile();
  49. });
  50. },
  51. /**
  52. * Sets the Google Web Client ID used for authenticating with Google and
  53. * making Google API requests.
  54. *
  55. * @param {string} clientId - The client ID to be used with the API library.
  56. * @param {boolean} enableYoutube - Whether youtube scope is enabled.
  57. * @param {boolean} enableCalendar - Whether calendar scope is enabled.
  58. * @returns {Promise}
  59. */
  60. initializeClient(clientId, enableYoutube, enableCalendar) {
  61. return this.get()
  62. .then(api => new Promise((resolve, reject) => {
  63. const scope
  64. = `${enableYoutube ? GOOGLE_SCOPE_YOUTUBE : ''} ${enableCalendar ? GOOGLE_SCOPE_CALENDAR : ''}`
  65. .trim();
  66. // setTimeout is used as a workaround for api.client.init not
  67. // resolving consistently when the Google API Client Library is
  68. // loaded asynchronously. See:
  69. // github.com/google/google-api-javascript-client/issues/399
  70. setTimeout(() => {
  71. api.client.init({
  72. clientId,
  73. discoveryDocs: DISCOVERY_DOCS,
  74. scope
  75. })
  76. .then(resolve)
  77. .catch(reject);
  78. }, 500);
  79. }));
  80. },
  81. /**
  82. * Checks whether a user is currently authenticated with Google through an
  83. * initialized Google API Client Library.
  84. *
  85. * @returns {Promise}
  86. */
  87. isSignedIn() {
  88. return this.get()
  89. .then(api => Boolean(api
  90. && api.auth2
  91. && api.auth2.getAuthInstance
  92. && api.auth2.getAuthInstance()
  93. && api.auth2.getAuthInstance().isSignedIn
  94. && api.auth2.getAuthInstance().isSignedIn.get()));
  95. },
  96. /**
  97. * Generates a script tag and downloads the Google API Client Library.
  98. *
  99. * @returns {Promise}
  100. */
  101. load() {
  102. if (googleClientLoadPromise) {
  103. return googleClientLoadPromise;
  104. }
  105. googleClientLoadPromise = new Promise((resolve, reject) => {
  106. const scriptTag = document.createElement('script');
  107. scriptTag.async = true;
  108. scriptTag.addEventListener('error', () => {
  109. scriptTag.remove();
  110. googleClientLoadPromise = null;
  111. reject();
  112. });
  113. scriptTag.addEventListener('load', resolve);
  114. scriptTag.type = 'text/javascript';
  115. scriptTag.src = GOOGLE_API_CLIENT_LIBRARY_URL;
  116. document.head.appendChild(scriptTag);
  117. })
  118. .then(() => new Promise((resolve, reject) =>
  119. this._getGoogleApiClient().load('client:auth2', {
  120. callback: resolve,
  121. onerror: reject
  122. })))
  123. .then(() => this._getGoogleApiClient());
  124. return googleClientLoadPromise;
  125. },
  126. /**
  127. * Executes a request for a list of all YouTube broadcasts associated with
  128. * user currently signed in to the Google API Client Library.
  129. *
  130. * @returns {Promise}
  131. */
  132. requestAvailableYouTubeBroadcasts() {
  133. return this.get()
  134. .then(api => api.client.request(API_URL_LIVE_BROADCASTS));
  135. },
  136. /**
  137. * Executes a request to get all live streams associated with a broadcast
  138. * in YouTube.
  139. *
  140. * @param {string} boundStreamID - The bound stream ID associated with a
  141. * broadcast in YouTube.
  142. * @returns {Promise}
  143. */
  144. requestLiveStreamsForYouTubeBroadcast(boundStreamID) {
  145. return this.get()
  146. .then(api => api.client.request(
  147. `${API_URL_BROADCAST_STREAMS}${boundStreamID}`));
  148. },
  149. /**
  150. * Prompts the participant to sign in to the Google API Client Library, even
  151. * if already signed in.
  152. *
  153. * @returns {Promise}
  154. */
  155. showAccountSelection() {
  156. return this.get()
  157. .then(api => api.auth2.getAuthInstance().signIn());
  158. },
  159. /**
  160. * Prompts the participant to sign in to the Google API Client Library, if
  161. * not already signed in.
  162. *
  163. * @returns {Promise}
  164. */
  165. signInIfNotSignedIn() {
  166. return this.get()
  167. .then(() => this.isSignedIn())
  168. .then(isSignedIn => {
  169. if (!isSignedIn) {
  170. return this.showAccountSelection();
  171. }
  172. });
  173. },
  174. /**
  175. * Sign out from the Google API Client Library.
  176. *
  177. * @returns {Promise}
  178. */
  179. signOut() {
  180. return this.get()
  181. .then(api =>
  182. api.auth2
  183. && api.auth2.getAuthInstance
  184. && api.auth2.getAuthInstance()
  185. && api.auth2.getAuthInstance().signOut());
  186. },
  187. /**
  188. * Parses the google calendar entries to a known format.
  189. *
  190. * @param {Object} entry - The google calendar entry.
  191. * @returns {{
  192. * calendarId: string,
  193. * description: string,
  194. * endDate: string,
  195. * id: string,
  196. * location: string,
  197. * startDate: string,
  198. * title: string}}
  199. * @private
  200. */
  201. _convertCalendarEntry(entry) {
  202. return {
  203. calendarId: entry.calendarId,
  204. description: entry.description,
  205. endDate: entry.end.dateTime,
  206. id: entry.id,
  207. location: entry.location,
  208. startDate: entry.start.dateTime,
  209. title: entry.summary,
  210. url: this._getConferenceDataVideoUri(entry.conferenceData)
  211. };
  212. },
  213. /**
  214. * Checks conference data for jitsi conference solution and returns
  215. * its video url.
  216. *
  217. * @param {Object} conferenceData - The conference data of the event.
  218. * @returns {string|undefined} Returns the found video uri or undefined.
  219. */
  220. _getConferenceDataVideoUri(conferenceData = {}) {
  221. try {
  222. // check conference data coming from calendar addons
  223. if (conferenceData.parameters.addOnParameters.parameters
  224. .conferenceSolutionType === 'jitsi') {
  225. const videoEntry = conferenceData.entryPoints.find(
  226. e => e.entryPointType === 'video');
  227. if (videoEntry) {
  228. return videoEntry.uri;
  229. }
  230. }
  231. } catch (error) {
  232. // we don't care about undefined fields
  233. }
  234. },
  235. /**
  236. * Retrieves calendar entries from all available calendars.
  237. *
  238. * @param {number} fetchStartDays - The number of days to go back
  239. * when fetching.
  240. * @param {number} fetchEndDays - The number of days to fetch.
  241. * @returns {Promise<CalendarEntry>}
  242. * @private
  243. */
  244. _getCalendarEntries(fetchStartDays, fetchEndDays) {
  245. return this.get()
  246. .then(() => this.isSignedIn())
  247. .then(isSignedIn => {
  248. if (!isSignedIn) {
  249. return null;
  250. }
  251. // user can edit the events, so we want only those that
  252. // can be edited
  253. return this._getGoogleApiClient()
  254. .client.calendar.calendarList.list();
  255. })
  256. .then(calendarList => {
  257. // no result, maybe not signed in
  258. if (!calendarList) {
  259. return Promise.resolve();
  260. }
  261. const calendarIds
  262. = calendarList.result.items.map(en => {
  263. return {
  264. id: en.id,
  265. accessRole: en.accessRole
  266. };
  267. });
  268. const promises = calendarIds.map(({ id, accessRole }) => {
  269. const startDate = new Date();
  270. const endDate = new Date();
  271. startDate.setDate(startDate.getDate() + fetchStartDays);
  272. endDate.setDate(endDate.getDate() + fetchEndDays);
  273. // retrieve the events and adds to the result the calendarId
  274. return this._getGoogleApiClient()
  275. .client.calendar.events.list({
  276. 'calendarId': id,
  277. 'timeMin': startDate.toISOString(),
  278. 'timeMax': endDate.toISOString(),
  279. 'showDeleted': false,
  280. 'singleEvents': true,
  281. 'orderBy': 'startTime'
  282. })
  283. .then(result => result.result.items
  284. .map(item => {
  285. const resultItem = { ...item };
  286. // add the calendarId only for the events
  287. // we can edit
  288. if (accessRole === 'writer'
  289. || accessRole === 'owner') {
  290. resultItem.calendarId = id;
  291. }
  292. return resultItem;
  293. }));
  294. });
  295. return Promise.all(promises)
  296. .then(results => [].concat(...results))
  297. .then(entries =>
  298. entries.map(e => this._convertCalendarEntry(e)));
  299. });
  300. },
  301. /* eslint-disable max-params */
  302. /**
  303. * Updates the calendar event and adds a location and text.
  304. *
  305. * @param {string} id - The event id to update.
  306. * @param {string} calendarId - The calendar id to use.
  307. * @param {string} location - The location to add to the event.
  308. * @param {string} text - The description text to set/append.
  309. * @returns {Promise<T | never>}
  310. * @private
  311. */
  312. _updateCalendarEntry(id, calendarId, location, text) {
  313. return this.get()
  314. .then(() => this.isSignedIn())
  315. .then(isSignedIn => {
  316. if (!isSignedIn) {
  317. return null;
  318. }
  319. return this._getGoogleApiClient()
  320. .client.calendar.events.get({
  321. 'calendarId': calendarId,
  322. 'eventId': id
  323. }).then(event => {
  324. let newDescription = text;
  325. if (event.result.description) {
  326. newDescription = `${event.result.description}\n\n${
  327. text}`;
  328. }
  329. return this._getGoogleApiClient()
  330. .client.calendar.events.patch({
  331. 'calendarId': calendarId,
  332. 'eventId': id,
  333. 'description': newDescription,
  334. 'location': location
  335. });
  336. });
  337. });
  338. },
  339. /* eslint-enable max-params */
  340. /**
  341. * Returns the global Google API Client Library object. Direct use of this
  342. * method is discouraged; instead use the {@link get} method.
  343. *
  344. * @private
  345. * @returns {Object|undefined}
  346. */
  347. _getGoogleApiClient() {
  348. return window.gapi;
  349. }
  350. };
  351. export default googleApi;