Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

actions.js 6.8KB

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