您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

googleApi.web.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. url: this._getConferenceDataVideoUri(entry.conferenceData)
  209. };
  210. },
  211. /**
  212. * Checks conference data for jitsi conference solution and returns
  213. * its video url.
  214. *
  215. * @param {Object} conferenceData - The conference data of the event.
  216. * @returns {string|undefined} Returns the found video uri or undefined.
  217. */
  218. _getConferenceDataVideoUri(conferenceData = {}) {
  219. try {
  220. // check conference data coming from calendar addons
  221. if (conferenceData.parameters.addOnParameters.parameters
  222. .conferenceSolutionType === 'jitsi') {
  223. const videoEntry = conferenceData.entryPoints.find(
  224. e => e.entryPointType === 'video');
  225. if (videoEntry) {
  226. return videoEntry.uri;
  227. }
  228. }
  229. } catch (error) {
  230. // we don't care about undefined fields
  231. }
  232. },
  233. /**
  234. * Retrieves calendar entries from all available calendars.
  235. *
  236. * @param {number} fetchStartDays - The number of days to go back
  237. * when fetching.
  238. * @param {number} fetchEndDays - The number of days to fetch.
  239. * @returns {Promise<CalendarEntry>}
  240. * @private
  241. */
  242. _getCalendarEntries(fetchStartDays, fetchEndDays) {
  243. return this.get()
  244. .then(() => this.isSignedIn())
  245. .then(isSignedIn => {
  246. if (!isSignedIn) {
  247. return null;
  248. }
  249. // user can edit the events, so we want only those that
  250. // can be edited
  251. return this._getGoogleApiClient()
  252. .client.calendar.calendarList.list();
  253. })
  254. .then(calendarList => {
  255. // no result, maybe not signed in
  256. if (!calendarList) {
  257. return Promise.resolve();
  258. }
  259. const calendarIds
  260. = calendarList.result.items.map(en => {
  261. return {
  262. id: en.id,
  263. accessRole: en.accessRole
  264. };
  265. });
  266. const promises = calendarIds.map(({ id, accessRole }) => {
  267. const startDate = new Date();
  268. const endDate = new Date();
  269. startDate.setDate(startDate.getDate() + fetchStartDays);
  270. endDate.setDate(endDate.getDate() + fetchEndDays);
  271. // retrieve the events and adds to the result the calendarId
  272. return this._getGoogleApiClient()
  273. .client.calendar.events.list({
  274. 'calendarId': id,
  275. 'timeMin': startDate.toISOString(),
  276. 'timeMax': endDate.toISOString(),
  277. 'showDeleted': false,
  278. 'singleEvents': true,
  279. 'orderBy': 'startTime'
  280. })
  281. .then(result => result.result.items
  282. .map(item => {
  283. const resultItem = { ...item };
  284. // add the calendarId only for the events
  285. // we can edit
  286. if (accessRole === 'writer'
  287. || accessRole === 'owner') {
  288. resultItem.calendarId = id;
  289. }
  290. return resultItem;
  291. }));
  292. });
  293. return Promise.all(promises)
  294. .then(results => [].concat(...results))
  295. .then(entries =>
  296. entries.map(e => this._convertCalendarEntry(e)));
  297. });
  298. },
  299. /* eslint-disable max-params */
  300. /**
  301. * Updates the calendar event and adds a location and text.
  302. *
  303. * @param {string} id - The event id to update.
  304. * @param {string} calendarId - The calendar id to use.
  305. * @param {string} location - The location to add to the event.
  306. * @param {string} text - The description text to set/append.
  307. * @returns {Promise<T | never>}
  308. * @private
  309. */
  310. _updateCalendarEntry(id, calendarId, location, text) {
  311. return this.get()
  312. .then(() => this.isSignedIn())
  313. .then(isSignedIn => {
  314. if (!isSignedIn) {
  315. return null;
  316. }
  317. return this._getGoogleApiClient()
  318. .client.calendar.events.get({
  319. 'calendarId': calendarId,
  320. 'eventId': id
  321. }).then(event => {
  322. let newDescription = text;
  323. if (event.result.description) {
  324. newDescription = `${event.result.description}\n\n${
  325. text}`;
  326. }
  327. return this._getGoogleApiClient()
  328. .client.calendar.events.patch({
  329. 'calendarId': calendarId,
  330. 'eventId': id,
  331. 'description': newDescription,
  332. 'location': location
  333. });
  334. });
  335. });
  336. },
  337. /* eslint-enable max-params */
  338. /**
  339. * Returns the global Google API Client Library object. Direct use of this
  340. * method is discouraged; instead use the {@link get} method.
  341. *
  342. * @private
  343. * @returns {Object|undefined}
  344. */
  345. _getGoogleApiClient() {
  346. return window.gapi;
  347. }
  348. };
  349. export default googleApi;