From bbadd59a4af9139cbe32f5b3465d94add5bfdd2c Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 20 Sep 2023 16:24:27 +0100 Subject: [PATCH] refactor: [#276] extract fn Torrent::customize_announcement_info_for --- src/models/torrent_file.rs | 21 +++++++++++++-------- src/services/torrent.rs | 11 ++++++++--- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/models/torrent_file.rs b/src/models/torrent_file.rs index 7045c147..98e57b92 100644 --- a/src/models/torrent_file.rs +++ b/src/models/torrent_file.rs @@ -4,7 +4,6 @@ use serde_bytes::ByteBuf; use sha1::{Digest, Sha1}; use super::info_hash::InfoHash; -use crate::config::Configuration; use crate::utils::hex::{from_bytes, into_bytes}; #[derive(PartialEq, Debug, Clone, Serialize, Deserialize)] @@ -101,19 +100,25 @@ impl Torrent { } } - /// Sets the announce url to the tracker url and removes all other trackers - /// if the torrent is private. - pub async fn set_announce_urls(&mut self, cfg: &Configuration) { - let settings = cfg.settings.read().await; + /// Sets the announce url to the tracker url. + pub fn set_announce_to(&mut self, tracker_url: &str) { + self.announce = Some(tracker_url.to_owned()); + } - self.announce = Some(settings.tracker.url.clone()); + /// Removes all other trackers if the torrent is private. + pub fn reset_announce_list_if_private(&mut self) { + if self.is_private() { + self.announce_list = None; + } + } - // if torrent is private, remove all other trackers + fn is_private(&self) -> bool { if let Some(private) = self.info.private { if private == 1 { - self.announce_list = None; + return true; } } + false } /// It calculates the info hash of the torrent file. diff --git a/src/services/torrent.rs b/src/services/torrent.rs index cb40de57..7d59320b 100644 --- a/src/services/torrent.rs +++ b/src/services/torrent.rs @@ -136,9 +136,7 @@ impl Index { let (mut torrent, original_info_hash) = decode_and_validate_torrent_file(&add_torrent_req.torrent_buffer)?; - // Customize the announce URLs with the linked tracker URL - // and remove others if the torrent is private. - torrent.set_announce_urls(&self.configuration).await; + self.customize_announcement_info_for(&mut torrent).await; let canonical_info_hash = torrent.canonical_info_hash(); @@ -233,6 +231,13 @@ impl Index { Ok(metadata) } + async fn customize_announcement_info_for(&self, torrent: &mut Torrent) { + let settings = self.configuration.settings.read().await; + let tracker_url = settings.tracker.url.clone(); + torrent.set_announce_to(&tracker_url); + torrent.reset_announce_list_if_private(); + } + /// Gets a torrent from the Index. /// /// # Errors