From c29155752601993c678c1c4ee41b2aadb8db762a Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 29 Nov 2022 18:48:18 +0000 Subject: [PATCH] fix: [#84] restore behavior. Update torrent stats after upload When a new torrent is uploaded we have to update the tracker torrent stats in the `torrust_torrent_tracker_stats` table. It was from the UI but not for the DB migration script becuase the frontend makes a request to get the torrent info after the upload and that endpint updates the torrent tracket stats. It must update the stats even if that endpoint is not called after uploading a new torrent. --- src/routes/torrent.rs | 6 ++++++ src/tracker.rs | 10 ++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/routes/torrent.rs b/src/routes/torrent.rs index 41074b4b..c58c5599 100644 --- a/src/routes/torrent.rs +++ b/src/routes/torrent.rs @@ -96,6 +96,12 @@ pub async fn upload_torrent(req: HttpRequest, payload: Multipart, app_data: WebA ) .await?; + // update torrent tracker stats + let _ = app_data + .tracker + .update_torrent_tracker_stats(torrent_id, &torrent_request.torrent.info_hash()) + .await; + // whitelist info hash on tracker if let Err(e) = app_data .tracker diff --git a/src/tracker.rs b/src/tracker.rs index 6b42dc7c..4ceae007 100644 --- a/src/tracker.rs +++ b/src/tracker.rs @@ -179,13 +179,19 @@ impl TrackerService { } pub async fn update_torrents(&self) -> Result<(), ServiceError> { - println!("Updating torrents.."); + println!("Updating torrents ..."); let torrents = self.database.get_all_torrents_compact().await?; for torrent in torrents { - let _ = self.get_torrent_info(torrent.torrent_id, &torrent.info_hash).await; + let _ = self + .update_torrent_tracker_stats(torrent.torrent_id, &torrent.info_hash) + .await; } Ok(()) } + + pub async fn update_torrent_tracker_stats(&self, torrent_id: i64, info_hash: &str) -> Result { + self.get_torrent_info(torrent_id, info_hash).await + } }