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

269 lines
6.4 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import * as types 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,
2017-11-13 22:02:23 +01:00
} from "redux/selectors/app";
import { doFetchDaemonSettings } from "redux/actions/settings";
import { doBalanceSubscribe } from "redux/actions/wallet";
import { doAuthenticate } from "redux/actions/user";
import { doFetchFileInfosAndPublishedClaims } from "redux/actions/file_info";
2017-10-03 23:52:53 +02:00
import * as modals from "constants/modal_types";
2017-11-21 21:09:20 +01:00
import { doFetchRewardedContent } from "redux/actions/content";
import { selectCurrentModal } from "redux/selectors/app";
2017-04-07 07:15:22 +02:00
2017-06-06 23:19:12 +02:00
const { remote, ipcRenderer, shell } = require("electron");
const path = require("path");
const { download } = remote.require("electron-dl");
const fs = remote.require("fs");
2017-11-24 15:31:05 +01:00
const { lbrySettings: config } = require("../../../../main/package.json");
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: types.OPEN_MODAL,
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: types.CLOSE_MODAL,
2017-06-06 23:19:12 +02:00
};
2017-04-07 07:15:22 +02:00
}
export function doUpdateDownloadProgress(percent) {
return {
type: types.UPGRADE_DOWNLOAD_PROGRESSED,
data: {
2017-06-06 23:19:12 +02:00
percent: percent,
},
};
2017-04-07 07:15:22 +02:00
}
export function doSkipUpgrade() {
return {
2017-06-06 23:19:12 +02:00
type: types.SKIP_UPGRADE,
};
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
2017-06-06 23:19:12 +02:00
ipcRenderer.send("upgrade", upgradeDownloadPath);
};
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(
2017-11-24 15:31:05 +01:00
remote.app.getPath("temp") + require("path").sep
),
2017-07-29 01:31:10 +02:00
upgradeFilename = selectUpgradeFilename(state);
2017-04-07 07:15:22 +02:00
let 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,
};
2017-11-24 15:31:05 +01:00
download(remote.getCurrentWindow(), selectUpdateUrl(state), options).then(
downloadItem => {
/**
2017-04-07 07:15:22 +02:00
* 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-11-24 15:31:05 +01:00
dispatch({
type: types.UPGRADE_DOWNLOAD_COMPLETED,
data: {
downloadItem,
path: path.join(dir, upgradeFilename),
},
});
}
);
2017-04-07 07:15:22 +02:00
dispatch({
2017-06-06 23:19:12 +02:00
type: types.UPGRADE_DOWNLOAD_STARTED,
});
2017-04-07 07:15:22 +02:00
dispatch({
type: types.OPEN_MODAL,
data: {
2017-10-03 23:52:53 +02:00
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
}
}
2017-06-06 23:19:12 +02:00
dispatch({ type: types.UPGRADE_CANCELLED });
};
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({
2017-11-16 22:39:10 +01:00
type: types.CHECK_UPGRADE_START,
});
2017-04-07 07:15:22 +02:00
const success = ({ remoteVersion, upgradeAvailable }) => {
dispatch({
2017-11-16 22:39:10 +01:00
type: types.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: types.OPEN_MODAL,
data: {
2017-10-03 23:52:53 +02:00
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({
2017-11-16 22:39:10 +01:00
type: types.CHECK_UPGRADE_FAIL,
});
};
2017-11-16 22:39:10 +01:00
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({
2017-11-16 22:39:10 +01:00
type: types.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, getState) {
lbry.version().then(({ lbrynet_version }) => {
dispatch({
2017-11-24 15:31:05 +01:00
type:
config.lbrynetDaemonVersion == lbrynet_version
? types.DAEMON_VERSION_MATCH
: types.DAEMON_VERSION_MISMATCH,
});
});
};
}
2017-04-07 07:15:22 +02:00
export function doAlertError(errorList) {
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
const state = getState();
2017-04-07 07:15:22 +02:00
dispatch({
type: types.OPEN_MODAL,
data: {
2017-10-03 23:52:53 +02:00
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: types.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: types.SHOW_SNACKBAR,
data,
2017-06-06 23:19:12 +02:00
};
2017-05-23 09:21:21 +02:00
}
export function doRemoveSnackBarSnack() {
return {
type: types.REMOVE_SNACKBAR_SNACK,
2017-06-06 23:19:12 +02:00
};
2017-05-23 09:21:21 +02:00
}
export function doClearCache() {
return function(dispatch, getState) {
window.cacheStore.purge();
return Promise.resolve();
};
}
export function doQuit() {
return function(dispatch, getState) {
remote.app.quit();
};
}
export function doChangeVolume(volume) {
return function(dispatch, getState) {
dispatch({
type: types.VOLUME_CHANGED,
data: {
volume,
},
});
};
}