From 90e8c74006011f7ca755e88b9e5ad2c79d6f2681 Mon Sep 17 00:00:00 2001 From: Sean Yesmunt Date: Tue, 15 Oct 2019 00:20:12 -0400 Subject: [PATCH] use sdk preference endpoints --- package.json | 4 +- src/ui/component/app/index.js | 4 +- src/ui/component/app/view.jsx | 9 ++-- src/ui/component/userEmailNew/index.js | 2 +- src/ui/component/userSignIn/view.jsx | 2 +- src/ui/index.jsx | 2 +- src/ui/page/settings/view.jsx | 8 +-- src/ui/redux/actions/app.js | 42 ++++++++++----- src/ui/redux/actions/subscriptions.js | 21 +++----- src/ui/store.js | 72 ++++++++++++++++---------- src/ui/util/saved-passwords.js | 6 +++ yarn.lock | 8 +-- 12 files changed, 110 insertions(+), 70 deletions(-) diff --git a/package.json b/package.json index a9f518080..4656bfd72 100644 --- a/package.json +++ b/package.json @@ -129,8 +129,8 @@ "husky": "^0.14.3", "json-loader": "^0.5.4", "lbry-format": "https://github.com/lbryio/lbry-format.git", - "lbry-redux": "lbryio/lbry-redux#0a1c95a08835a6b892d853b156d4934e469c9589", - "lbryinc": "lbryio/lbryinc#d1dba98bb6f1dc67bc0db4c0a20fc13b8a0de98b", + "lbry-redux": "lbryio/lbry-redux#cd69946d4ca016c896639aa401d8f6b87d3e410e", + "lbryinc": "lbryio/lbryinc#b8e1708ee4491db342c81576265e1b58f542bedb", "lint-staged": "^7.0.2", "localforage": "^1.7.1", "lodash-es": "^4.17.14", diff --git a/src/ui/component/app/index.js b/src/ui/component/app/index.js index 8788fb428..fd1e312fa 100644 --- a/src/ui/component/app/index.js +++ b/src/ui/component/app/index.js @@ -1,7 +1,7 @@ import * as SETTINGS from 'constants/settings'; import { hot } from 'react-hot-loader/root'; import { connect } from 'react-redux'; -import { selectUser, doRewardList, doFetchRewardedContent, doFetchAccessToken } from 'lbryinc'; +import { selectUser, doRewardList, doFetchRewardedContent, doAuthenticate } from 'lbryinc'; import { doFetchTransactions, doFetchChannelListMine, selectBalance } from 'lbry-redux'; import { makeSelectClientSetting, selectThemePath } from 'redux/selectors/settings'; import { selectIsUpgradeAvailable, selectAutoUpdateDownloaded } from 'redux/selectors/app'; @@ -21,7 +21,7 @@ const perform = dispatch => ({ fetchRewards: () => dispatch(doRewardList()), fetchRewardedContent: () => dispatch(doFetchRewardedContent()), fetchTransactions: () => dispatch(doFetchTransactions()), - fetchAccessToken: () => dispatch(doFetchAccessToken()), + fetchAccessToken: () => dispatch(doAuthenticate()), fetchChannelListMine: () => dispatch(doFetchChannelListMine()), signIn: () => dispatch(doSignIn()), requestDownloadUpgrade: () => dispatch(doDownloadUpgradeRequested()), diff --git a/src/ui/component/app/view.jsx b/src/ui/component/app/view.jsx index f83d1bf8c..456dd9cb4 100644 --- a/src/ui/component/app/view.jsx +++ b/src/ui/component/app/view.jsx @@ -1,6 +1,6 @@ // @flow import * as ICONS from 'constants/icons'; -import React, { useEffect, useRef } from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import classnames from 'classnames'; import analytics from 'analytics'; import { buildURI, parseURI } from 'lbry-redux'; @@ -56,6 +56,7 @@ function App(props: Props) { } = props; const appRef = useRef(); const isEnhancedLayout = useKonamiListener(); + const [hasSignedIn, setHasSignedIn] = useState(false); const userId = user && user.id; const hasVerifiedEmail = user && user.has_verified_email; const isRewardApproved = user && user.is_reward_approved; @@ -112,10 +113,12 @@ function App(props: Props) { useEffect(() => { // Wait for balance to be populated on desktop so we know when we can begin syncing // @syncwithbalancefixme - if (hasVerifiedEmail && (IS_WEB || balance !== undefined)) { + if (!hasSignedIn && hasVerifiedEmail && (IS_WEB || balance !== undefined)) { signIn(); + + setHasSignedIn(true); } - }, [hasVerifiedEmail, signIn, balance]); + }, [hasVerifiedEmail, signIn, balance, hasSignedIn]); if (!user) { return null; diff --git a/src/ui/component/userEmailNew/index.js b/src/ui/component/userEmailNew/index.js index 6b18ebf16..17530ee20 100644 --- a/src/ui/component/userEmailNew/index.js +++ b/src/ui/component/userEmailNew/index.js @@ -2,8 +2,8 @@ import * as SETTINGS from 'constants/settings'; import { connect } from 'react-redux'; import { selectBalance } from 'lbry-redux'; import { selectEmailNewIsPending, selectEmailNewErrorMessage, doUserEmailNew } from 'lbryinc'; -import { makeSelectClientSetting } from 'redux/selectors/settings'; import { doSetClientSetting } from 'redux/actions/settings'; +import { makeSelectClientSetting } from 'redux/selectors/settings'; import UserEmailNew from './view'; const select = state => ({ diff --git a/src/ui/component/userSignIn/view.jsx b/src/ui/component/userSignIn/view.jsx index 5eede6c02..006afe287 100644 --- a/src/ui/component/userSignIn/view.jsx +++ b/src/ui/component/userSignIn/view.jsx @@ -119,8 +119,8 @@ function UserSignIn(props: Props) { ), - showSyncPassword && , // @endif + showSyncPassword && , showLoadingSpinner && (
diff --git a/src/ui/index.jsx b/src/ui/index.jsx index 12c9d0592..fd02df782 100644 --- a/src/ui/index.jsx +++ b/src/ui/index.jsx @@ -54,7 +54,7 @@ if (process.env.SEARCH_API_URL) { } // @if TARGET='web' -const SDK_API_URL = process.env.SDK_API_URL || 'https://api.lbry.tv/api/proxy'; +const SDK_API_URL = process.env.SDK_API_URL || 'https://api.lbry.tv/api/v1/proxy'; Lbry.setDaemonConnectionString(SDK_API_URL); // @endif diff --git a/src/ui/page/settings/view.jsx b/src/ui/page/settings/view.jsx index 5a8351096..00a23561e 100644 --- a/src/ui/page/settings/view.jsx +++ b/src/ui/page/settings/view.jsx @@ -52,7 +52,7 @@ type Props = { themes: Array, automaticDarkModeEnabled: boolean, autoplay: boolean, - autoDownload: boolean, + // autoDownload: boolean, encryptWallet: () => void, decryptWallet: () => void, updateWalletStatus: () => void, @@ -198,7 +198,7 @@ class SettingsPage extends React.PureComponent { autoplay, walletEncrypted, osNotificationsEnabled, - autoDownload, + // autoDownload, setDaemonSetting, setClientSetting, supportOption, @@ -588,7 +588,7 @@ class SettingsPage extends React.PureComponent { /> {/* @if TARGET='app' */} - setClientSetting(SETTINGS.AUTO_DOWNLOAD, !autoDownload)} @@ -597,7 +597,7 @@ class SettingsPage extends React.PureComponent { helper={__( "The latest file from each of your subscriptions will be downloaded for quick access as soon as it's published." )} - /> + /> */} { + function handlePreferencesAfterSync() { + function successCb(savedPreferences) { + dispatch(doPopulateSharedUserState(savedPreferences)); + } + + function failCb() { + dispatch( + doToast({ + isError: true, + message: __('Unable to load your saved preferences.'), + }) + ); + } + + doPreferenceGet('shared', successCb, failCb); + } + // The balance is subscribed to on launch for desktop // @if TARGET='web' const { auth_token: authToken } = cookie.parse(document.cookie); Lbry.setApiHeader('X-Lbry-Auth-Token', authToken); dispatch(doBalanceSubscribe()); dispatch(doFetchChannelListMine()); - dispatch(doCheckSubscriptionsInit()); // @endif - // @if TARGET='app' const state = getState(); const syncEnabled = makeSelectClientSetting(SETTINGS.ENABLE_SYNC)(state); + const hasSyncedBefore = selectSyncHash(state); const balance = selectBalance(state); // For existing users, check if they've synced before, or have 0 balance - if (syncEnabled && (hasSyncedBefore || balance === 0)) { + // Always sync for web + const canSync = syncEnabled && (hasSyncedBefore || balance === 0 || IS_WEB); + + if (canSync) { getSavedPassword().then(password => { const passwordArgument = password === null ? '' : password; - dispatch(doGetSync(passwordArgument, !hasSyncedBefore)); + + // Only set the default account if they have never synced before + dispatch(doGetSync(passwordArgument, handlePreferencesAfterSync)); setInterval(() => { - dispatch(doGetSync(passwordArgument)); + dispatch(doGetSync(passwordArgument, handlePreferencesAfterSync)); }, 1000 * 60 * 5); }); } - // @endif - - Lbryio.call('user_settings', 'get').then(settings => { - dispatch(doPopulateSharedUserState(settings)); - }); }; } diff --git a/src/ui/redux/actions/subscriptions.js b/src/ui/redux/actions/subscriptions.js index bd910724a..79bfcaca8 100644 --- a/src/ui/redux/actions/subscriptions.js +++ b/src/ui/redux/actions/subscriptions.js @@ -1,16 +1,8 @@ // @flow -import { PAGE_SIZE } from 'constants/claim'; import * as ACTIONS from 'constants/action_types'; -import * as SETTINGS from 'constants/settings'; -import * as NOTIFICATION_TYPES from 'constants/subscriptions'; import { Lbryio, rewards, doClaimRewardType } from 'lbryinc'; -import { selectSubscriptions, selectUnreadByChannel } from 'redux/selectors/subscriptions'; -import { makeSelectClientSetting } from 'redux/selectors/settings'; -import { Lbry, parseURI, doResolveUris } from 'lbry-redux'; -import { doPlayUri } from 'redux/actions/content'; - -const CHECK_SUBSCRIPTIONS_INTERVAL = 15 * 60 * 1000; -const SUBSCRIPTION_DOWNLOAD_LIMIT = 1; +import { selectUnreadByChannel } from 'redux/selectors/subscriptions'; +import { parseURI, doResolveUris } from 'lbry-redux'; export const doSetViewMode = (viewMode: ViewMode) => (dispatch: Dispatch) => dispatch({ @@ -73,7 +65,6 @@ export const doFetchMySubscriptions = () => (dispatch: Dispatch, getState: GetSt }); dispatch(doResolveUris(subscriptions.map(({ uri }) => uri))); - dispatch(doCheckSubscriptions()); }) .catch(() => { dispatch({ @@ -190,6 +181,7 @@ export const doRemoveUnreadSubscription = (channelUri: string, readUri: string) dispatch(doRemoveUnreadSubscriptions(channelUri, [readUri])); }; +<<<<<<< HEAD export const doCheckSubscription = (subscriptionUri: string, shouldNotify?: boolean) => ( dispatch: Dispatch, getState: GetState @@ -290,6 +282,8 @@ export const doCheckSubscription = (subscriptionUri: string, shouldNotify?: bool }); }; +======= +>>>>>>> use sdk preference endpoints export const doChannelSubscribe = (subscription: Subscription) => (dispatch: Dispatch, getState: GetState) => { const { settings: { daemonSettings }, @@ -318,8 +312,6 @@ export const doChannelSubscribe = (subscription: Subscription) => (dispatch: Dis dispatch(doClaimRewardType(rewards.TYPE_SUBSCRIPTION, { failSilently: true })); } - - dispatch(doCheckSubscription(subscription.uri, true)); }; export const doChannelUnsubscribe = (subscription: Subscription) => (dispatch: Dispatch, getState: GetState) => { @@ -342,6 +334,7 @@ export const doChannelUnsubscribe = (subscription: Subscription) => (dispatch: D } }; +<<<<<<< HEAD export const doCheckSubscriptions = () => (dispatch: Dispatch, getState: GetState) => { const state = getState(); const subscriptions = selectSubscriptions(state); @@ -363,6 +356,8 @@ export const doCheckSubscriptionsInit = () => (dispatch: Dispatch) => { }); }; +======= +>>>>>>> use sdk preference endpoints export const doFetchRecommendedSubscriptions = () => (dispatch: Dispatch) => { dispatch({ type: ACTIONS.GET_SUGGESTED_SUBSCRIPTIONS_START, diff --git a/src/ui/store.js b/src/ui/store.js index ceeb6a379..9d7e7fbdc 100644 --- a/src/ui/store.js +++ b/src/ui/store.js @@ -1,3 +1,5 @@ +import * as ACTIONS from 'constants/action_types'; +import * as SETTINGS from 'constants/settings'; import { persistStore, persistReducer } from 'redux-persist'; import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2'; import createCompressor from 'redux-persist-transform-compress'; @@ -8,8 +10,10 @@ import thunk from 'redux-thunk'; import { createHashHistory, createBrowserHistory } from 'history'; import { routerMiddleware } from 'connected-react-router'; import createRootReducer from './reducers'; -import { Lbryio } from 'lbryinc'; -import isEqual from 'util/deep-equal'; +import { buildSharedStateMiddleware, ACTIONS as LBRY_REDUX_ACTIONS } from 'lbry-redux'; +import { doGetSync, selectUserVerifiedEmail } from 'lbryinc'; +import { getSavedPassword } from 'util/saved-passwords'; +import { makeSelectClientSetting } from 'redux/selectors/settings'; function isFunction(object) { return typeof object === 'function'; @@ -50,6 +54,7 @@ const appFilter = createFilter('app', ['hasClickedComment', 'searchOptionsExpand const walletFilter = createFilter('wallet', ['receiveAddress']); const searchFilter = createFilter('search', ['options']); const tagsFilter = createFilter('tags', ['followedTags']); +const subscriptionsFilter = createFilter('subscriptions', ['subscriptions']); const blockedFilter = createFilter('blocked', ['blockedChannels']); const whiteListedReducers = [ // @if TARGET='app' @@ -64,6 +69,7 @@ const whiteListedReducers = [ 'blocked', 'settings', 'sync', + 'subscriptions', ]; const transforms = [ @@ -77,6 +83,7 @@ const transforms = [ searchFilter, tagsFilter, contentFilter, + subscriptionsFilter, createCompressor(), ]; @@ -98,10 +105,46 @@ history = createHashHistory(); history = createBrowserHistory(); // @endif +const sharedStateActions = [ + ACTIONS.CHANNEL_SUBSCRIBE, + ACTIONS.CHANNEL_UNSUBSCRIBE, + LBRY_REDUX_ACTIONS.TOGGLE_TAG_FOLLOW, + LBRY_REDUX_ACTIONS.TOGGLE_BLOCK_CHANNEL, +]; + +/** + * source: the reducer name + * property: the property in the reducer-specific state + * transform: optional method to modify the value to be stored + */ +const sharedStateFilters = { + tags: { source: 'tags', property: 'followedTags' }, + subscriptions: { + source: 'subscriptions', + property: 'subscriptions', + transform: function(value) { + return value.map(({ uri }) => uri); + }, + }, + blocked: { source: 'blocked', property: 'blockedChannels' }, +}; + +const sharedStateCb = ({ dispatch, getState }) => { + const state = getState(); + const syncEnabled = makeSelectClientSetting(SETTINGS.ENABLE_SYNC)(state); + const emailVerified = selectUserVerifiedEmail(state); + if (syncEnabled && emailVerified) { + getSavedPassword().then(savedPassword => { + dispatch(doGetSync(savedPassword)); + }); + } +}; + +const sharedStateMiddleware = buildSharedStateMiddleware(sharedStateActions, sharedStateFilters, sharedStateCb); const rootReducer = createRootReducer(history); const persistedReducer = persistReducer(persistOptions, rootReducer); const bulkThunk = createBulkThunkMiddleware(); -const middleware = [routerMiddleware(history), thunk, bulkThunk]; +const middleware = [sharedStateMiddleware, routerMiddleware(history), thunk, bulkThunk]; const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; const store = createStore( enableBatching(persistedReducer), @@ -109,29 +152,6 @@ const store = createStore( composeEnhancers(applyMiddleware(...middleware)) ); -let currentPayload; -store.subscribe(() => { - const state = store.getState(); - const subscriptions = state.subscriptions.subscriptions.map(({ uri }) => uri); - const tags = state.tags.followedTags; - const authToken = state.user.accessToken; - - const newPayload = { - version: '0.1', - shared: { - subscriptions, - tags, - }, - }; - - if (!isEqual(newPayload, currentPayload)) { - currentPayload = newPayload; - if (authToken) { - Lbryio.call('user_settings', 'set', { settings: newPayload }); - } - } -}); - const persistor = persistStore(store); window.persistor = persistor; diff --git a/src/ui/util/saved-passwords.js b/src/ui/util/saved-passwords.js index 03d0f6a6e..74bec7a92 100644 --- a/src/ui/util/saved-passwords.js +++ b/src/ui/util/saved-passwords.js @@ -11,10 +11,16 @@ export const setSavedPassword = value => { export const getSavedPassword = () => { return new Promise(resolve => { + // @if TARGET='app' ipcRenderer.once('get-password-response', (event, password) => { resolve(password); }); ipcRenderer.send('get-password'); + // @endif + + // @if TARGET='web' + resolve(); + // @endif }); }; diff --git a/yarn.lock b/yarn.lock index 408c0b2d4..b37032108 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6862,17 +6862,17 @@ lazy-val@^1.0.3, lazy-val@^1.0.4: yargs "^13.2.2" zstd-codec "^0.1.1" -lbry-redux@lbryio/lbry-redux#0a1c95a08835a6b892d853b156d4934e469c9589: +lbry-redux@lbryio/lbry-redux#cd69946d4ca016c896639aa401d8f6b87d3e410e: version "0.0.1" - resolved "https://codeload.github.com/lbryio/lbry-redux/tar.gz/0a1c95a08835a6b892d853b156d4934e469c9589" + resolved "https://codeload.github.com/lbryio/lbry-redux/tar.gz/cd69946d4ca016c896639aa401d8f6b87d3e410e" dependencies: proxy-polyfill "0.1.6" reselect "^3.0.0" uuid "^3.3.2" -lbryinc@lbryio/lbryinc#d1dba98bb6f1dc67bc0db4c0a20fc13b8a0de98b: +lbryinc@lbryio/lbryinc#b8e1708ee4491db342c81576265e1b58f542bedb: version "0.0.1" - resolved "https://codeload.github.com/lbryio/lbryinc/tar.gz/d1dba98bb6f1dc67bc0db4c0a20fc13b8a0de98b" + resolved "https://codeload.github.com/lbryio/lbryinc/tar.gz/b8e1708ee4491db342c81576265e1b58f542bedb" dependencies: reselect "^3.0.0"