Skip to content

Commit

Permalink
refactor: [#276] extract fn Torrent::customize_announcement_info_for
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Sep 20, 2023
1 parent 4cc97c7 commit bbadd59
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
21 changes: 13 additions & 8 deletions src/models/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 8 additions & 3 deletions src/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bbadd59

Please sign in to comment.