2021-10-17 10:36:14 +02:00
|
|
|
// @flow
|
2020-10-02 17:03:25 +02:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2021-10-17 10:36:14 +02:00
|
|
|
import * as SETTINGS from 'constants/settings';
|
2020-10-02 17:03:25 +02:00
|
|
|
import { Lbryio } from 'lbryinc';
|
2021-10-17 10:36:14 +02:00
|
|
|
import Lbry from 'lbry';
|
|
|
|
import { doWalletEncrypt, doWalletDecrypt } from 'redux/actions/wallet';
|
2021-11-15 04:28:30 +01:00
|
|
|
import {
|
|
|
|
selectSyncHash,
|
|
|
|
selectGetSyncIsPending,
|
|
|
|
selectSetSyncIsPending,
|
|
|
|
selectSyncIsLocked,
|
|
|
|
} from 'redux/selectors/sync';
|
2020-10-05 20:31:51 +02:00
|
|
|
import { makeSelectClientSetting } from 'redux/selectors/settings';
|
2021-08-18 16:49:09 +02:00
|
|
|
import { getSavedPassword, getAuthToken } from 'util/saved-passwords';
|
2020-10-05 20:31:51 +02:00
|
|
|
import { doAnalyticsTagSync, doHandleSyncComplete } from 'redux/actions/app';
|
|
|
|
import { selectUserVerifiedEmail } from 'redux/selectors/user';
|
2021-08-18 16:49:09 +02:00
|
|
|
import { X_LBRY_AUTH_TOKEN } from 'constants/token';
|
2020-10-05 20:31:51 +02:00
|
|
|
|
|
|
|
let syncTimer = null;
|
|
|
|
const SYNC_INTERVAL = 1000 * 60 * 5; // 5 minutes
|
2020-10-27 15:05:59 +01:00
|
|
|
const NO_WALLET_ERROR = 'no wallet found for this user';
|
2020-11-12 20:47:39 +01:00
|
|
|
const BAD_PASSWORD_ERROR_NAME = 'InvalidPasswordError';
|
2020-10-02 17:03:25 +02:00
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
export function doSetDefaultAccount(success: () => void, failure: (string) => void) {
|
|
|
|
return (dispatch: Dispatch) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SET_DEFAULT_ACCOUNT,
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbry.account_list()
|
2021-03-24 03:53:33 +01:00
|
|
|
.then((accountList) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
const { lbc_mainnet: accounts } = accountList;
|
|
|
|
let defaultId;
|
|
|
|
for (let i = 0; i < accounts.length; ++i) {
|
|
|
|
if (accounts[i].satoshis > 0) {
|
|
|
|
defaultId = accounts[i].id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In a case where there's no balance on either account
|
|
|
|
// assume the second (which is created after sync) as default
|
|
|
|
if (!defaultId && accounts.length > 1) {
|
|
|
|
defaultId = accounts[1].id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the default account
|
|
|
|
if (defaultId) {
|
|
|
|
Lbry.account_set({ account_id: defaultId, default: true })
|
|
|
|
.then(() => {
|
|
|
|
if (success) {
|
|
|
|
success();
|
|
|
|
}
|
|
|
|
})
|
2021-03-24 03:53:33 +01:00
|
|
|
.catch((err) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
if (failure) {
|
|
|
|
failure(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (failure) {
|
|
|
|
// no default account to set
|
|
|
|
failure('Could not set a default account'); // fail
|
|
|
|
}
|
|
|
|
})
|
2021-03-24 03:53:33 +01:00
|
|
|
.catch((err) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
if (failure) {
|
|
|
|
failure(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
export function doSetSync(oldHash: string, newHash: string, data: any) {
|
|
|
|
return (dispatch: Dispatch) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SET_SYNC_STARTED,
|
|
|
|
});
|
|
|
|
|
|
|
|
return Lbryio.call('sync', 'set', { old_hash: oldHash, new_hash: newHash, data }, 'post')
|
2021-03-24 03:53:33 +01:00
|
|
|
.then((response) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
if (!response.hash) {
|
|
|
|
throw Error('No hash returned for sync/set.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return dispatch({
|
|
|
|
type: ACTIONS.SET_SYNC_COMPLETED,
|
|
|
|
data: { syncHash: response.hash },
|
|
|
|
});
|
|
|
|
})
|
2021-03-24 03:53:33 +01:00
|
|
|
.catch((error) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SET_SYNC_FAILED,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
export const doGetSyncDesktop = (cb?: (any, any) => void, password?: string) => (
|
|
|
|
dispatch: Dispatch,
|
|
|
|
getState: GetState
|
|
|
|
) => {
|
2020-10-05 20:31:51 +02:00
|
|
|
const state = getState();
|
|
|
|
const syncEnabled = makeSelectClientSetting(SETTINGS.ENABLE_SYNC)(state);
|
|
|
|
const getSyncPending = selectGetSyncIsPending(state);
|
|
|
|
const setSyncPending = selectSetSyncIsPending(state);
|
|
|
|
const syncLocked = selectSyncIsLocked(state);
|
|
|
|
|
2021-03-24 03:53:33 +01:00
|
|
|
return getSavedPassword().then((savedPassword) => {
|
2020-10-05 20:31:51 +02:00
|
|
|
const passwordArgument = password || password === '' ? password : savedPassword === null ? '' : savedPassword;
|
|
|
|
|
|
|
|
if (syncEnabled && !getSyncPending && !setSyncPending && !syncLocked) {
|
|
|
|
return dispatch(doGetSync(passwordArgument, cb));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
export function doSyncLoop(noInterval?: boolean) {
|
|
|
|
return (dispatch: Dispatch, getState: GetState) => {
|
2021-01-21 20:50:51 +01:00
|
|
|
if (!noInterval && syncTimer) clearInterval(syncTimer);
|
2020-10-05 20:31:51 +02:00
|
|
|
const state = getState();
|
|
|
|
const hasVerifiedEmail = selectUserVerifiedEmail(state);
|
|
|
|
const syncEnabled = makeSelectClientSetting(SETTINGS.ENABLE_SYNC)(state);
|
|
|
|
const syncLocked = selectSyncIsLocked(state);
|
|
|
|
if (hasVerifiedEmail && syncEnabled && !syncLocked) {
|
|
|
|
dispatch(doGetSyncDesktop((error, hasNewData) => dispatch(doHandleSyncComplete(error, hasNewData))));
|
2021-01-21 20:50:51 +01:00
|
|
|
if (!noInterval) {
|
|
|
|
syncTimer = setInterval(() => {
|
|
|
|
const state = getState();
|
|
|
|
const syncEnabled = makeSelectClientSetting(SETTINGS.ENABLE_SYNC)(state);
|
|
|
|
if (syncEnabled) {
|
|
|
|
dispatch(doGetSyncDesktop((error, hasNewData) => dispatch(doHandleSyncComplete(error, hasNewData))));
|
|
|
|
dispatch(doAnalyticsTagSync());
|
|
|
|
}
|
|
|
|
}, SYNC_INTERVAL);
|
|
|
|
}
|
2020-10-05 20:31:51 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doSyncUnsubscribe() {
|
2021-10-17 10:36:14 +02:00
|
|
|
return () => {
|
2020-10-05 20:31:51 +02:00
|
|
|
if (syncTimer) {
|
|
|
|
clearInterval(syncTimer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
export function doGetSync(passedPassword?: string, callback?: (any, ?boolean) => void) {
|
2020-10-02 17:03:25 +02:00
|
|
|
const password = passedPassword === null || passedPassword === undefined ? '' : passedPassword;
|
|
|
|
|
|
|
|
function handleCallback(error, hasNewData) {
|
|
|
|
if (callback) {
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw new Error('Second argument passed to "doGetSync" must be a function');
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(error, hasNewData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 16:49:09 +02:00
|
|
|
// @if TARGET='web'
|
|
|
|
const xAuth =
|
|
|
|
Lbry.getApiRequestHeaders() && Object.keys(Lbry.getApiRequestHeaders()).includes(X_LBRY_AUTH_TOKEN)
|
|
|
|
? Lbry.getApiRequestHeaders()[X_LBRY_AUTH_TOKEN]
|
|
|
|
: '';
|
|
|
|
if (xAuth && xAuth !== getAuthToken()) {
|
|
|
|
window.location.reload();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// @endif
|
|
|
|
|
2021-11-15 04:28:30 +01:00
|
|
|
return (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
const localHash = selectSyncHash(state);
|
|
|
|
|
2020-10-02 17:03:25 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.GET_SYNC_STARTED,
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = {};
|
|
|
|
|
|
|
|
Lbry.wallet_status()
|
2021-03-24 03:53:33 +01:00
|
|
|
.then((status) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
if (status.is_locked) {
|
|
|
|
return Lbry.wallet_unlock({ password });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wallet is already unlocked
|
|
|
|
return true;
|
|
|
|
})
|
2021-03-24 03:53:33 +01:00
|
|
|
.then((isUnlocked) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
if (isUnlocked) {
|
|
|
|
return Lbry.sync_hash();
|
|
|
|
}
|
|
|
|
data.unlockFailed = true;
|
|
|
|
throw new Error();
|
|
|
|
})
|
2021-10-17 10:36:14 +02:00
|
|
|
.then((hash?: string) => Lbryio.call('sync', 'get', { hash }, 'post'))
|
|
|
|
.then((response: any) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
const syncHash = response.hash;
|
|
|
|
data.syncHash = syncHash;
|
|
|
|
data.syncData = response.data;
|
2021-11-15 04:28:30 +01:00
|
|
|
data.changed = response.changed || syncHash !== localHash;
|
2020-10-02 17:03:25 +02:00
|
|
|
data.hasSyncedWallet = true;
|
|
|
|
|
|
|
|
if (response.changed) {
|
|
|
|
return Lbry.sync_apply({ password, data: response.data, blocking: true });
|
|
|
|
}
|
|
|
|
})
|
2021-03-24 03:53:33 +01:00
|
|
|
.then((response) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
if (!response) {
|
|
|
|
dispatch({ type: ACTIONS.GET_SYNC_COMPLETED, data });
|
|
|
|
handleCallback(null, data.changed);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { hash: walletHash, data: walletData } = response;
|
|
|
|
|
|
|
|
if (walletHash !== data.syncHash) {
|
|
|
|
// different local hash, need to synchronise
|
|
|
|
dispatch(doSetSync(data.syncHash, walletHash, walletData));
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({ type: ACTIONS.GET_SYNC_COMPLETED, data });
|
|
|
|
handleCallback(null, data.changed);
|
|
|
|
})
|
2021-03-24 03:53:33 +01:00
|
|
|
.catch((syncAttemptError) => {
|
2020-11-12 20:47:39 +01:00
|
|
|
const badPasswordError =
|
|
|
|
syncAttemptError && syncAttemptError.data && syncAttemptError.data.name === BAD_PASSWORD_ERROR_NAME;
|
|
|
|
|
2020-10-02 17:03:25 +02:00
|
|
|
if (data.unlockFailed) {
|
|
|
|
dispatch({ type: ACTIONS.GET_SYNC_FAILED, data: { error: syncAttemptError } });
|
|
|
|
|
2020-11-12 20:47:39 +01:00
|
|
|
if (badPasswordError) {
|
2020-10-02 17:03:25 +02:00
|
|
|
dispatch({ type: ACTIONS.SYNC_APPLY_BAD_PASSWORD });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCallback(syncAttemptError);
|
|
|
|
} else if (data.hasSyncedWallet) {
|
|
|
|
const error = (syncAttemptError && syncAttemptError.message) || 'Error getting synced wallet';
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.GET_SYNC_FAILED,
|
|
|
|
data: {
|
|
|
|
error,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-11-12 20:47:39 +01:00
|
|
|
if (badPasswordError) {
|
2020-10-02 17:03:25 +02:00
|
|
|
dispatch({ type: ACTIONS.SYNC_APPLY_BAD_PASSWORD });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCallback(error);
|
|
|
|
} else {
|
2020-11-12 18:38:28 +01:00
|
|
|
const noWalletError = syncAttemptError && syncAttemptError.message === NO_WALLET_ERROR;
|
|
|
|
|
2020-10-02 17:03:25 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.GET_SYNC_COMPLETED,
|
2020-11-12 18:38:28 +01:00
|
|
|
data: {
|
|
|
|
hasSyncedWallet: false,
|
|
|
|
syncHash: null,
|
|
|
|
// If there was some unknown error, bail
|
|
|
|
fatalError: !noWalletError,
|
|
|
|
},
|
2020-10-02 17:03:25 +02:00
|
|
|
});
|
|
|
|
|
2020-11-12 18:38:28 +01:00
|
|
|
// user doesn't have a synced wallet
|
|
|
|
// call sync_apply to get data to sync
|
|
|
|
// first time sync. use any string for old hash
|
|
|
|
if (noWalletError) {
|
2020-10-27 15:05:59 +01:00
|
|
|
Lbry.sync_apply({ password })
|
|
|
|
.then(({ hash: walletHash, data: syncApplyData }) => {
|
2021-10-17 10:36:14 +02:00
|
|
|
dispatch(doSetSync('', walletHash, syncApplyData));
|
2020-10-27 15:05:59 +01:00
|
|
|
handleCallback();
|
|
|
|
})
|
2021-03-24 03:53:33 +01:00
|
|
|
.catch((syncApplyError) => {
|
2020-10-27 15:05:59 +01:00
|
|
|
handleCallback(syncApplyError);
|
|
|
|
});
|
|
|
|
}
|
2020-10-02 17:03:25 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
export function doSyncApply(syncHash: string, syncData: any, password: string) {
|
|
|
|
return (dispatch: Dispatch) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SYNC_APPLY_STARTED,
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbry.sync_apply({ password, data: syncData })
|
|
|
|
.then(({ hash: walletHash, data: walletData }) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SYNC_APPLY_COMPLETED,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (walletHash !== syncHash) {
|
|
|
|
// different local hash, need to synchronise
|
|
|
|
dispatch(doSetSync(syncHash, walletHash, walletData));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SYNC_APPLY_FAILED,
|
|
|
|
data: {
|
|
|
|
error: 'Invalid password specified. Please enter the password for your previously synchronised wallet.',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doCheckSync() {
|
2021-10-17 10:36:14 +02:00
|
|
|
return (dispatch: Dispatch) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.GET_SYNC_STARTED,
|
|
|
|
});
|
|
|
|
|
2021-03-24 03:53:33 +01:00
|
|
|
Lbry.sync_hash().then((hash) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
Lbryio.call('sync', 'get', { hash }, 'post')
|
2021-03-24 03:53:33 +01:00
|
|
|
.then((response) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
const data = {
|
|
|
|
hasSyncedWallet: true,
|
|
|
|
syncHash: response.hash,
|
|
|
|
syncData: response.data,
|
|
|
|
hashChanged: response.changed,
|
|
|
|
};
|
|
|
|
dispatch({ type: ACTIONS.GET_SYNC_COMPLETED, data });
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
// user doesn't have a synced wallet
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.GET_SYNC_COMPLETED,
|
|
|
|
data: { hasSyncedWallet: false, syncHash: null },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doResetSync() {
|
2021-10-17 10:36:14 +02:00
|
|
|
return (dispatch: Dispatch): Promise<any> =>
|
2021-03-24 03:53:33 +01:00
|
|
|
new Promise((resolve) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
dispatch({ type: ACTIONS.SYNC_RESET });
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
export function doSyncEncryptAndDecrypt(oldPassword: string, newPassword: string, encrypt: boolean) {
|
|
|
|
return (dispatch: Dispatch) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
const data = {};
|
|
|
|
return Lbry.sync_hash()
|
2021-03-24 03:53:33 +01:00
|
|
|
.then((hash) => Lbryio.call('sync', 'get', { hash }, 'post'))
|
|
|
|
.then((syncGetResponse) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
data.oldHash = syncGetResponse.hash;
|
|
|
|
|
|
|
|
return Lbry.sync_apply({ password: oldPassword, data: syncGetResponse.data });
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
if (encrypt) {
|
|
|
|
dispatch(doWalletEncrypt(newPassword));
|
|
|
|
} else {
|
|
|
|
dispatch(doWalletDecrypt());
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => Lbry.sync_apply({ password: newPassword }))
|
2021-03-24 03:53:33 +01:00
|
|
|
.then((syncApplyResponse) => {
|
2020-10-02 17:03:25 +02:00
|
|
|
if (syncApplyResponse.hash !== data.oldHash) {
|
|
|
|
return dispatch(doSetSync(data.oldHash, syncApplyResponse.hash, syncApplyResponse.data));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(console.error); // eslint-disable-line
|
|
|
|
};
|
|
|
|
}
|
2020-10-05 20:31:51 +02:00
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
export function doSetSyncLock(lock: boolean) {
|
2020-10-05 20:31:51 +02:00
|
|
|
return {
|
|
|
|
type: ACTIONS.SET_SYNC_LOCK,
|
|
|
|
data: lock,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doSetPrefsReady() {
|
|
|
|
return {
|
|
|
|
type: ACTIONS.SET_PREFS_READY,
|
|
|
|
data: true,
|
|
|
|
};
|
|
|
|
}
|
2021-10-17 10:36:14 +02:00
|
|
|
|
|
|
|
type SharedData = {
|
|
|
|
version: '0.1',
|
|
|
|
value: {
|
|
|
|
subscriptions?: Array<string>,
|
|
|
|
following?: Array<{ uri: string, notificationsDisabled: boolean }>,
|
|
|
|
tags?: Array<string>,
|
|
|
|
blocked?: Array<string>,
|
|
|
|
coin_swap_codes?: Array<string>,
|
|
|
|
settings?: any,
|
|
|
|
app_welcome_version?: number,
|
|
|
|
sharing_3P?: boolean,
|
|
|
|
unpublishedCollections: CollectionGroup,
|
|
|
|
editedCollections: CollectionGroup,
|
|
|
|
builtinCollections: CollectionGroup,
|
|
|
|
savedCollections: Array<string>,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
function extractUserState(rawObj: SharedData) {
|
|
|
|
if (rawObj && rawObj.version === '0.1' && rawObj.value) {
|
|
|
|
const {
|
|
|
|
subscriptions,
|
|
|
|
following,
|
|
|
|
tags,
|
|
|
|
blocked,
|
|
|
|
coin_swap_codes,
|
|
|
|
settings,
|
|
|
|
app_welcome_version,
|
|
|
|
sharing_3P,
|
|
|
|
unpublishedCollections,
|
|
|
|
editedCollections,
|
|
|
|
builtinCollections,
|
|
|
|
savedCollections,
|
|
|
|
} = rawObj.value;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...(subscriptions ? { subscriptions } : {}),
|
|
|
|
...(following ? { following } : {}),
|
|
|
|
...(tags ? { tags } : {}),
|
|
|
|
...(blocked ? { blocked } : {}),
|
|
|
|
...(coin_swap_codes ? { coin_swap_codes } : {}),
|
|
|
|
...(settings ? { settings } : {}),
|
|
|
|
...(app_welcome_version ? { app_welcome_version } : {}),
|
|
|
|
...(sharing_3P ? { sharing_3P } : {}),
|
|
|
|
...(unpublishedCollections ? { unpublishedCollections } : {}),
|
|
|
|
...(editedCollections ? { editedCollections } : {}),
|
|
|
|
...(builtinCollections ? { builtinCollections } : {}),
|
|
|
|
...(savedCollections ? { savedCollections } : {}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doPopulateSharedUserState(sharedSettings: any) {
|
|
|
|
return (dispatch: Dispatch) => {
|
|
|
|
const {
|
|
|
|
subscriptions,
|
|
|
|
following,
|
|
|
|
tags,
|
|
|
|
blocked,
|
|
|
|
coin_swap_codes,
|
|
|
|
settings,
|
|
|
|
app_welcome_version,
|
|
|
|
sharing_3P,
|
|
|
|
unpublishedCollections,
|
|
|
|
editedCollections,
|
|
|
|
builtinCollections,
|
|
|
|
savedCollections,
|
|
|
|
} = extractUserState(sharedSettings);
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_STATE_POPULATE,
|
|
|
|
data: {
|
|
|
|
subscriptions,
|
|
|
|
following,
|
|
|
|
tags,
|
|
|
|
blocked,
|
|
|
|
coinSwapCodes: coin_swap_codes,
|
|
|
|
settings,
|
|
|
|
welcomeVersion: app_welcome_version,
|
|
|
|
allowAnalytics: sharing_3P,
|
|
|
|
unpublishedCollections,
|
|
|
|
editedCollections,
|
|
|
|
builtinCollections,
|
|
|
|
savedCollections,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doPreferenceSet(key: string, value: any, version: string, success: Function, fail: Function) {
|
|
|
|
return (dispatch: Dispatch) => {
|
|
|
|
const preference = {
|
|
|
|
type: typeof value,
|
|
|
|
version,
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
key,
|
|
|
|
value: JSON.stringify(preference),
|
|
|
|
};
|
|
|
|
|
|
|
|
Lbry.preference_set(options)
|
|
|
|
.then(() => {
|
|
|
|
if (success) {
|
|
|
|
success(preference);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SYNC_FATAL_ERROR,
|
|
|
|
error: err,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (fail) {
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doPreferenceGet(key: string, success: Function, fail?: Function) {
|
|
|
|
return (dispatch: Dispatch) => {
|
|
|
|
const options = {
|
|
|
|
key,
|
|
|
|
};
|
|
|
|
|
|
|
|
return Lbry.preference_get(options)
|
|
|
|
.then((result) => {
|
|
|
|
if (result) {
|
|
|
|
const preference = result[key];
|
|
|
|
return success(preference);
|
|
|
|
}
|
|
|
|
|
|
|
|
return success(null);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SYNC_FATAL_ERROR,
|
|
|
|
error: err,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (fail) {
|
|
|
|
fail(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|