Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

googleApi.js 12KB

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