From 63b5a19f024d2ca4aba3d0919cb4f85db1b0c0cb Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Wed, 27 Mar 2024 22:22:57 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B2=20Mild=20refactor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/qbittorrent.service.ts | 43 +++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/services/qbittorrent.service.ts b/services/qbittorrent.service.ts index d227b87..aa6b0d7 100644 --- a/services/qbittorrent.service.ts +++ b/services/qbittorrent.service.ts @@ -167,10 +167,45 @@ export default class QBittorrentService extends Service { handler: async ( ctx: Context<{ infoHashes: { _id: string; infoHashes: string[] }[] }>, ) => { + const { infoHashes } = ctx.params; await this.broker.call("qbittorrent.loginWithStoredCredentials", {}); try { - return await this.meta.sync.maindata(1); + // Increment rid for each call + this.rid = typeof this.rid === "number" ? this.rid + 1 : 0; + const data = await this.meta.sync.maindata(this.rid); + const torrentDetails: any = []; + + infoHashes.forEach(({ _id, infoHashes }) => { + // Initialize an object to hold details for this _id + const details: any = []; + + infoHashes.forEach((hash) => { + // Assuming 'data.torrents[hash]' retrieves the details for the hash + const torrent = data.torrents[hash]; + if (torrent) { + details.push({ + torrent, + }); + } + }); + + // If you have details for this _id, add them to the main array + if (details.length > 0) { + torrentDetails.push({ + _id, + details, + }); + } + }); + // Update rid with the latest value if needed based on the response + // Assuming `data.rid` contains the latest rid from the server + if (data.rid !== undefined) { + this.rid = data.rid; + console.log(`rid is ${this.rid}`); + } + console.log(JSON.stringify(torrentDetails, null, 4)); + return torrentDetails; } catch (err) { this.logger.error(err); throw err; @@ -179,7 +214,11 @@ export default class QBittorrentService extends Service { }, }, methods: {}, - async started() {}, + async started() { + console.log(`Initializing rid...`); + this.rid = 0; + console.log(`rid is ${this.rid}`); + }, }); } }