mirror of
https://github.com/zoriya/flood.git
synced 2026-06-04 19:36:42 +00:00
server: migrate client [get/set]Settings to clientGatewayService
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user