From 69276b5fa4f895bba85bae811a7ef7cae2da63a7 Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Tue, 29 Sep 2020 12:28:54 +0800 Subject: [PATCH] server: migrate checkHash to clientGatewayService --- .../src/javascript/actions/TorrentActions.ts | 9 ++-- .../torrent-list/TorrentListContextMenu.js | 4 +- server/models/client.js | 11 ---- server/routes/client.ts | 44 ++++++++++++++-- server/services/clientGatewayService.ts | 51 +++++++++++++------ server/services/torrentService.ts | 7 --- shared/types/Action.ts | 6 +++ 7 files changed, 90 insertions(+), 42 deletions(-) diff --git a/client/src/javascript/actions/TorrentActions.ts b/client/src/javascript/actions/TorrentActions.ts index ec2746ed..867c0d55 100644 --- a/client/src/javascript/actions/TorrentActions.ts +++ b/client/src/javascript/actions/TorrentActions.ts @@ -2,6 +2,7 @@ import axios from 'axios'; import type { AddTorrentByURLOptions, + CheckTorrentsOptions, DeleteTorrentsOptions, MoveTorrentsOptions, StartTorrentsOptions, @@ -91,9 +92,9 @@ const TorrentActions = { }, ), - checkHash: (hashes: Array) => + checkHash: (options: CheckTorrentsOptions) => axios - .post(`${baseURI}api/client/torrents/check-hash`, {hashes}) + .post(`${baseURI}api/client/torrents/check-hash`, options) .then((json) => json.data) .then( (data) => { @@ -101,7 +102,7 @@ const TorrentActions = { type: 'CLIENT_CHECK_HASH_SUCCESS', data: { data, - count: hashes.length, + count: options.hashes.length, }, }); }, @@ -110,7 +111,7 @@ const TorrentActions = { type: 'CLIENT_CHECK_HASH_ERROR', error: { error, - count: hashes.length, + count: options.hashes.length, }, }); }, diff --git a/client/src/javascript/components/torrent-list/TorrentListContextMenu.js b/client/src/javascript/components/torrent-list/TorrentListContextMenu.js index 736a254c..3e8cde5f 100644 --- a/client/src/javascript/components/torrent-list/TorrentListContextMenu.js +++ b/client/src/javascript/components/torrent-list/TorrentListContextMenu.js @@ -38,7 +38,9 @@ const handleItemClick = (action, event, torrent) => { const selectedTorrents = TorrentStore.getSelectedTorrents(); switch (action) { case 'check-hash': - TorrentActions.checkHash(selectedTorrents); + TorrentActions.checkHash({ + hashes: selectedTorrents, + }); break; case 'set-taxonomy': UIActions.displayModal({id: 'set-taxonomy'}); diff --git a/server/models/client.js b/server/models/client.js index de795d4a..91f1f4fa 100644 --- a/server/models/client.js +++ b/server/models/client.js @@ -85,17 +85,6 @@ const client = { settings.set(user, {id: 'startTorrentsOnLoad', data: start}); }, - checkHash(user, services, {hashes}, callback) { - const request = new ClientRequest(user, services); - - request.checkHash(hashes); - request.onComplete((response, error) => { - services.torrentService.fetchTorrentList(); - callback(response, error); - }); - request.send(); - }, - downloadFiles(user, services, hash, fileString, res) { try { const selectedTorrent = services.torrentService.getTorrent(hash); diff --git a/server/routes/client.ts b/server/routes/client.ts index 36492e70..dabcf741 100644 --- a/server/routes/client.ts +++ b/server/routes/client.ts @@ -1,7 +1,12 @@ import express from 'express'; import multer from 'multer'; -import type {DeleteTorrentsOptions, StartTorrentsOptions, StopTorrentsOptions} from '@shared/types/Action'; +import type { + CheckTorrentsOptions, + DeleteTorrentsOptions, + StartTorrentsOptions, + StopTorrentsOptions, +} from '@shared/types/Action'; import ajaxUtil from '../util/ajaxUtil'; import booleanCoerce from '../middleware/booleanCoerce'; @@ -84,6 +89,10 @@ router.post('/torrents/start', (req, res req.services?.clientGatewayService .startTorrents({hashes}) + .then((response) => { + req.services?.torrentService.fetchTorrentList(); + return response; + }) .then(callback) .catch((err) => { callback(null, err); @@ -105,14 +114,39 @@ router.post('/torrents/stop', (req, res) req.services?.clientGatewayService .stopTorrents({hashes}) + .then((response) => { + req.services?.torrentService.fetchTorrentList(); + return response; + }) .then(callback) .catch((err) => { callback(null, err); }); }); -router.post('/torrents/check-hash', (req, res) => { - client.checkHash(req.user, req.services, req.body, ajaxUtil.getResponseFn(res)); +/** + * POST /api/client/torrents/check-hash + * @summary Hash checks torrents. + * @tags Torrents + * @security AuthenticatedUser + * @param {CheckTorrentsOptions} request.body.required - options - application/json + * @return {object} 200 - success response - application/json + * @return {Error} 500 - failure response - application/json + */ +router.post('/torrents/check-hash', (req, res) => { + const {hashes} = req.body; + const callback = ajaxUtil.getResponseFn(res); + + req.services?.clientGatewayService + .checkTorrents({hashes}) + .then((response) => { + req.services?.torrentService.fetchTorrentList(); + return response; + }) + .then(callback) + .catch((err) => { + callback(null, err); + }); }); router.post('/torrents/move', (req, res) => { @@ -134,6 +168,10 @@ router.post('/torrents/delete', (req, r req.services?.clientGatewayService .removeTorrents({hashes, deleteData}) + .then((response) => { + req.services?.torrentService.fetchTorrentList(); + return response; + }) .then(callback) .catch((err) => { callback(null, err); diff --git a/server/services/clientGatewayService.ts b/server/services/clientGatewayService.ts index 6488d15f..b6a516cf 100644 --- a/server/services/clientGatewayService.ts +++ b/server/services/clientGatewayService.ts @@ -2,7 +2,12 @@ import path from 'path'; import fs from 'fs'; import type {Credentials} from '@shared/types/Auth'; -import type {DeleteTorrentsOptions, StartTorrentsOptions, StopTorrentsOptions} from '@shared/types/Action'; +import type { + CheckTorrentsOptions, + DeleteTorrentsOptions, + StartTorrentsOptions, + StopTorrentsOptions, +} from '@shared/types/Action'; import type {TorrentProperties, Torrents} from '@shared/types/Torrent'; import type {TransferSummary} from '@shared/types/TransferData'; @@ -14,7 +19,6 @@ import scgiUtil from '../util/scgiUtil'; import type {MultiMethodCalls} from './clientRequestManager'; interface ClientGatewayServiceEvents { - TORRENTS_REMOVED: () => void; CLIENT_CONNECTION_STATE_CHANGE: () => void; PROCESS_TORRENT_LIST_START: () => void; PROCESS_TORRENT_LIST_END: (processedTorrentList: {torrents: Torrents}) => void; @@ -68,6 +72,31 @@ class ClientGatewayService extends BaseService { this.torrentListReducers.push(reducer); } + /** + * Checks torrents + * + * @param {CheckTorrentsOptions} options - An object of options... + * @return {Promise} - Resolves with RPC call response or rejects with error. + */ + async checkTorrents({hashes}: CheckTorrentsOptions) { + if (this.services == null || this.services.clientRequestManager == null || this.services.torrentService == null) { + return Promise.reject(); + } + + const methodCalls = hashes.reduce((accumulator: MultiMethodCalls, hash) => { + accumulator.push({ + methodName: 'd.check_hash', + params: [hash], + }); + + return accumulator; + }, []); + + return this.services?.clientRequestManager + .methodCall('system.multicall', [methodCalls]) + .then(this.processClientRequestSuccess, this.processClientRequestError); + } + /** * Removes torrents from rTorrent's session. Optionally deletes data of torrents. * @@ -148,8 +177,6 @@ class ClientGatewayService extends BaseService { }); } - this.emit('TORRENTS_REMOVED'); - return response; }, this.processClientRequestError); } @@ -161,7 +188,7 @@ class ClientGatewayService extends BaseService { * @return {Promise} - Resolves with RPC call response or rejects with error. */ async startTorrents({hashes}: StartTorrentsOptions) { - if (this.services == null || this.services.clientRequestManager == null || this.services.torrentService == null) { + if (this.services == null || this.services.clientRequestManager == null) { return Promise.reject(); } @@ -181,11 +208,7 @@ class ClientGatewayService extends BaseService { return this.services.clientRequestManager .methodCall('system.multicall', [methodCalls]) - .then(this.processClientRequestSuccess, this.processClientRequestError) - .then((response) => { - this.services?.torrentService.fetchTorrentList(); - return response; - }); + .then(this.processClientRequestSuccess, this.processClientRequestError); } /** @@ -195,7 +218,7 @@ class ClientGatewayService extends BaseService { * @return {Promise} - Resolves with RPC call response or rejects with error. */ async stopTorrents({hashes}: StopTorrentsOptions) { - if (this.services == null || this.services.clientRequestManager == null || this.services.torrentService == null) { + if (this.services == null || this.services.clientRequestManager == null) { return Promise.reject(); } @@ -215,11 +238,7 @@ class ClientGatewayService extends BaseService { return this.services.clientRequestManager .methodCall('system.multicall', [methodCalls]) - .then(this.processClientRequestSuccess, this.processClientRequestError) - .then((response) => { - this.services?.torrentService.fetchTorrentList(); - return response; - }); + .then(this.processClientRequestSuccess, this.processClientRequestError); } /** diff --git a/server/services/torrentService.ts b/server/services/torrentService.ts index cb1f1424..ca3a048b 100644 --- a/server/services/torrentService.ts +++ b/server/services/torrentService.ts @@ -35,7 +35,6 @@ class TorrentService extends BaseService { this.fetchTorrentList = this.fetchTorrentList.bind(this); this.handleTorrentProcessed = this.handleTorrentProcessed.bind(this); - this.handleTorrentsRemoved = this.handleTorrentsRemoved.bind(this); this.handleFetchTorrentListSuccess = this.handleFetchTorrentListSuccess.bind(this); this.handleFetchTorrentListError = this.handleFetchTorrentListError.bind(this); @@ -63,8 +62,6 @@ class TorrentService extends BaseService { clientGatewayService.on('PROCESS_TORRENT', this.handleTorrentProcessed); - clientGatewayService.on('TORRENTS_REMOVED', this.handleTorrentsRemoved); - this.fetchTorrentList(); }; } @@ -237,10 +234,6 @@ class TorrentService extends BaseService { ]); } } - - handleTorrentsRemoved() { - this.fetchTorrentList(); - } } export default TorrentService; diff --git a/shared/types/Action.ts b/shared/types/Action.ts index fb608946..f22d745f 100644 --- a/shared/types/Action.ts +++ b/shared/types/Action.ts @@ -9,6 +9,12 @@ export interface AddTorrentByURLOptions { tags?: Array; } +// POST /api/client/torrents/check-hash +export interface CheckTorrentsOptions { + // An array of string representing hashes of torrents to be checked + hashes: Array; +} + // POST /api/client/torrents/delete export interface DeleteTorrentsOptions { // An array of string representing hashes of torrents to be removed