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.

actions.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* @flow */
  2. import {
  3. SET_GOOGLE_API_PROFILE,
  4. SET_GOOGLE_API_STATE
  5. } from './actionTypes';
  6. import { GOOGLE_API_STATES } from './constants';
  7. import googleApi from './googleApi';
  8. /**
  9. * Loads Google API.
  10. *
  11. * @param {string} clientId - The client ID to be used with the API library.
  12. * @returns {Function}
  13. */
  14. export function loadGoogleAPI(clientId: string) {
  15. return (dispatch: Dispatch<*>) =>
  16. googleApi.get()
  17. .then(() => googleApi.initializeClient(clientId))
  18. .then(() => dispatch({
  19. type: SET_GOOGLE_API_STATE,
  20. googleAPIState: GOOGLE_API_STATES.LOADED }))
  21. .then(() => googleApi.isSignedIn())
  22. .then(isSignedIn => {
  23. if (isSignedIn) {
  24. dispatch({
  25. type: SET_GOOGLE_API_STATE,
  26. googleAPIState: GOOGLE_API_STATES.SIGNED_IN });
  27. }
  28. });
  29. }
  30. /**
  31. * Prompts the participant to sign in to the Google API Client Library.
  32. *
  33. * @returns {function(Dispatch<*>): Promise<string | never>}
  34. */
  35. export function signIn() {
  36. return (dispatch: Dispatch<*>) => googleApi.get()
  37. .then(() => googleApi.signInIfNotSignedIn())
  38. .then(() => dispatch({
  39. type: SET_GOOGLE_API_STATE,
  40. googleAPIState: GOOGLE_API_STATES.SIGNED_IN
  41. }));
  42. }
  43. /**
  44. * Updates the profile data that is currently used.
  45. *
  46. * @returns {function(Dispatch<*>): Promise<string | never>}
  47. */
  48. export function updateProfile() {
  49. return (dispatch: Dispatch<*>) => googleApi.get()
  50. .then(() => googleApi.signInIfNotSignedIn())
  51. .then(() => dispatch({
  52. type: SET_GOOGLE_API_STATE,
  53. googleAPIState: GOOGLE_API_STATES.SIGNED_IN
  54. }))
  55. .then(() => googleApi.getCurrentUserProfile())
  56. .then(profile => dispatch({
  57. type: SET_GOOGLE_API_PROFILE,
  58. profileEmail: profile.getEmail()
  59. }));
  60. }
  61. /**
  62. * Executes a request for a list of all YouTube broadcasts associated with
  63. * user currently signed in to the Google API Client Library.
  64. *
  65. * @returns {function(): (Promise<*>|Promise<any[] | never>)}
  66. */
  67. export function requestAvailableYouTubeBroadcasts() {
  68. return () =>
  69. googleApi.requestAvailableYouTubeBroadcasts()
  70. .then(response => {
  71. // Takes in a list of broadcasts from the YouTube API,
  72. // removes dupes, removes broadcasts that cannot get a stream key,
  73. // and parses the broadcasts into flat objects.
  74. const broadcasts = response.result.items;
  75. const parsedBroadcasts = {};
  76. for (let i = 0; i < broadcasts.length; i++) {
  77. const broadcast = broadcasts[i];
  78. const boundStreamID = broadcast.contentDetails.boundStreamId;
  79. if (boundStreamID && !parsedBroadcasts[boundStreamID]) {
  80. parsedBroadcasts[boundStreamID] = {
  81. boundStreamID,
  82. id: broadcast.id,
  83. status: broadcast.status.lifeCycleStatus,
  84. title: broadcast.snippet.title
  85. };
  86. }
  87. }
  88. return Object.values(parsedBroadcasts);
  89. });
  90. }
  91. /**
  92. * Fetches the stream key for a YouTube broadcast and updates the internal
  93. * state to display the associated stream key as being entered.
  94. *
  95. * @param {string} boundStreamID - The bound stream ID associated with the
  96. * broadcast from which to get the stream key.
  97. * @returns {function(): (Promise<*>|Promise<{
  98. * streamKey: (*|string),
  99. * selectedBoundStreamID: *} | never>)}
  100. */
  101. export function requestLiveStreamsForYouTubeBroadcast(boundStreamID: string) {
  102. return () =>
  103. googleApi.requestLiveStreamsForYouTubeBroadcast(boundStreamID)
  104. .then(response => {
  105. const broadcasts = response.result.items;
  106. const streamName = broadcasts
  107. && broadcasts[0]
  108. && broadcasts[0].cdn.ingestionInfo.streamName;
  109. const streamKey = streamName || '';
  110. return {
  111. streamKey,
  112. selectedBoundStreamID: boundStreamID
  113. };
  114. });
  115. }
  116. /**
  117. * Forces the Google web client application to prompt for a sign in, such as
  118. * when changing account, and will then fetch available YouTube broadcasts.
  119. *
  120. * @returns {function(): (Promise<*>|Promise<{
  121. * streamKey: (*|string),
  122. * selectedBoundStreamID: *} | never>)}
  123. */
  124. export function showAccountSelection() {
  125. return () =>
  126. googleApi.showAccountSelection();
  127. }