server: migrate client [get/set]Settings to clientGatewayService

This commit is contained in:
Jesse Chan
2020-10-09 13:24:23 +08:00
parent 9e637f6780
commit b2f6fdad21
14 changed files with 334 additions and 172 deletions
-27
View File
@@ -3,8 +3,6 @@
*/
import util from 'util';
import {clientSettingsMap} from '../../shared/constants/clientSettingsMap';
const getEnsuredArray = (item) => {
if (!util.isArray(item)) {
return [item];
@@ -90,31 +88,6 @@ class ClientRequest {
this.clientRequestManager.methodCall('system.multicall', [this.requests]).then(handleSuccess).catch(handleError);
}
fetchSettings(options) {
let {requestedSettings} = options;
if (requestedSettings == null) {
requestedSettings = Object.values(clientSettingsMap);
}
// Ensure client's response gets mapped to the correct requested keys.
if (options.setRequestedKeysArr) {
options.setRequestedKeysArr(requestedSettings);
}
requestedSettings.forEach((settingsKey) => {
this.requests.push(getMethodCall(settingsKey));
});
}
setSettings(options) {
const settings = getEnsuredArray(options.settings);
settings.forEach((setting) => {
this.requests.push(getMethodCall(`${clientSettingsMap[setting.id]}.set`, ['', setting.data]));
});
}
setTracker(options) {
const existingTrackerIndex = 0;
const {tracker} = options;
-74
View File
@@ -5,7 +5,6 @@ import {series} from 'async';
import tar from 'tar-stream';
import ClientRequest from './ClientRequest';
import {clientSettingsBiMap} from '../../shared/constants/clientSettingsMap';
import torrentFileUtil from '../util/torrentFileUtil';
const client = {
@@ -89,79 +88,6 @@ const client = {
return selectedFiles;
},
getSettings(user, services, options, callback) {
let requestedSettingsKeys = [];
const request = new ClientRequest(user, services);
const response = {};
const outboundTransformation = {
throttleGlobalDownMax: (apiResponse) => Number(apiResponse) / 1024,
throttleGlobalUpMax: (apiResponse) => Number(apiResponse) / 1024,
piecesMemoryMax: (apiResponse) => Number(apiResponse) / (1024 * 1024),
};
request.fetchSettings({
options,
setRequestedKeysArr: (requestedSettingsKeysArr) => {
requestedSettingsKeys = requestedSettingsKeysArr;
},
});
request.postProcess((data) => {
if (!data) {
return null;
}
data.forEach((datum, index) => {
let value = datum[0];
const settingsKey = clientSettingsBiMap[requestedSettingsKeys[index]];
if (outboundTransformation[settingsKey]) {
value = outboundTransformation[settingsKey](value);
}
response[settingsKey] = value;
});
return response;
});
request.onComplete(callback);
request.send();
},
setSettings(user, services, payloads, callback) {
const request = new ClientRequest(user, services);
if (payloads.length === 0) return callback({});
const inboundTransformations = new Map();
inboundTransformations
.set('throttleGlobalDownMax', (userInput) => ({
id: userInput.id,
data: Number(userInput.data) * 1024,
}))
.set('throttleGlobalUpMax', (userInput) => ({
id: userInput.id,
data: Number(userInput.data) * 1024,
}))
.set('piecesMemoryMax', (userInput) => ({
id: userInput.id,
data: (Number(userInput.data) * 1024 * 1024).toString(),
}));
const transformedPayloads = payloads.map((payload) => {
if (inboundTransformations.has(payload.id)) {
const inboundTransformation = inboundTransformations.get(payload.id);
return inboundTransformation(payload);
}
return payload;
});
request.setSettings({settings: transformedPayloads});
request.onComplete(callback);
request.send();
},
setSpeedLimits(user, services, data, callback) {
const request = new ClientRequest(user, services);