2019-03-05 05:46:57 +01:00
|
|
|
// @if TARGET='app'
|
|
|
|
import fs from 'fs';
|
|
|
|
import http from 'http';
|
|
|
|
// @endif
|
2018-04-18 06:03:01 +02:00
|
|
|
import { Lbry, ACTIONS, SETTINGS } from 'lbry-redux';
|
2018-01-14 10:14:15 +01:00
|
|
|
import moment from 'moment';
|
2018-02-16 10:16:50 +01:00
|
|
|
import analytics from 'analytics';
|
2018-01-14 10:14:15 +01:00
|
|
|
|
2018-01-19 22:37:22 +01:00
|
|
|
const UPDATE_IS_NIGHT_INTERVAL = 10 * 60 * 1000;
|
2017-12-26 21:13:05 +01:00
|
|
|
|
2017-05-17 23:52:45 +02:00
|
|
|
export function doFetchDaemonSettings() {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-12-21 18:32:51 +01:00
|
|
|
Lbry.settings_get().then(settings => {
|
2018-02-16 10:16:50 +01:00
|
|
|
analytics.toggle(settings.share_usage_data);
|
2017-05-17 23:52:45 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.DAEMON_SETTINGS_RECEIVED,
|
2017-05-17 23:52:45 +02:00
|
|
|
data: {
|
2017-06-06 23:19:12 +02:00
|
|
|
settings,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2017-05-17 23:52:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doSetDaemonSetting(key, value) {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2019-02-12 18:26:50 +01:00
|
|
|
const newSettings = {
|
|
|
|
key,
|
|
|
|
value: !value && value !== false ? null : value,
|
|
|
|
};
|
2017-12-26 21:13:05 +01:00
|
|
|
Lbry.settings_set(newSettings).then(newSettings);
|
|
|
|
Lbry.settings_get().then(settings => {
|
2019-04-01 16:30:19 +02:00
|
|
|
analytics.toggle(settings.share_usage_data);
|
2017-05-17 23:52:45 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.DAEMON_SETTINGS_RECEIVED,
|
2017-05-17 23:52:45 +02:00
|
|
|
data: {
|
2017-12-26 21:13:05 +01:00
|
|
|
settings,
|
2017-06-06 23:19:12 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2017-06-06 06:21:55 +02:00
|
|
|
}
|
2017-06-28 09:12:01 +02:00
|
|
|
|
|
|
|
export function doSetClientSetting(key, value) {
|
|
|
|
return {
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.CLIENT_SETTING_CHANGED,
|
2017-06-28 09:12:01 +02:00
|
|
|
data: {
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2017-08-08 11:36:14 +02:00
|
|
|
|
2017-08-24 18:11:39 +02:00
|
|
|
export function doGetThemes() {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-12-21 18:32:51 +01:00
|
|
|
const themes = ['light', 'dark'];
|
|
|
|
dispatch(doSetClientSetting(SETTINGS.THEMES, themes));
|
2017-08-26 21:04:44 +02:00
|
|
|
};
|
2017-08-24 20:28:05 +02:00
|
|
|
}
|
2017-08-26 21:04:44 +02:00
|
|
|
|
2018-01-14 10:14:15 +01:00
|
|
|
export function doUpdateIsNight() {
|
2018-01-19 22:37:22 +01:00
|
|
|
const momentNow = moment();
|
2018-01-14 10:14:15 +01:00
|
|
|
return {
|
|
|
|
type: ACTIONS.UPDATE_IS_NIGHT,
|
2018-03-08 00:03:45 +01:00
|
|
|
data: {
|
|
|
|
isNight: (() => {
|
2018-02-27 02:22:25 +01:00
|
|
|
const startNightMoment = moment('21:00', 'HH:mm');
|
2018-01-30 00:19:46 +01:00
|
|
|
const endNightMoment = moment('8:00', 'HH:mm');
|
|
|
|
return !(momentNow.isAfter(endNightMoment) && momentNow.isBefore(startNightMoment));
|
2018-02-24 01:24:00 +01:00
|
|
|
})(),
|
2018-01-19 22:37:22 +01:00
|
|
|
},
|
2018-01-14 10:14:15 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-08 00:03:45 +01:00
|
|
|
export function doUpdateIsNightAsync() {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch(doUpdateIsNight());
|
2019-02-12 18:26:50 +01:00
|
|
|
|
2019-03-05 05:46:57 +01:00
|
|
|
setInterval(() => dispatch(doUpdateIsNight()), UPDATE_IS_NIGHT_INTERVAL);
|
2018-03-08 00:03:45 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-19 20:03:51 +02:00
|
|
|
export function doDownloadLanguage(langFile) {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2019-03-05 05:46:57 +01:00
|
|
|
const destinationPath = `${i18n.directory}/${langFile}`;
|
2017-12-21 18:32:51 +01:00
|
|
|
const language = langFile.replace('.json', '');
|
|
|
|
const errorHandler = () => {
|
2019-03-05 05:46:57 +01:00
|
|
|
fs.unlink(destinationPath, () => {}); // Delete the file async. (But we don't check the result)
|
2017-12-21 18:32:51 +01:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.DOWNLOAD_LANGUAGE_FAILED,
|
|
|
|
data: { language },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-03-05 05:46:57 +01:00
|
|
|
const req = http.get(
|
2017-08-19 20:03:51 +02:00
|
|
|
{
|
|
|
|
headers: {
|
2017-12-21 18:32:51 +01:00
|
|
|
'Content-Type': 'text/html',
|
2017-08-19 20:03:51 +02:00
|
|
|
},
|
2019-03-20 22:43:00 +01:00
|
|
|
host: 'i18n.lbry.com',
|
2017-08-19 20:03:51 +02:00
|
|
|
path: `/langs/${langFile}`,
|
|
|
|
},
|
|
|
|
response => {
|
|
|
|
if (response.statusCode === 200) {
|
2019-03-05 05:46:57 +01:00
|
|
|
const file = fs.createWriteStream(destinationPath);
|
2017-08-19 20:03:51 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
file.on('error', errorHandler);
|
|
|
|
file.on('finish', () => {
|
2017-08-08 11:36:14 +02:00
|
|
|
file.close();
|
|
|
|
|
|
|
|
// push to our local list
|
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.DOWNLOAD_LANGUAGE_SUCCEEDED,
|
2017-12-13 22:36:30 +01:00
|
|
|
data: { language },
|
2017-08-08 11:36:14 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
response.pipe(file);
|
2017-08-19 20:03:51 +02:00
|
|
|
} else {
|
2017-12-21 18:32:51 +01:00
|
|
|
errorHandler(new Error('Language request failed.'));
|
2017-08-08 11:36:14 +02:00
|
|
|
}
|
2017-08-19 20:03:51 +02:00
|
|
|
}
|
|
|
|
);
|
2017-08-08 11:36:14 +02:00
|
|
|
|
2017-12-13 22:36:30 +01:00
|
|
|
req.setTimeout(30000, () => {
|
2017-08-19 20:03:51 +02:00
|
|
|
req.abort();
|
2017-08-08 11:36:14 +02:00
|
|
|
});
|
2017-08-19 20:03:51 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
req.on('error', errorHandler);
|
2017-08-19 20:03:51 +02:00
|
|
|
|
|
|
|
req.end();
|
2017-08-08 11:36:14 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doDownloadLanguages() {
|
2017-12-28 00:48:11 +01:00
|
|
|
return () => {
|
2017-12-13 22:36:30 +01:00
|
|
|
// temporarily disable i18n so I can get a working build out -- Jeremy
|
2019-03-05 05:46:57 +01:00
|
|
|
// if (!fs.existsSync(i18n.directory)) {
|
|
|
|
// fs.mkdirSync(i18n.directory);
|
2017-12-21 18:32:51 +01:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// function checkStatus(response) {
|
|
|
|
// if (response.status >= 200 && response.status < 300) {
|
|
|
|
// return response;
|
|
|
|
// }
|
|
|
|
// throw new Error(
|
|
|
|
// __("The list of available languages could not be retrieved.")
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// function parseJSON(response) {
|
|
|
|
// return response.json();
|
|
|
|
// }
|
|
|
|
//
|
2019-03-20 22:43:00 +01:00
|
|
|
// return fetch("http://i18n.lbry.com")
|
2017-12-21 18:32:51 +01:00
|
|
|
// .then(checkStatus)
|
|
|
|
// .then(parseJSON)
|
|
|
|
// .then(files => {
|
|
|
|
// const actions = files.map(doDownloadLanguage);
|
|
|
|
// dispatch(batchActions(...actions));
|
|
|
|
// });
|
2017-08-19 20:03:51 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doChangeLanguage(language) {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-12-21 18:32:51 +01:00
|
|
|
dispatch(doSetClientSetting(SETTINGS.LANGUAGE, language));
|
2019-03-05 05:46:57 +01:00
|
|
|
i18n.setLocale(language);
|
2017-08-08 11:36:14 +02:00
|
|
|
};
|
|
|
|
}
|