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

406 lines
9.6 KiB
JavaScript
Raw Normal View History

import { execSync } from 'child_process';
import isDev from 'electron-is-dev';
import path from 'path';
import { ipcRenderer, remote } from 'electron';
2018-11-07 23:44:38 +01:00
import * as ACTIONS from 'constants/action_types';
2018-04-23 07:04:11 +02:00
import {
Lbry,
doBalanceSubscribe,
doFetchFileInfosAndPublishedClaims,
doNotify,
2018-04-23 20:02:06 +02:00
selectNotification,
MODALS,
doHideNotification,
2018-04-23 07:04:11 +02:00
} from 'lbry-redux';
2018-04-18 06:03:01 +02:00
import Native from 'native';
import { doFetchDaemonSettings } from 'redux/actions/settings';
2018-04-18 06:03:01 +02:00
import { doAuthNavigate } from 'redux/actions/navigation';
2018-08-30 05:28:53 +02:00
import { doCheckSubscriptionsInit } from 'redux/actions/subscriptions';
2017-04-07 07:15:22 +02:00
import {
selectIsUpgradeSkipped,
2017-04-07 07:15:22 +02:00
selectUpdateUrl,
selectUpgradeDownloadItem,
selectUpgradeDownloadPath,
2017-07-29 01:31:10 +02:00
selectUpgradeFilename,
selectAutoUpdateDeclined,
selectRemoteVersion,
selectUpgradeTimer,
} from 'redux/selectors/app';
import { doAuthenticate, doFetchRewardedContent } from 'lbryinc';
2018-09-24 05:44:42 +02:00
import { lbrySettings as config, version as appVersion } from 'package.json';
const { autoUpdater } = remote.require('electron-updater');
const { download } = remote.require('electron-dl');
const Fs = remote.require('fs');
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
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 (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 (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
});
dispatch(doHideNotification());
2018-04-23 20:02:06 +02:00
dispatch(
doNotify({
id: MODALS.DOWNLOADING,
})
2018-04-23 20:02:06 +02:00
);
2017-06-06 23:19:12 +02:00
};
2017-04-07 07:15:22 +02:00
}
export function doDownloadUpgradeRequested() {
// This means the user requested an upgrade by clicking the "upgrade" button in the navbar.
// If on Mac and Windows, we do some new behavior for the auto-update system.
// This will probably be reorganized once we get auto-update going on Linux and remove
// the old logic.
return (dispatch, getState) => {
const state = getState();
const autoUpdateDeclined = selectAutoUpdateDeclined(state);
if (['win32', 'darwin'].includes(process.platform)) {
// electron-updater behavior
if (autoUpdateDeclined) {
// The user declined an update before, so show the "confirm" dialog
2018-04-23 20:02:06 +02:00
dispatch(
doNotify({
id: MODALS.AUTO_UPDATE_CONFIRM,
})
2018-04-23 20:02:06 +02:00
);
} else {
// The user was never shown the original update dialog (e.g. because they were
// watching a video). So show the inital "update downloaded" dialog.
2018-04-23 20:02:06 +02:00
dispatch(
doNotify({
id: MODALS.AUTO_UPDATE_DOWNLOADED,
})
2018-04-23 20:02:06 +02:00
);
}
} else {
// Old behavior for Linux
dispatch(doDownloadUpgrade());
}
};
}
export function doClearUpgradeTimer() {
return (dispatch, getState) => {
const state = getState();
if (selectUpgradeTimer(state)) {
clearInterval(selectUpgradeTimer(state));
dispatch({
type: ACTIONS.CLEAR_UPGRADE_TIMER,
});
}
};
}
export function doAutoUpdate() {
return dispatch => {
dispatch({
type: ACTIONS.AUTO_UPDATE_DOWNLOADED,
});
2018-04-23 20:02:06 +02:00
dispatch(
doNotify({
id: MODALS.AUTO_UPDATE_DOWNLOADED,
})
2018-04-23 20:02:06 +02:00
);
dispatch(doClearUpgradeTimer());
};
}
export function doAutoUpdateDeclined() {
return dispatch => {
dispatch(doClearUpgradeTimer());
dispatch({
type: ACTIONS.AUTO_UPDATE_DECLINED,
});
2018-02-24 01:24:00 +01:00
};
}
2017-04-07 07:15:22 +02:00
export function doCancelUpgrade() {
return (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) {
// eslint-disable-next-line no-console
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 (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
if (['win32', 'darwin'].includes(process.platform)) {
// On Windows and Mac, updates happen silently through
// electron-updater.
const autoUpdateDeclined = selectAutoUpdateDeclined(state);
if (!autoUpdateDeclined && !isDev) {
autoUpdater.checkForUpdates();
}
return;
}
const success = ({ remoteVersion, upgradeAvailable }) => {
dispatch({
type: ACTIONS.CHECK_UPGRADE_SUCCESS,
data: {
upgradeAvailable,
remoteVersion,
},
});
if (
upgradeAvailable &&
2018-04-23 20:02:06 +02:00
!selectNotification(state) &&
(!selectIsUpgradeSkipped(state) || remoteVersion !== selectRemoteVersion(state))
) {
2018-04-23 20:02:06 +02:00
dispatch(
doNotify({
id: MODALS.UPGRADE,
})
2018-04-23 20:02:06 +02:00
);
}
};
const fail = () => {
dispatch({
type: ACTIONS.CHECK_UPGRADE_FAIL,
});
};
2018-04-18 06:03:01 +02:00
Native.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 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 dispatch => {
Lbry.version().then(({ lbrynet_version: lbrynetVersion }) => {
// Avoid the incompatible daemon modal if running in dev mode
2018-07-11 07:01:54 +02:00
// Lets you run a different daemon than the one specified in package.json
if (isDev || config.lbrynetDaemonVersion === lbrynetVersion) {
return dispatch({
2018-05-11 01:06:41 +02:00
type: ACTIONS.DAEMON_VERSION_MATCH,
});
}
dispatch({
2018-05-11 01:06:41 +02:00
type: ACTIONS.DAEMON_VERSION_MISMATCH,
});
return dispatch(
doNotify({
id: MODALS.INCOMPATIBLE_DAEMON,
})
2018-05-11 01:06:41 +02:00
);
});
};
}
2018-07-18 21:48:30 +02:00
export function doNotifyEncryptWallet() {
return dispatch => {
dispatch(
doNotify({
id: MODALS.WALLET_ENCRYPT,
})
2018-07-18 21:48:30 +02:00
);
};
}
export function doNotifyDecryptWallet() {
return dispatch => {
dispatch(
doNotify({
id: MODALS.WALLET_DECRYPT,
})
2018-07-18 21:48:30 +02:00
);
};
}
export function doNotifyUnlockWallet() {
return dispatch => {
dispatch(
doNotify({
id: MODALS.WALLET_UNLOCK,
})
2018-07-18 21:48:30 +02:00
);
};
}
2017-04-07 07:15:22 +02:00
export function doAlertError(errorList) {
return dispatch => {
2018-04-23 07:04:11 +02:00
dispatch(
doNotify({
id: MODALS.ERROR,
error: errorList,
})
2018-04-23 07:04:11 +02:00
);
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() {
return (dispatch, getState) => {
2017-11-16 22:39:10 +01:00
const state = getState();
2018-09-24 05:44:42 +02:00
dispatch(doAuthenticate(appVersion));
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());
dispatch(doCheckSubscriptionsInit());
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 doClearCache() {
return () => {
window.cacheStore.purge();
return Promise.resolve();
};
}
export function doQuit() {
return () => {
remote.app.quit();
};
}
export function doQuitAnyDaemon() {
return dispatch => {
try {
if (process.platform === 'win32') {
execSync('taskkill /im lbrynet-daemon.exe /t /f');
} else {
execSync('pkill lbrynet-daemon');
}
} catch (error) {
dispatch(doAlertError(`Quitting daemon failed due to: ${error.message}`));
} finally {
dispatch(doQuit());
}
};
}
export function doChangeVolume(volume) {
return dispatch => {
dispatch({
type: ACTIONS.VOLUME_CHANGED,
data: {
volume,
},
});
};
}
2017-12-23 03:09:06 +01:00
2018-11-07 23:44:38 +01:00
export function doClickCommentButton() {
return {
type: ACTIONS.ADD_COMMENT,
};
}
2017-12-23 03:09:06 +01:00
export function doConditionalAuthNavigate(newSession) {
return (dispatch, getState) => {
2017-12-23 03:09:06 +01:00
const state = getState();
2018-04-23 20:02:06 +02:00
const notification = selectNotification(state);
if (newSession || (notification && notification.id !== 'email_collection')) {
2017-12-23 03:09:06 +01:00
dispatch(doAuthNavigate());
}
};
}