Skip to content

Commit ed5c1ed

Browse files
committed
refactor: extract fn is_info_hash_whitelisted
1 parent 8af9834 commit ed5c1ed

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/databases/database.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ pub trait Database: Sync + Send {
5353
async fn add_key_to_keys(&self, auth_key: &AuthKey) -> Result<usize, Error>;
5454

5555
async fn remove_key_from_keys(&self, key: &str) -> Result<usize, Error>;
56+
57+
async fn is_info_hash_whitelisted(&self, info_hash: &InfoHash) -> Result<bool, Error> {
58+
if let Err(e) = self.get_info_hash_from_whitelist(&info_hash.to_owned().to_string()).await {
59+
if let Error::QueryReturnedNoRows = e {
60+
return Ok(false);
61+
} else {
62+
return Err(e);
63+
}
64+
}
65+
Ok(true)
66+
}
5667
}
5768

5869
#[derive(Debug, Display, PartialEq, Eq, Error)]

src/tracker/mod.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,12 @@ impl TorrentTracker {
108108

109109
/// It adds a torrent to the whitelist if it has not been whitelisted previously
110110
async fn add_torrent_to_database_whitelist(&self, info_hash: &InfoHash) -> Result<(), database::Error> {
111-
if let Err(e) = self
112-
.database
113-
.get_info_hash_from_whitelist(&info_hash.to_owned().to_string())
114-
.await
115-
{
116-
if let database::Error::QueryReturnedNoRows = e {
117-
self.database.add_info_hash_to_whitelist(*info_hash).await?;
118-
} else {
119-
eprintln!("{e}");
120-
return Err(e);
121-
}
111+
if self.database.is_info_hash_whitelisted(info_hash).await.unwrap() {
112+
return Ok(());
122113
}
114+
115+
self.database.add_info_hash_to_whitelist(*info_hash).await?;
116+
123117
Ok(())
124118
}
125119

0 commit comments

Comments
 (0)