lbry-desktop/ui/redux/actions/settings.js

476 lines
14 KiB
JavaScript
Raw Normal View History

import Lbry from 'lbry';
import { doWalletReconnect } from 'redux/actions/wallet';
import * as SETTINGS from 'constants/settings';
import * as DAEMON_SETTINGS from 'constants/daemon_settings';
import * as ACTIONS from 'constants/action_types';
import * as SHARED_PREFERENCES from 'constants/shared_preferences';
import { doToast } from 'redux/actions/notifications';
import analytics from 'analytics';
2019-11-05 19:54:58 +01:00
import SUPPORTED_LANGUAGES from 'constants/supported_languages';
2019-11-18 19:30:15 +01:00
import { launcher } from 'util/autoLaunch';
import { makeSelectClientSetting } from 'redux/selectors/settings';
2021-01-21 20:50:51 +01:00
import { doSyncLoop, doSyncUnsubscribe, doSetSyncLock } from 'redux/actions/sync';
import { doAlertWaitingForSync, doGetAndPopulatePreferences } from 'redux/actions/app';
import { selectPrefsReady } from 'redux/selectors/sync';
import { Lbryio } from 'lbryinc';
import { getDefaultLanguage } from 'util/default-languages';
import { getSubsetFromKeysArray } from 'util/sync-settings';
2020-09-04 17:02:30 +02:00
const { DEFAULT_LANGUAGE } = require('config');
const { SDK_SYNC_KEYS, CLIENT_SYNC_KEYS } = SHARED_PREFERENCES;
2018-01-14 10:14:15 +01:00
2019-11-18 19:30:15 +01:00
export const IS_MAC = process.platform === 'darwin';
2019-08-18 18:54:55 +02:00
const UPDATE_IS_NIGHT_INTERVAL = 5 * 60 * 1000;
2017-12-26 21:13:05 +01:00
export function doFetchDaemonSettings() {
return (dispatch) => {
Lbry.settings_get().then((settings) => {
analytics.toggleInternal(settings.share_usage_data);
dispatch({
type: ACTIONS.DAEMON_SETTINGS_RECEIVED,
data: {
2017-06-06 23:19:12 +02:00
settings,
},
});
});
};
}
export function doFindFFmpeg() {
return (dispatch) => {
dispatch({
type: ACTIONS.FINDING_FFMPEG_STARTED,
});
return Lbry.ffmpeg_find().then((done) => {
2020-03-26 17:28:48 +01:00
dispatch(doGetDaemonStatus());
dispatch({
type: ACTIONS.FINDING_FFMPEG_COMPLETED,
});
});
};
}
2019-12-12 21:18:13 +01:00
export function doGetDaemonStatus() {
return (dispatch) => {
return Lbry.status().then((status) => {
2019-12-12 21:18:13 +01:00
dispatch({
type: ACTIONS.DAEMON_STATUS_RECEIVED,
data: {
status,
},
});
return status;
});
2019-12-12 21:18:13 +01:00
};
}
2019-12-12 21:18:13 +01:00
export function doClearDaemonSetting(key) {
return (dispatch, getState) => {
const state = getState();
const ready = selectPrefsReady(state);
if (!ready && key !== DAEMON_SETTINGS.LBRYUM_SERVERS) {
return dispatch(doAlertWaitingForSync());
}
const clearKey = {
key,
};
// not if syncLocked
Lbry.settings_clear(clearKey).then((defaultSettings) => {
if (SDK_SYNC_KEYS.includes(key)) {
dispatch({
type: ACTIONS.SHARED_PREFERENCE_SET,
2019-12-13 17:13:49 +01:00
data: { key: key, value: null },
});
}
if (key === DAEMON_SETTINGS.LBRYUM_SERVERS) {
dispatch(doWalletReconnect(true));
}
});
Lbry.settings_get().then((settings) => {
analytics.toggleInternal(settings.share_usage_data);
dispatch({
type: ACTIONS.DAEMON_SETTINGS_RECEIVED,
data: {
settings,
},
});
});
};
}
// if doPopulate is applying settings, we don't want to cause a loop; doNotDispatch = true.
export function doSetDaemonSetting(key, value, doNotDispatch = false) {
return (dispatch, getState) => {
const state = getState();
const ready = selectPrefsReady(state);
if (!ready) {
return dispatch(doAlertWaitingForSync());
}
dispatch({
type: ACTIONS.SETTING_DAEMON_SETTINGS,
data: {
val: true,
},
});
const newSettings = {
key,
value: !value && value !== false ? null : value,
};
Lbry.settings_set(newSettings)
.then((newSetting) => {
if (SDK_SYNC_KEYS.includes(key) && !doNotDispatch) {
dispatch({
type: ACTIONS.SHARED_PREFERENCE_SET,
data: { key: key, value: newSetting[key] },
});
}
// hardcoding this in lieu of a better solution
if (key === DAEMON_SETTINGS.LBRYUM_SERVERS) {
dispatch(doWalletReconnect());
// todo: add sdk reloadsettings() (or it happens automagically?)
}
})
.then(() => {
dispatch(doFetchDaemonSettings());
})
.then(() => {
dispatch({
type: ACTIONS.SETTING_DAEMON_SETTINGS,
data: {
val: false,
},
});
})
.catch((e) => {
console.log('error setting or fetching daemon setting', e.message);
dispatch({
type: ACTIONS.SETTING_DAEMON_SETTINGS,
data: {
val: false,
},
});
});
2022-02-08 23:16:22 +01:00
};
}
export function doCleanBlobs(fetchAfter = true) {
2022-02-08 23:16:22 +01:00
return (dispatch) => {
return Lbry.blob_clean().then(() => {
if (fetchAfter) {
dispatch(doFetchDaemonSettings());
}
2022-02-25 21:01:14 +01:00
return 'done';
});
2017-06-06 23:19:12 +02:00
};
2017-06-06 06:21:55 +02:00
}
2017-06-28 09:12:01 +02:00
2019-12-12 21:18:13 +01:00
export function doSaveCustomWalletServers(servers) {
return {
2019-12-12 21:18:13 +01:00
type: ACTIONS.SAVE_CUSTOM_WALLET_SERVERS,
data: servers,
};
}
2020-09-04 17:02:30 +02:00
export function doSetClientSetting(key, value, pushPrefs) {
return (dispatch, getState) => {
const state = getState();
const ready = selectPrefsReady(state);
if (!ready && pushPrefs) {
return dispatch(doAlertWaitingForSync());
}
dispatch({
type: ACTIONS.CLIENT_SETTING_CHANGED,
data: {
key,
value,
},
});
2020-09-04 17:02:30 +02:00
if (pushPrefs) {
dispatch(doPushSettingsToPrefs());
}
};
}
2018-01-14 10:14:15 +01:00
export function doUpdateIsNight() {
return {
type: ACTIONS.UPDATE_IS_NIGHT,
};
}
export function doUpdateIsNightAsync() {
return (dispatch) => {
dispatch(doUpdateIsNight());
2019-03-05 05:46:57 +01:00
setInterval(() => dispatch(doUpdateIsNight()), UPDATE_IS_NIGHT_INTERVAL);
};
}
2019-08-18 18:54:55 +02:00
export function doSetDarkTime(value, options) {
const { fromTo, time } = options;
return (dispatch, getState) => {
const state = getState();
2019-09-17 20:49:03 +02:00
const darkModeTimes = state.settings.clientSettings[SETTINGS.DARK_MODE_TIMES];
2019-08-18 18:54:55 +02:00
const { hour, min } = darkModeTimes[fromTo];
const newHour = time === 'hour' ? value : hour;
const newMin = time === 'min' ? value : min;
const modifiedTimes = {
[fromTo]: {
hour: newHour,
min: newMin,
formattedTime: newHour + ':' + newMin,
},
};
const mergedTimes = { ...darkModeTimes, ...modifiedTimes };
2019-09-24 01:47:58 +02:00
dispatch(doSetClientSetting(SETTINGS.DARK_MODE_TIMES, mergedTimes));
2019-08-18 18:54:55 +02:00
dispatch(doUpdateIsNight());
};
}
2019-11-05 19:54:58 +01:00
export function doGetWalletSyncPreference() {
const SYNC_KEY = 'enable-sync';
return (dispatch) => {
return Lbry.preference_get({ key: SYNC_KEY }).then((result) => {
const enabled = result && result[SYNC_KEY];
if (enabled !== null) {
dispatch(doSetClientSetting(SETTINGS.ENABLE_SYNC, enabled));
}
return enabled;
});
};
}
export function doSetWalletSyncPreference(pref) {
const SYNC_KEY = 'enable-sync';
return (dispatch) => {
return Lbry.preference_set({ key: SYNC_KEY, value: pref }).then((result) => {
const enabled = result && result[SYNC_KEY];
if (enabled !== null) {
dispatch(doSetClientSetting(SETTINGS.ENABLE_SYNC, enabled));
}
return enabled;
});
};
}
2020-09-04 17:02:30 +02:00
export function doPushSettingsToPrefs() {
return (dispatch, getState) => {
const state = getState();
2022-01-07 19:03:29 +01:00
const {
settings: { clientSettings, sharedPreferences },
2022-01-07 19:03:29 +01:00
} = state;
const selectedClientSettings = getSubsetFromKeysArray(clientSettings, CLIENT_SYNC_KEYS);
const newSharedPreferences = { ...sharedPreferences, ...selectedClientSettings };
2020-09-04 17:02:30 +02:00
return new Promise((resolve, reject) => {
dispatch({
type: ACTIONS.SYNC_CLIENT_SETTINGS,
data: newSharedPreferences,
});
2020-09-04 17:02:30 +02:00
resolve();
});
};
}
export function doEnterSettingsPage() {
return async (dispatch, getState) => {
const state = getState();
const syncEnabled = makeSelectClientSetting(SETTINGS.ENABLE_SYNC)(state);
const hasVerifiedEmail = state.user && state.user.user && state.user.user.has_verified_email;
2022-01-07 20:02:33 +01:00
2020-09-04 17:02:30 +02:00
dispatch(doSyncUnsubscribe());
if (syncEnabled && hasVerifiedEmail) {
2021-01-21 20:50:51 +01:00
await dispatch(doSyncLoop(true));
2020-09-04 17:02:30 +02:00
} else {
await dispatch(doGetAndPopulatePreferences());
}
2020-09-04 17:02:30 +02:00
dispatch(doSetSyncLock(true));
};
}
export function doExitSettingsPage() {
2022-01-07 20:02:33 +01:00
return (dispatch) => {
2020-09-04 17:02:30 +02:00
dispatch(doSetSyncLock(false));
dispatch(doPushSettingsToPrefs());
2021-01-21 20:50:51 +01:00
// syncLoop is restarted in store.js sharedStateCB if necessary
};
}
export function doFetchLanguage(language) {
return (dispatch, getState) => {
const { settings } = getState();
if (settings.language !== language || (settings.loadedLanguages && !settings.loadedLanguages.includes(language))) {
// this should match the behavior/logic in index-web.html
fetch('https://lbry.com/i18n/get/lbry-desktop/app-strings/' + language + '.json')
.then((r) => r.json())
.then((j) => {
window.i18n_messages[language] = j;
dispatch({
type: ACTIONS.DOWNLOAD_LANGUAGE_SUCCESS,
data: {
language,
},
});
2020-06-18 17:41:37 +02:00
})
.catch((e) => {
2020-06-18 17:41:37 +02:00
dispatch({
type: ACTIONS.DOWNLOAD_LANGUAGE_FAILURE,
2020-06-18 17:41:37 +02:00
});
});
}
};
}
export function doSetHomepage(code) {
return (dispatch, getState) => {
let languageCode;
if (code === getDefaultLanguage()) {
languageCode = null;
} else {
languageCode = code;
}
dispatch(doSetClientSetting(SETTINGS.HOMEPAGE, languageCode));
};
}
2019-11-05 19:54:58 +01:00
export function doSetLanguage(language) {
2019-11-12 23:25:44 +01:00
return (dispatch, getState) => {
const { settings } = getState();
const { daemonSettings } = settings;
const { share_usage_data: shareSetting } = daemonSettings;
2022-01-07 20:02:33 +01:00
const isSharingData = shareSetting;
let languageSetting;
if (language === getDefaultLanguage()) {
languageSetting = null;
} else {
languageSetting = language;
}
if (
settings.language !== languageSetting ||
(settings.loadedLanguages && !settings.loadedLanguages.includes(language))
) {
2019-11-12 23:25:44 +01:00
// this should match the behavior/logic in index-web.html
fetch('https://lbry.com/i18n/get/lbry-desktop/app-strings/' + language + '.json')
.then((r) => r.json())
.then((j) => {
2019-11-12 23:25:44 +01:00
window.i18n_messages[language] = j;
dispatch({
type: ACTIONS.DOWNLOAD_LANGUAGE_SUCCESS,
2019-11-12 23:25:44 +01:00
data: {
language,
},
});
})
.then(() => {
// set on localStorage so it can be read outside of redux
window.localStorage.setItem(SETTINGS.LANGUAGE, language);
dispatch(doSetClientSetting(SETTINGS.LANGUAGE, languageSetting));
if (isSharingData) {
Lbryio.call('user', 'language', {
language: language,
});
}
2019-11-12 23:25:44 +01:00
})
.catch((e) => {
window.localStorage.setItem(SETTINGS.LANGUAGE, DEFAULT_LANGUAGE);
dispatch(doSetClientSetting(SETTINGS.LANGUAGE, DEFAULT_LANGUAGE));
2019-11-12 23:25:44 +01:00
const languageName = SUPPORTED_LANGUAGES[language] ? SUPPORTED_LANGUAGES[language] : language;
dispatch(
doToast({
message: __('Failed to load %language% translations.', { language: languageName }),
2019-11-14 00:54:41 +01:00
isError: true,
2019-11-12 23:25:44 +01:00
})
);
2019-11-08 21:51:42 +01:00
});
2019-11-12 23:25:44 +01:00
}
2019-11-05 19:54:58 +01:00
};
}
2019-11-18 19:30:15 +01:00
export function doSetAutoLaunch(value) {
return (dispatch, getState) => {
const state = getState();
const autoLaunch = makeSelectClientSetting(SETTINGS.AUTO_LAUNCH)(state);
if (IS_MAC || process.env.NODE_ENV !== 'production') {
2019-11-18 19:30:15 +01:00
return;
}
if (value === undefined) {
launcher.isEnabled().then((isEnabled) => {
2019-11-18 19:30:15 +01:00
if (isEnabled) {
if (!autoLaunch) {
launcher.disable().then(() => {
dispatch(doSetClientSetting(SETTINGS.AUTO_LAUNCH, false));
});
}
} else {
if (autoLaunch || autoLaunch === null || autoLaunch === undefined) {
launcher.enable().then(() => {
dispatch(doSetClientSetting(SETTINGS.AUTO_LAUNCH, true));
});
}
}
});
} else if (value === true) {
launcher.isEnabled().then(function (isEnabled) {
2019-11-18 19:30:15 +01:00
if (!isEnabled) {
launcher.enable().then(() => {
dispatch(doSetClientSetting(SETTINGS.AUTO_LAUNCH, true));
});
} else {
dispatch(doSetClientSetting(SETTINGS.AUTO_LAUNCH, true));
}
});
} else {
// value = false
launcher.isEnabled().then(function (isEnabled) {
2019-11-18 19:30:15 +01:00
if (isEnabled) {
launcher.disable().then(() => {
dispatch(doSetClientSetting(SETTINGS.AUTO_LAUNCH, false));
});
} else {
dispatch(doSetClientSetting(SETTINGS.AUTO_LAUNCH, false));
}
});
}
};
}
2020-08-13 18:57:00 +02:00
export function doSetAppToTrayWhenClosed(value) {
return (dispatch) => {
2020-08-13 18:57:00 +02:00
window.localStorage.setItem(SETTINGS.TO_TRAY_WHEN_CLOSED, value);
dispatch(doSetClientSetting(SETTINGS.TO_TRAY_WHEN_CLOSED, value));
};
}
2021-01-08 16:21:27 +01:00
export function toggleVideoTheaterMode() {
return (dispatch, getState) => {
const state = getState();
const videoTheaterMode = makeSelectClientSetting(SETTINGS.VIDEO_THEATER_MODE)(state);
dispatch(doSetClientSetting(SETTINGS.VIDEO_THEATER_MODE, !videoTheaterMode));
};
}
export function toggleAutoplayNext() {
return (dispatch, getState) => {
const state = getState();
const ready = selectPrefsReady(state);
const autoplayNext = makeSelectClientSetting(SETTINGS.AUTOPLAY_NEXT)(state);
dispatch(doSetClientSetting(SETTINGS.AUTOPLAY_NEXT, !autoplayNext, ready));
dispatch(
doToast({
message: autoplayNext ? __('Autoplay Next is off.') : __('Autoplay Next is on.'),
})
);
};
}