Skip to content

Commit 2b43dce

Browse files
authored
feat(rTorrent): enhance method call handling and improve error reporting (#953)
1 parent 2fc0036 commit 2b43dce

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ If you are using rakshasa/rtorrent>0.15.1 (upstream rtorrent with json-rpc suppo
2525
you will need to add these options to your config:
2626

2727
```ini
28-
method.redirect=load.throw,load.normal
29-
method.redirect=load.start_throw,load.start
3028
method.insert=d.down.sequential,value|const,0
3129
method.insert=d.down.sequential.set,value|const,0
3230
```

server/services/rTorrent/clientGatewayService.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,19 @@ import {
5757

5858
class RTorrentClientGatewayService extends ClientGatewayService {
5959
clientRequestManager = new ClientRequestManager(this.user.client as RTorrentConnectionSettings);
60-
availableMethodCalls = this.fetchAvailableMethodCalls(true);
60+
availableMethodCalls = this.fetchAvailableMethodCalls();
61+
62+
async getPreferredMethod(methods: string[]): Promise<string> {
63+
const {methodList} = await this.availableMethodCalls;
64+
65+
const matchedMethod = methods.find((method) => methodList.includes(method));
66+
67+
if (!matchedMethod) {
68+
throw new Error(`None of the requested methods are available: ${methods.join(', ')}`);
69+
}
70+
71+
return matchedMethod;
72+
}
6173

6274
async appendTorrentCommentCall(file: string, additionalCalls: string[]) {
6375
const comment = await getComment(Buffer.from(file, 'base64'));
@@ -106,11 +118,15 @@ class RTorrentClientGatewayService extends ClientGatewayService {
106118
const result: string[] = [];
107119

108120
if (this.clientRequestManager.isJSONCapable) {
121+
const methodName = await this.getPreferredMethod(
122+
start ? ['load.start_throw', 'load.start'] : ['load.throw', 'load.normal'],
123+
);
124+
109125
await this.clientRequestManager
110126
.methodCall('system.multicall', [
111127
await Promise.all(
112128
processedFiles.map(async (file) => ({
113-
methodName: start ? 'load.start' : 'load.normal',
129+
methodName,
114130
params: [
115131
'',
116132
`data:applications/x-bittorrent;base64,${file}`,
@@ -159,13 +175,9 @@ class RTorrentClientGatewayService extends ClientGatewayService {
159175
const result: string[] = [];
160176

161177
if (urls[0]) {
162-
const methodName = start ? 'load.start' : 'load.normal';
163-
164-
// if (this.clientRequestManager.isJSONCapable) {
165-
// methodName = start ? 'load.start_throw' : 'load.throw';
166-
// } else {
167-
// methodName = start ? 'load.start' : 'load.normal';
168-
// }
178+
const methodName = await this.getPreferredMethod(
179+
start ? ['load.start_throw', 'load.start'] : ['load.throw', 'load.normal'],
180+
);
169181

170182
await this.clientRequestManager
171183
.methodCall('system.multicall', [
@@ -789,6 +801,7 @@ class RTorrentClientGatewayService extends ClientGatewayService {
789801
}
790802

791803
async fetchAvailableMethodCalls(fallback = false): Promise<{
804+
methodList: string[];
792805
clientSetting: string[];
793806
torrentContent: string[];
794807
torrentList: string[];
@@ -828,6 +841,7 @@ class RTorrentClientGatewayService extends ClientGatewayService {
828841
: (methodCalls: Array<string>) => methodCalls;
829842

830843
return {
844+
methodList,
831845
clientSetting: getAvailableMethodCalls(getMethodCalls(clientSettingMethodCallConfigs)),
832846
torrentContent: getAvailableMethodCalls(getMethodCalls(torrentContentMethodCallConfigs)),
833847
torrentList: getAvailableMethodCalls(getMethodCalls(torrentListMethodCallConfigs)),

0 commit comments

Comments
 (0)