123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- /* @flow */
-
- import {
- SET_GOOGLE_API_PROFILE,
- SET_GOOGLE_API_STATE
- } from './actionTypes';
- import { GOOGLE_API_STATES } from './constants';
- import googleApi from './googleApi';
-
- /**
- * Retrieves the current calendar events.
- *
- * @param {number} fetchStartDays - The number of days to go back when fetching.
- * @param {number} fetchEndDays - The number of days to fetch.
- * @returns {function(Dispatch<*>): Promise<CalendarEntries>}
- */
- export function getCalendarEntries(
- fetchStartDays: ?number, fetchEndDays: ?number) {
- return () =>
- googleApi.get()
- .then(() =>
- googleApi._getCalendarEntries(fetchStartDays, fetchEndDays));
- }
-
- /**
- * Loads Google API.
- *
- * @param {string} clientId - The client ID to be used with the API library.
- * @returns {Function}
- */
- export function loadGoogleAPI(clientId: string) {
- return (dispatch: Dispatch<*>, getState: Function) =>
- googleApi.get()
- .then(() => {
- if (getState()['features/google-api'].googleAPIState
- === GOOGLE_API_STATES.NEEDS_LOADING) {
- return googleApi.initializeClient(clientId);
- }
-
- return Promise.resolve();
- })
- .then(() => dispatch({
- type: SET_GOOGLE_API_STATE,
- googleAPIState: GOOGLE_API_STATES.LOADED }))
- .then(() => googleApi.isSignedIn())
- .then(isSignedIn => {
- if (isSignedIn) {
- dispatch({
- type: SET_GOOGLE_API_STATE,
- googleAPIState: GOOGLE_API_STATES.SIGNED_IN });
- }
- });
- }
-
- /**
- * Executes a request for a list of all YouTube broadcasts associated with
- * user currently signed in to the Google API Client Library.
- *
- * @returns {function(): (Promise<*>|Promise<any[] | never>)}
- */
- export function requestAvailableYouTubeBroadcasts() {
- return () =>
- googleApi.requestAvailableYouTubeBroadcasts()
- .then(response => {
- // Takes in a list of broadcasts from the YouTube API,
- // removes dupes, removes broadcasts that cannot get a stream key,
- // and parses the broadcasts into flat objects.
- const broadcasts = response.result.items;
-
- const parsedBroadcasts = {};
-
- for (let i = 0; i < broadcasts.length; i++) {
- const broadcast = broadcasts[i];
- const boundStreamID = broadcast.contentDetails.boundStreamId;
-
- if (boundStreamID && !parsedBroadcasts[boundStreamID]) {
- parsedBroadcasts[boundStreamID] = {
- boundStreamID,
- id: broadcast.id,
- status: broadcast.status.lifeCycleStatus,
- title: broadcast.snippet.title
- };
- }
- }
-
- return Object.values(parsedBroadcasts);
- });
- }
-
- /**
- * Fetches the stream key for a YouTube broadcast and updates the internal
- * state to display the associated stream key as being entered.
- *
- * @param {string} boundStreamID - The bound stream ID associated with the
- * broadcast from which to get the stream key.
- * @returns {function(): (Promise<*>|Promise<{
- * streamKey: (*|string),
- * selectedBoundStreamID: *} | never>)}
- */
- export function requestLiveStreamsForYouTubeBroadcast(boundStreamID: string) {
- return () =>
- googleApi.requestLiveStreamsForYouTubeBroadcast(boundStreamID)
- .then(response => {
- const broadcasts = response.result.items;
- const streamName = broadcasts
- && broadcasts[0]
- && broadcasts[0].cdn.ingestionInfo.streamName;
- const streamKey = streamName || '';
-
- return {
- streamKey,
- selectedBoundStreamID: boundStreamID
- };
- });
- }
-
- /**
- * Forces the Google web client application to prompt for a sign in, such as
- * when changing account, and will then fetch available YouTube broadcasts.
- *
- * @returns {function(): (Promise<*>|Promise<{
- * streamKey: (*|string),
- * selectedBoundStreamID: *} | never>)}
- */
- export function showAccountSelection() {
- return () =>
- googleApi.showAccountSelection();
- }
-
- /**
- * Prompts the participant to sign in to the Google API Client Library.
- *
- * @returns {function(Dispatch<*>): Promise<string | never>}
- */
- export function signIn() {
- return (dispatch: Dispatch<*>) => googleApi.get()
- .then(() => googleApi.signInIfNotSignedIn())
- .then(() => dispatch({
- type: SET_GOOGLE_API_STATE,
- googleAPIState: GOOGLE_API_STATES.SIGNED_IN
- }));
- }
-
- /**
- * Logs out the user.
- *
- * @returns {function(Dispatch<*>): Promise<string | never>}
- */
- export function signOut() {
- return (dispatch: Dispatch<*>) =>
- googleApi.get()
- .then(() => googleApi.signOut())
- .then(() => {
- dispatch({
- type: SET_GOOGLE_API_STATE,
- googleAPIState: GOOGLE_API_STATES.LOADED
- });
- dispatch({
- type: SET_GOOGLE_API_PROFILE,
- profileEmail: ''
- });
- });
- }
-
- /**
- * Updates the profile data that is currently used.
- *
- * @returns {function(Dispatch<*>): Promise<string | never>}
- */
- export function updateProfile() {
- return (dispatch: Dispatch<*>) => googleApi.get()
- .then(() => googleApi.signInIfNotSignedIn())
- .then(() => dispatch({
- type: SET_GOOGLE_API_STATE,
- googleAPIState: GOOGLE_API_STATES.SIGNED_IN
- }))
- .then(() => googleApi.getCurrentUserProfile())
- .then(profile => {
- dispatch({
- type: SET_GOOGLE_API_PROFILE,
- profileEmail: profile.getEmail()
- });
-
- return profile.getEmail();
- });
- }
|