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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. * @returns {Promise}
  57. */
  58. initializeClient(clientId) {
  59. return this.get()
  60. .then(api => new Promise((resolve, reject) => {
  61. // setTimeout is used as a workaround for api.client.init not
  62. // resolving consistently when the Google API Client Library is
  63. // loaded asynchronously. See:
  64. // github.com/google/google-api-javascript-client/issues/399
  65. setTimeout(() => {
  66. api.client.init({
  67. clientId,
  68. discoveryDocs: DISCOVERY_DOCS,
  69. scope: [
  70. GOOGLE_SCOPE_CALENDAR,
  71. GOOGLE_SCOPE_YOUTUBE
  72. ].join(' ')
  73. })
  74. .then(resolve)
  75. .catch(reject);
  76. }, 500);
  77. }));
  78. },
  79. /**
  80. * Checks whether a user is currently authenticated with Google through an
  81. * initialized Google API Client Library.
  82. *
  83. * @returns {Promise}
  84. */
  85. isSignedIn() {
  86. return this.get()
  87. .then(api => Boolean(api
  88. && api.auth2
  89. && api.auth2.getAuthInstance
  90. && api.auth2.getAuthInstance()
  91. && api.auth2.getAuthInstance().isSignedIn
  92. && api.auth2.getAuthInstance().isSignedIn.get()));
  93. },
  94. /**
  95. * Generates a script tag and downloads the Google API Client Library.
  96. *
  97. * @returns {Promise}
  98. */
  99. load() {
  100. if (googleClientLoadPromise) {
  101. return googleClientLoadPromise;
  102. }
  103. googleClientLoadPromise = new Promise((resolve, reject) => {
  104. const scriptTag = document.createElement('script');
  105. scriptTag.async = true;
  106. scriptTag.addEventListener('error', () => {
  107. scriptTag.remove();
  108. googleClientLoadPromise = null;
  109. reject();
  110. });
  111. scriptTag.addEventListener('load', resolve);
  112. scriptTag.type = 'text/javascript';
  113. scriptTag.src = GOOGLE_API_CLIENT_LIBRARY_URL;
  114. document.head.appendChild(scriptTag);
  115. })
  116. .then(() => new Promise((resolve, reject) =>
  117. this._getGoogleApiClient().load('client:auth2', {
  118. callback: resolve,
  119. onerror: reject
  120. })))
  121. .then(() => this._getGoogleApiClient());
  122. return googleClientLoadPromise;
  123. },
  124. /**
  125. * Executes a request for a list of all YouTube broadcasts associated with
  126. * user currently signed in to the Google API Client Library.
  127. *
  128. * @returns {Promise}
  129. */
  130. requestAvailableYouTubeBroadcasts() {
  131. return this.get()
  132. .then(api => api.client.request(API_URL_LIVE_BROADCASTS));
  133. },
  134. /**
  135. * Executes a request to get all live streams associated with a broadcast
  136. * in YouTube.
  137. *
  138. * @param {string} boundStreamID - The bound stream ID associated with a
  139. * broadcast in YouTube.
  140. * @returns {Promise}
  141. */
  142. requestLiveStreamsForYouTubeBroadcast(boundStreamID) {
  143. return this.get()
  144. .then(api => api.client.request(
  145. `${API_URL_BROADCAST_STREAMS}${boundStreamID}`));
  146. },
  147. /**
  148. * Prompts the participant to sign in to the Google API Client Library, even
  149. * if already signed in.
  150. *
  151. * @returns {Promise}
  152. */
  153. showAccountSelection() {
  154. return this.get()
  155. .then(api => api.auth2.getAuthInstance().signIn());
  156. },
  157. /**
  158. * Prompts the participant to sign in to the Google API Client Library, if
  159. * not already signed in.
  160. *
  161. * @returns {Promise}
  162. */
  163. signInIfNotSignedIn() {
  164. return this.get()
  165. .then(() => this.isSignedIn())
  166. .then(isSignedIn => {
  167. if (!isSignedIn) {
  168. return this.showAccountSelection();
  169. }
  170. });
  171. },
  172. /**
  173. * Sign out from the Google API Client Library.
  174. *
  175. * @returns {Promise}
  176. */
  177. signOut() {
  178. return this.get()
  179. .then(api =>
  180. api.auth2
  181. && api.auth2.getAuthInstance
  182. && api.auth2.getAuthInstance()
  183. && api.auth2.getAuthInstance().signOut());
  184. },
  185. /**
  186. * Parses the google calendar entries to a known format.
  187. *
  188. * @param {Object} entry - The google calendar entry.
  189. * @returns {{
  190. * calendarId: string,
  191. * description: string,
  192. * endDate: string,
  193. * id: string,
  194. * location: string,
  195. * startDate: string,
  196. * title: string}}
  197. * @private
  198. */
  199. _convertCalendarEntry(entry) {
  200. return {
  201. calendarId: entry.calendarId,
  202. description: entry.description,
  203. endDate: entry.end.dateTime,
  204. id: entry.id,
  205. location: entry.location,
  206. startDate: entry.start.dateTime,
  207. title: entry.summary
  208. };
  209. },
  210. /**
  211. * Retrieves calendar entries from all available calendars.
  212. *
  213. * @param {number} fetchStartDays - The number of days to go back
  214. * when fetching.
  215. * @param {number} fetchEndDays - The number of days to fetch.
  216. * @returns {Promise<CalendarEntry>}
  217. * @private
  218. */
  219. _getCalendarEntries(fetchStartDays, fetchEndDays) {
  220. return this.get()
  221. .then(() => this.isSignedIn())
  222. .then(isSignedIn => {
  223. if (!isSignedIn) {
  224. return null;
  225. }
  226. // user can edit the events, so we want only those that
  227. // can be edited
  228. return this._getGoogleApiClient()
  229. .client.calendar.calendarList.list();
  230. })
  231. .then(calendarList => {
  232. // no result, maybe not signed in
  233. if (!calendarList) {
  234. return Promise.resolve();
  235. }
  236. const calendarIds
  237. = calendarList.result.items.map(en => {
  238. return {
  239. id: en.id,
  240. accessRole: en.accessRole
  241. };
  242. });
  243. const promises = calendarIds.map(({ id, accessRole }) => {
  244. const startDate = new Date();
  245. const endDate = new Date();
  246. startDate.setDate(startDate.getDate() + fetchStartDays);
  247. endDate.setDate(endDate.getDate() + fetchEndDays);
  248. // retrieve the events and adds to the result the calendarId
  249. return this._getGoogleApiClient()
  250. .client.calendar.events.list({
  251. 'calendarId': id,
  252. 'timeMin': startDate.toISOString(),
  253. 'timeMax': endDate.toISOString(),
  254. 'showDeleted': false,
  255. 'singleEvents': true,
  256. 'orderBy': 'startTime'
  257. })
  258. .then(result => result.result.items
  259. .map(item => {
  260. const resultItem = { ...item };
  261. // add the calendarId only for the events
  262. // we can edit
  263. if (accessRole === 'writer'
  264. || accessRole === 'owner') {
  265. resultItem.calendarId = id;
  266. }
  267. return resultItem;
  268. }));
  269. });
  270. return Promise.all(promises)
  271. .then(results => [].concat(...results))
  272. .then(entries =>
  273. entries.map(e => this._convertCalendarEntry(e)));
  274. });
  275. },
  276. /* eslint-disable max-params */
  277. /**
  278. * Updates the calendar event and adds a location and text.
  279. *
  280. * @param {string} id - The event id to update.
  281. * @param {string} calendarId - The calendar id to use.
  282. * @param {string} location - The location to add to the event.
  283. * @param {string} text - The description text to set/append.
  284. * @returns {Promise<T | never>}
  285. * @private
  286. */
  287. _updateCalendarEntry(id, calendarId, location, text) {
  288. return this.get()
  289. .then(() => this.isSignedIn())
  290. .then(isSignedIn => {
  291. if (!isSignedIn) {
  292. return null;
  293. }
  294. return this._getGoogleApiClient()
  295. .client.calendar.events.get({
  296. 'calendarId': calendarId,
  297. 'eventId': id
  298. }).then(event => {
  299. let newDescription = text;
  300. if (event.result.description) {
  301. newDescription = `${event.result.description}\n\n${
  302. text}`;
  303. }
  304. return this._getGoogleApiClient()
  305. .client.calendar.events.patch({
  306. 'calendarId': calendarId,
  307. 'eventId': id,
  308. 'description': newDescription,
  309. 'location': location
  310. });
  311. });
  312. });
  313. },
  314. /* eslint-enable max-params */
  315. /**
  316. * Returns the global Google API Client Library object. Direct use of this
  317. * method is discouraged; instead use the {@link get} method.
  318. *
  319. * @private
  320. * @returns {Object|undefined}
  321. */
  322. _getGoogleApiClient() {
  323. return window.gapi;
  324. }
  325. };
  326. export default googleApi;