lbry-desktop/src/renderer/redux/actions/app.js

274 lines
6.5 KiB
JavaScript
Raw Normal View History

import * as ACTIONS from 'constants/action_types';
import Lbry from 'lbry';
2017-04-07 07:15:22 +02:00
import {
selectUpdateUrl,
selectUpgradeDownloadPath,
2017-04-07 07:15:22 +02:00
selectUpgradeDownloadItem,
2017-07-29 01:31:10 +02:00
selectUpgradeFilename,
selectIsUpgradeSkipped,
selectRemoteVersion,
selectCurrentModal,
} from 'redux/selectors/app';
import { doFetchDaemonSettings } from 'redux/actions/settings';
2017-12-22 02:21:22 +01:00
import { doBalanceSubscribe } from 'redux/actions/wallet';
import { doAuthenticate } from 'redux/actions/user';
import { doFetchFileInfosAndPublishedClaims } from 'redux/actions/file_info';
import * as MODALS from 'constants/modal_types';
import { doFetchRewardedContent } from 'redux/actions/content';
2017-12-26 14:25:26 +01:00
import { doAuthNavigate } from 'redux/actions/navigation';
import { remote, ipcRenderer } from 'electron';
import Path from 'path';
const { download } = remote.require('electron-dl');
const Fs = remote.require('fs');
const { lbrySettings: config } = require('package.json');
2017-12-13 22:36:30 +01:00
2017-11-16 22:39:10 +01:00
const CHECK_UPGRADE_INTERVAL = 10 * 60 * 1000;
2017-04-07 07:15:22 +02:00
2017-09-08 05:15:05 +02:00
export function doOpenModal(modal, modalProps = {}) {
2017-04-07 07:15:22 +02:00
return {
type: ACTIONS.OPEN_MODAL,
2017-04-07 07:15:22 +02:00
data: {
2017-06-06 23:19:12 +02:00
modal,
2017-09-08 05:15:05 +02:00
modalProps,
2017-06-06 23:19:12 +02:00
},
};
2017-04-07 07:15:22 +02:00
}
export function doCloseModal() {
return {
type: ACTIONS.CLOSE_MODAL,
2017-06-06 23:19:12 +02:00
};
2017-04-07 07:15:22 +02:00
}
export function doUpdateDownloadProgress(percent) {
return {
type: ACTIONS.UPGRADE_DOWNLOAD_PROGRESSED,
2017-04-07 07:15:22 +02:00
data: {
2017-12-13 22:36:30 +01:00
percent,
2017-06-06 23:19:12 +02:00
},
};
2017-04-07 07:15:22 +02:00
}
export function doSkipUpgrade() {
return {
type: ACTIONS.SKIP_UPGRADE,
2017-06-06 23:19:12 +02:00
};
2017-04-07 07:15:22 +02:00
}
export function doStartUpgrade() {
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
const state = getState();
const upgradeDownloadPath = selectUpgradeDownloadPath(state);
2017-04-07 07:15:22 +02:00
ipcRenderer.send('upgrade', upgradeDownloadPath);
2017-06-06 23:19:12 +02:00
};
2017-04-07 07:15:22 +02:00
}
export function doDownloadUpgrade() {
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
const state = getState();
2017-04-07 07:15:22 +02:00
// Make a new directory within temp directory so the filename is guaranteed to be available
const dir = Fs.mkdtempSync(remote.app.getPath('temp') + Path.sep);
const upgradeFilename = selectUpgradeFilename(state);
2017-04-07 07:15:22 +02:00
2017-12-13 22:36:30 +01:00
const options = {
2017-06-06 23:19:12 +02:00
onProgress: p => dispatch(doUpdateDownloadProgress(Math.round(p * 100))),
2017-04-07 07:15:22 +02:00
directory: dir,
};
download(remote.getCurrentWindow(), selectUpdateUrl(state), options).then(downloadItem => {
/**
* TODO: get the download path directly from the download object. It should just be
* downloadItem.getSavePath(), but the copy on the main process is being garbage collected
* too soon.
*/
2017-04-07 07:15:22 +02:00
dispatch({
type: ACTIONS.UPGRADE_DOWNLOAD_COMPLETED,
data: {
downloadItem,
path: Path.join(dir, upgradeFilename),
},
});
});
2017-04-07 07:15:22 +02:00
dispatch({
type: ACTIONS.UPGRADE_DOWNLOAD_STARTED,
2017-06-06 23:19:12 +02:00
});
2017-04-07 07:15:22 +02:00
dispatch({
type: ACTIONS.OPEN_MODAL,
2017-04-07 07:15:22 +02:00
data: {
modal: MODALS.DOWNLOADING,
2017-06-06 23:19:12 +02:00
},
});
};
2017-04-07 07:15:22 +02:00
}
export function doCancelUpgrade() {
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
const state = getState();
const upgradeDownloadItem = selectUpgradeDownloadItem(state);
2017-04-07 07:15:22 +02:00
if (upgradeDownloadItem) {
/*
* Right now the remote reference to the download item gets garbage collected as soon as the
* the download is over (maybe even earlier), so trying to cancel a finished download may
* throw an error.
*/
try {
upgradeDownloadItem.cancel();
} catch (err) {
2017-06-06 23:19:12 +02:00
console.error(err);
2017-04-07 07:15:22 +02:00
// Do nothing
}
}
dispatch({ type: ACTIONS.UPGRADE_CANCELLED });
2017-06-06 23:19:12 +02:00
};
2017-04-07 07:15:22 +02:00
}
export function doCheckUpgradeAvailable() {
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
const state = getState();
dispatch({
type: ACTIONS.CHECK_UPGRADE_START,
});
2017-04-07 07:15:22 +02:00
const success = ({ remoteVersion, upgradeAvailable }) => {
dispatch({
type: ACTIONS.CHECK_UPGRADE_SUCCESS,
data: {
upgradeAvailable,
remoteVersion,
},
});
if (
upgradeAvailable &&
2017-11-16 22:39:10 +01:00
!selectCurrentModal(state) &&
(!selectIsUpgradeSkipped(state) || remoteVersion !== selectRemoteVersion(state))
) {
2017-04-07 07:15:22 +02:00
dispatch({
type: ACTIONS.OPEN_MODAL,
2017-04-07 07:15:22 +02:00
data: {
modal: MODALS.UPGRADE,
2017-06-06 23:19:12 +02:00
},
});
2017-05-04 05:44:08 +02:00
}
};
2017-11-16 22:39:10 +01:00
const fail = () => {
dispatch({
type: ACTIONS.CHECK_UPGRADE_FAIL,
});
};
Lbry.getAppVersionInfo().then(success, fail);
};
}
/*
Initiate a timer that will check for an app upgrade every 10 minutes.
*/
2017-11-16 22:39:10 +01:00
export function doCheckUpgradeSubscribe() {
return function(dispatch) {
const checkUpgradeTimer = setInterval(
() => dispatch(doCheckUpgradeAvailable()),
2017-11-16 22:39:10 +01:00
CHECK_UPGRADE_INTERVAL
);
dispatch({
type: ACTIONS.CHECK_UPGRADE_SUBSCRIBE,
data: { checkUpgradeTimer },
2017-04-07 07:15:22 +02:00
});
2017-06-06 23:19:12 +02:00
};
2017-04-07 07:15:22 +02:00
}
export function doCheckDaemonVersion() {
return function(dispatch) {
Lbry.version().then(({ lbrynet_version: lbrynetVersion }) => {
dispatch({
2017-11-24 15:31:05 +01:00
type:
config.lbrynetDaemonVersion === lbrynetVersion
? ACTIONS.DAEMON_VERSION_MATCH
: ACTIONS.DAEMON_VERSION_MISMATCH,
});
});
};
}
2017-04-07 07:15:22 +02:00
export function doAlertError(errorList) {
return function(dispatch) {
2017-04-07 07:15:22 +02:00
dispatch({
type: ACTIONS.OPEN_MODAL,
2017-04-07 07:15:22 +02:00
data: {
modal: MODALS.ERROR,
2017-09-08 05:15:05 +02:00
modalProps: { error: errorList },
2017-06-06 23:19:12 +02:00
},
});
};
2017-04-07 07:15:22 +02:00
}
2017-04-22 15:17:01 +02:00
export function doDaemonReady() {
2017-06-01 18:20:12 +02:00
return function(dispatch, getState) {
2017-11-16 22:39:10 +01:00
const state = getState();
2017-06-08 02:56:52 +02:00
dispatch(doAuthenticate());
dispatch({ type: ACTIONS.DAEMON_READY });
2017-06-08 02:56:52 +02:00
dispatch(doFetchDaemonSettings());
dispatch(doBalanceSubscribe());
2017-09-07 15:38:44 +02:00
dispatch(doFetchFileInfosAndPublishedClaims());
2017-11-16 22:39:10 +01:00
dispatch(doFetchRewardedContent());
if (!selectIsUpgradeSkipped(state)) {
dispatch(doCheckUpgradeAvailable());
}
dispatch(doCheckUpgradeSubscribe());
2017-06-08 02:56:52 +02:00
};
2017-04-22 15:17:01 +02:00
}
2017-05-23 09:21:21 +02:00
export function doShowSnackBar(data) {
return {
type: ACTIONS.SHOW_SNACKBAR,
2017-05-23 09:21:21 +02:00
data,
2017-06-06 23:19:12 +02:00
};
2017-05-23 09:21:21 +02:00
}
export function doRemoveSnackBarSnack() {
return {
type: ACTIONS.REMOVE_SNACKBAR_SNACK,
2017-06-06 23:19:12 +02:00
};
2017-05-23 09:21:21 +02:00
}
export function doClearCache() {
return function() {
window.cacheStore.purge();
return Promise.resolve();
};
}
export function doQuit() {
return function() {
remote.app.quit();
};
}
export function doChangeVolume(volume) {
return function(dispatch) {
dispatch({
type: ACTIONS.VOLUME_CHANGED,
data: {
volume,
},
});
};
}
2017-12-23 03:09:06 +01:00
export function doConditionalAuthNavigate(newSession) {
return function(dispatch, getState) {
const state = getState();
2017-12-26 14:25:26 +01:00
if (newSession || selectCurrentModal(state) !== 'email_collection') {
2017-12-23 03:09:06 +01:00
dispatch(doAuthNavigate());
}
};
}