Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // @flow
  2. import type { Dispatch } from 'redux';
  3. import { getShareInfoText } from '../invite';
  4. import {
  5. SET_GOOGLE_API_PROFILE,
  6. SET_GOOGLE_API_STATE
  7. } from './actionTypes';
  8. import { GOOGLE_API_STATES } from './constants';
  9. import googleApi from './googleApi';
  10. /**
  11. * Retrieves the current calendar events.
  12. *
  13. * @param {number} fetchStartDays - The number of days to go back when fetching.
  14. * @param {number} fetchEndDays - The number of days to fetch.
  15. * @returns {function(Dispatch<any>): Promise<CalendarEntries>}
  16. */
  17. export function getCalendarEntries(
  18. fetchStartDays: ?number, fetchEndDays: ?number) {
  19. return () =>
  20. googleApi.get()
  21. .then(() =>
  22. googleApi._getCalendarEntries(fetchStartDays, fetchEndDays));
  23. }
  24. /**
  25. * Loads Google API.
  26. *
  27. * @returns {Function}
  28. */
  29. export function loadGoogleAPI() {
  30. return (dispatch: Dispatch<any>, getState: Function) =>
  31. googleApi.get()
  32. .then(() => {
  33. const {
  34. liveStreamingEnabled,
  35. enableCalendarIntegration,
  36. googleApiApplicationClientID
  37. } = getState()['features/base/config'];
  38. if (getState()['features/google-api'].googleAPIState
  39. === GOOGLE_API_STATES.NEEDS_LOADING) {
  40. return googleApi.initializeClient(
  41. googleApiApplicationClientID, liveStreamingEnabled, enableCalendarIntegration);
  42. }
  43. return Promise.resolve();
  44. })
  45. .then(() => dispatch(setGoogleAPIState(GOOGLE_API_STATES.LOADED)))
  46. .then(() => googleApi.isSignedIn())
  47. .then(isSignedIn => {
  48. if (isSignedIn) {
  49. dispatch(setGoogleAPIState(GOOGLE_API_STATES.SIGNED_IN));
  50. }
  51. });
  52. }
  53. /**
  54. * Executes a request for a list of all YouTube broadcasts associated with
  55. * user currently signed in to the Google API Client Library.
  56. *
  57. * @returns {function(): (Promise<*>|Promise<any[] | never>)}
  58. */
  59. export function requestAvailableYouTubeBroadcasts() {
  60. return () =>
  61. googleApi.requestAvailableYouTubeBroadcasts()
  62. .then(response => {
  63. // Takes in a list of broadcasts from the YouTube API,
  64. // removes dupes, removes broadcasts that cannot get a stream key,
  65. // and parses the broadcasts into flat objects.
  66. const broadcasts = response.result.items;
  67. const parsedBroadcasts = {};
  68. for (let i = 0; i < broadcasts.length; i++) {
  69. const broadcast = broadcasts[i];
  70. const boundStreamID = broadcast.contentDetails.boundStreamId;
  71. if (boundStreamID && !parsedBroadcasts[boundStreamID]) {
  72. parsedBroadcasts[boundStreamID] = {
  73. boundStreamID,
  74. id: broadcast.id,
  75. status: broadcast.status.lifeCycleStatus,
  76. title: broadcast.snippet.title
  77. };
  78. }
  79. }
  80. return Object.values(parsedBroadcasts);
  81. });
  82. }
  83. /**
  84. * Fetches the stream key for a YouTube broadcast and updates the internal
  85. * state to display the associated stream key as being entered.
  86. *
  87. * @param {string} boundStreamID - The bound stream ID associated with the
  88. * broadcast from which to get the stream key.
  89. * @returns {function(): (Promise<*>|Promise<{
  90. * streamKey: (*|string),
  91. * selectedBoundStreamID: *} | never>)}
  92. */
  93. export function requestLiveStreamsForYouTubeBroadcast(boundStreamID: string) {
  94. return () =>
  95. googleApi.requestLiveStreamsForYouTubeBroadcast(boundStreamID)
  96. .then(response => {
  97. const broadcasts = response.result.items;
  98. const streamName = broadcasts
  99. && broadcasts[0]
  100. && broadcasts[0].cdn.ingestionInfo.streamName;
  101. const streamKey = streamName || '';
  102. return {
  103. streamKey,
  104. selectedBoundStreamID: boundStreamID
  105. };
  106. });
  107. }
  108. /**
  109. * Sets the current Google API state.
  110. *
  111. * @param {number} googleAPIState - The state to be set.
  112. * @param {Object} googleResponse - The last response from Google.
  113. * @returns {{
  114. * type: SET_GOOGLE_API_STATE,
  115. * googleAPIState: number
  116. * }}
  117. */
  118. export function setGoogleAPIState(
  119. googleAPIState: number, googleResponse: ?Object) {
  120. return {
  121. type: SET_GOOGLE_API_STATE,
  122. googleAPIState,
  123. googleResponse
  124. };
  125. }
  126. /**
  127. * Forces the Google web client application to prompt for a sign in, such as
  128. * when changing account, and will then fetch available YouTube broadcasts.
  129. *
  130. * @returns {function(): (Promise<*>|Promise<{
  131. * streamKey: (*|string),
  132. * selectedBoundStreamID: *} | never>)}
  133. */
  134. export function showAccountSelection() {
  135. return () =>
  136. googleApi.showAccountSelection();
  137. }
  138. /**
  139. * Prompts the participant to sign in to the Google API Client Library.
  140. *
  141. * @returns {function(Dispatch<any>): Promise<string | never>}
  142. */
  143. export function signIn() {
  144. return (dispatch: Dispatch<any>) => googleApi.get()
  145. .then(() => googleApi.signInIfNotSignedIn())
  146. .then(() => dispatch({
  147. type: SET_GOOGLE_API_STATE,
  148. googleAPIState: GOOGLE_API_STATES.SIGNED_IN
  149. }));
  150. }
  151. /**
  152. * Logs out the user.
  153. *
  154. * @returns {function(Dispatch<any>): Promise<string | never>}
  155. */
  156. export function signOut() {
  157. return (dispatch: Dispatch<any>) =>
  158. googleApi.get()
  159. .then(() => googleApi.signOut())
  160. .then(() => {
  161. dispatch({
  162. type: SET_GOOGLE_API_STATE,
  163. googleAPIState: GOOGLE_API_STATES.LOADED
  164. });
  165. dispatch({
  166. type: SET_GOOGLE_API_PROFILE,
  167. profileEmail: ''
  168. });
  169. });
  170. }
  171. /**
  172. * Updates the profile data that is currently used.
  173. *
  174. * @returns {function(Dispatch<any>): Promise<string | never>}
  175. */
  176. export function updateProfile() {
  177. return (dispatch: Dispatch<any>) => googleApi.get()
  178. .then(() => googleApi.signInIfNotSignedIn())
  179. .then(() => dispatch({
  180. type: SET_GOOGLE_API_STATE,
  181. googleAPIState: GOOGLE_API_STATES.SIGNED_IN
  182. }))
  183. .then(() => googleApi.getCurrentUserProfile())
  184. .then(profile => {
  185. dispatch({
  186. type: SET_GOOGLE_API_PROFILE,
  187. profileEmail: profile.getEmail()
  188. });
  189. return profile.getEmail();
  190. });
  191. }
  192. /**
  193. * Updates the calendar event and adds a location and text.
  194. *
  195. * @param {string} id - The event id to update.
  196. * @param {string} calendarId - The calendar id to use.
  197. * @param {string} location - The location to add to the event.
  198. * @returns {function(Dispatch<any>): Promise<string | never>}
  199. */
  200. export function updateCalendarEvent(
  201. id: string, calendarId: string, location: string) {
  202. return (dispatch: Dispatch<any>, getState: Function) =>
  203. getShareInfoText(getState(), location)
  204. .then(text =>
  205. googleApi._updateCalendarEntry(id, calendarId, location, text));
  206. }