From 93b15ac845b2450e19edddee34b698449fec9864 Mon Sep 17 00:00:00 2001 From: Mario Date: Mon, 22 Jul 2024 20:06:04 +0200 Subject: [PATCH] refactor: [#615] renamed variables to maybe_user_id --- src/services/about.rs | 8 ++--- src/services/authorization.rs | 4 +-- src/services/category.rs | 4 +-- src/services/proxy.rs | 8 ++--- src/services/settings.rs | 4 +-- src/services/tag.rs | 4 +-- src/services/torrent.rs | 26 ++++++++------- .../api/server/v1/contexts/about/handlers.rs | 8 ++--- .../server/v1/contexts/category/handlers.rs | 4 +-- .../api/server/v1/contexts/proxy/handlers.rs | 4 +-- .../server/v1/contexts/settings/handlers.rs | 4 +-- .../api/server/v1/contexts/tag/handlers.rs | 4 +-- .../server/v1/contexts/torrent/handlers.rs | 32 ++++++++++++------- 13 files changed, 62 insertions(+), 52 deletions(-) diff --git a/src/services/about.rs b/src/services/about.rs index 8b294bed..6daacf0b 100644 --- a/src/services/about.rs +++ b/src/services/about.rs @@ -24,9 +24,9 @@ impl Service { /// /// * The user does not have the required permissions. /// * There is an error authorizing the action. - pub async fn get_about_page(&self, opt_user_id: Option) -> Result { + pub async fn get_about_page(&self, maybe_user_id: Option) -> Result { self.authorization_service - .authorize(ACTION::GetAboutPage, opt_user_id) + .authorize(ACTION::GetAboutPage, maybe_user_id) .await?; let html = r#" @@ -58,9 +58,9 @@ impl Service { /// /// * The user does not have the required permissions. /// * There is an error authorizing the action. - pub async fn get_license_page(&self, opt_user_id: Option) -> Result { + pub async fn get_license_page(&self, maybe_user_id: Option) -> Result { self.authorization_service - .authorize(ACTION::GetLicensePage, opt_user_id) + .authorize(ACTION::GetLicensePage, maybe_user_id) .await?; let html = r#" diff --git a/src/services/authorization.rs b/src/services/authorization.rs index 7448cafd..29ff79ae 100644 --- a/src/services/authorization.rs +++ b/src/services/authorization.rs @@ -74,8 +74,8 @@ impl Service { /// Will return an error if: /// - The user is not authorized to perform the action. - pub async fn authorize(&self, action: ACTION, opt_user_id: Option) -> std::result::Result<(), ServiceError> { - let role = self.get_role(opt_user_id).await; + pub async fn authorize(&self, action: ACTION, maybe_user_id: Option) -> std::result::Result<(), ServiceError> { + let role = self.get_role(maybe_user_id).await; let enforcer = self.casbin_enforcer.enforcer.read().await; diff --git a/src/services/category.rs b/src/services/category.rs index 746e859b..8c6bc062 100644 --- a/src/services/category.rs +++ b/src/services/category.rs @@ -87,9 +87,9 @@ impl Service { /// /// * The user does not have the required permissions. /// * There is a database error retrieving the categories. - pub async fn get_categories(&self, opt_user_id: Option) -> Result, ServiceError> { + pub async fn get_categories(&self, maybe_user_id: Option) -> Result, ServiceError> { self.authorization_service - .authorize(ACTION::GetCategories, opt_user_id) + .authorize(ACTION::GetCategories, maybe_user_id) .await?; self.category_repository diff --git a/src/services/proxy.rs b/src/services/proxy.rs index dddf0afc..37517c5c 100644 --- a/src/services/proxy.rs +++ b/src/services/proxy.rs @@ -39,13 +39,13 @@ impl Service { /// * The image is too big. /// * The user quota is met. #[allow(clippy::missing_panics_doc)] - pub async fn get_image_by_url(&self, url: &str, opt_user_id: Option) -> Result { + pub async fn get_image_by_url(&self, url: &str, maybe_user_id: Option) -> Result { self.authorization_service - .authorize(ACTION::GetImageByUrl, opt_user_id) + .authorize(ACTION::GetImageByUrl, maybe_user_id) .await .map_err(|_| Error::Unauthenticated)?; - // The unwrap should never panic as if the opt_user_id is none, an authorization error will be returned and handled at the method above - self.image_cache_service.get_image_by_url(url, opt_user_id.unwrap()).await + // The unwrap should never panic as if the maybe_user_id is none, an authorization error will be returned and handled at the method above + self.image_cache_service.get_image_by_url(url, maybe_user_id.unwrap()).await } } diff --git a/src/services/settings.rs b/src/services/settings.rs index 69914313..565580ad 100644 --- a/src/services/settings.rs +++ b/src/services/settings.rs @@ -62,9 +62,9 @@ impl Service { /// # Errors /// /// It returns an error if the user does not have the required permissions. - pub async fn get_public(&self, opt_user_id: Option) -> Result { + pub async fn get_public(&self, maybe_user_id: Option) -> Result { self.authorization_service - .authorize(ACTION::GetPublicSettings, opt_user_id) + .authorize(ACTION::GetPublicSettings, maybe_user_id) .await?; let settings_lock = self.configuration.get_all().await; diff --git a/src/services/tag.rs b/src/services/tag.rs index ec2974a1..d47b09b8 100644 --- a/src/services/tag.rs +++ b/src/services/tag.rs @@ -77,8 +77,8 @@ impl Service { /// /// * The user does not have the required permissions. /// * There is a database error retrieving the tags. - pub async fn get_tags(&self, opt_user_id: Option) -> Result, ServiceError> { - self.authorization_service.authorize(ACTION::GetTags, opt_user_id).await?; + pub async fn get_tags(&self, maybe_user_id: Option) -> Result, ServiceError> { + self.authorization_service.authorize(ACTION::GetTags, maybe_user_id).await?; self.tag_repository.get_all().await.map_err(|_| ServiceError::DatabaseError) } diff --git a/src/services/torrent.rs b/src/services/torrent.rs index 5e118270..0e230246 100644 --- a/src/services/torrent.rs +++ b/src/services/torrent.rs @@ -262,8 +262,10 @@ impl Index { /// /// This function will return an error if unable to get the torrent from the /// database. - pub async fn get_torrent(&self, info_hash: &InfoHash, opt_user_id: Option) -> Result { - self.authorization_service.authorize(ACTION::GetTorrent, opt_user_id).await?; + pub async fn get_torrent(&self, info_hash: &InfoHash, maybe_user_id: Option) -> Result { + self.authorization_service + .authorize(ACTION::GetTorrent, maybe_user_id) + .await?; let mut torrent = self.torrent_repository.get_by_info_hash(info_hash).await?; @@ -275,7 +277,7 @@ impl Index { if !tracker_is_private { torrent.include_url_as_main_tracker(&tracker_url); - } else if let Some(authenticated_user_id) = opt_user_id { + } else if let Some(authenticated_user_id) = maybe_user_id { let personal_announce_url = self.tracker_service.get_personal_announce_url(authenticated_user_id).await?; torrent.include_url_as_main_tracker(&personal_announce_url); } else { @@ -331,16 +333,16 @@ impl Index { pub async fn get_torrent_info( &self, info_hash: &InfoHash, - opt_user_id: Option, + maybe_user_id: Option, ) -> Result { self.authorization_service - .authorize(ACTION::GetTorrentInfo, opt_user_id) + .authorize(ACTION::GetTorrentInfo, maybe_user_id) .await?; let torrent_listing = self.torrent_listing_generator.one_torrent_by_info_hash(info_hash).await?; let torrent_response = self - .build_full_torrent_response(torrent_listing, info_hash, opt_user_id) + .build_full_torrent_response(torrent_listing, info_hash, maybe_user_id) .await?; Ok(torrent_response) @@ -354,10 +356,10 @@ impl Index { pub async fn generate_torrent_info_listing( &self, request: &ListingRequest, - opt_user_id: Option, + maybe_user_id: Option, ) -> Result { self.authorization_service - .authorize(ACTION::GenerateTorrentInfoListing, opt_user_id) + .authorize(ACTION::GenerateTorrentInfoListing, maybe_user_id) .await?; let torrent_listing_specification = self.listing_specification_from_user_request(request).await; @@ -484,7 +486,7 @@ impl Index { &self, torrent_listing: TorrentListing, info_hash: &InfoHash, - opt_user_id: Option, + maybe_user_id: Option, ) -> Result { let torrent_id: i64 = torrent_listing.torrent_id; @@ -515,7 +517,7 @@ impl Index { if self.tracker_is_private().await { // Add main tracker URL - match opt_user_id { + match maybe_user_id { Some(user_id) => { let personal_announce_url = self.tracker_service.get_personal_announce_url(user_id).await?; @@ -568,10 +570,10 @@ impl Index { pub async fn get_canonical_info_hash( &self, info_hash: &InfoHash, - opt_user_id: Option, + maybe_user_id: Option, ) -> Result, ServiceError> { self.authorization_service - .authorize(ACTION::GetCanonicalInfoHash, opt_user_id) + .authorize(ACTION::GetCanonicalInfoHash, maybe_user_id) .await?; self.torrent_info_hash_repository diff --git a/src/web/api/server/v1/contexts/about/handlers.rs b/src/web/api/server/v1/contexts/about/handlers.rs index 89a35c81..530eae89 100644 --- a/src/web/api/server/v1/contexts/about/handlers.rs +++ b/src/web/api/server/v1/contexts/about/handlers.rs @@ -12,9 +12,9 @@ use crate::web::api::server::v1::extractors::optional_user_id::ExtractOptionalLo #[allow(clippy::unused_async)] pub async fn about_page_handler( State(app_data): State>, - ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser, + ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser, ) -> Response { - match app_data.about_service.get_about_page(opt_user_id).await { + match app_data.about_service.get_about_page(maybe_user_id).await { Ok(html) => (StatusCode::OK, [(header::CONTENT_TYPE, "text/html; charset=utf-8")], html).into_response(), Err(error) => error.into_response(), } @@ -23,9 +23,9 @@ pub async fn about_page_handler( #[allow(clippy::unused_async)] pub async fn license_page_handler( State(app_data): State>, - ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser, + ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser, ) -> Response { - match app_data.about_service.get_license_page(opt_user_id).await { + match app_data.about_service.get_license_page(maybe_user_id).await { Ok(html) => (StatusCode::OK, [(header::CONTENT_TYPE, "text/html; charset=utf-8")], html) .into_response() .into_response(), diff --git a/src/web/api/server/v1/contexts/category/handlers.rs b/src/web/api/server/v1/contexts/category/handlers.rs index 376305cb..7a3d9425 100644 --- a/src/web/api/server/v1/contexts/category/handlers.rs +++ b/src/web/api/server/v1/contexts/category/handlers.rs @@ -28,9 +28,9 @@ use crate::web::api::server::v1::responses::{self}; #[allow(clippy::unused_async)] pub async fn get_all_handler( State(app_data): State>, - ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser, + ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser, ) -> Response { - match app_data.category_service.get_categories(opt_user_id).await { + match app_data.category_service.get_categories(maybe_user_id).await { Ok(categories) => { let categories: Vec = categories.into_iter().map(Category::from).collect(); Json(responses::OkResponseData { data: categories }).into_response() diff --git a/src/web/api/server/v1/contexts/proxy/handlers.rs b/src/web/api/server/v1/contexts/proxy/handlers.rs index 9153cbfe..241fe3aa 100644 --- a/src/web/api/server/v1/contexts/proxy/handlers.rs +++ b/src/web/api/server/v1/contexts/proxy/handlers.rs @@ -14,7 +14,7 @@ use crate::web::api::server::v1::extractors::optional_user_id::ExtractOptionalLo #[allow(clippy::unused_async)] pub async fn get_proxy_image_handler( State(app_data): State>, - ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser, + ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser, Path(url): Path, ) -> Response { // code-review: Handling status codes in the index-gui other tan OK is quite a pain. @@ -27,7 +27,7 @@ pub async fn get_proxy_image_handler( // Get image URL from URL path parameter. let image_url = urlencoding::decode(&url).unwrap_or_default().into_owned(); - match app_data.proxy_service.get_image_by_url(&image_url, opt_user_id).await { + match app_data.proxy_service.get_image_by_url(&image_url, maybe_user_id).await { Ok(image_bytes) => { // Returns the cached image. png_image(image_bytes) diff --git a/src/web/api/server/v1/contexts/settings/handlers.rs b/src/web/api/server/v1/contexts/settings/handlers.rs index d4849835..7b3a0bd7 100644 --- a/src/web/api/server/v1/contexts/settings/handlers.rs +++ b/src/web/api/server/v1/contexts/settings/handlers.rs @@ -33,9 +33,9 @@ pub async fn get_all_handler( #[allow(clippy::unused_async)] pub async fn get_public_handler( State(app_data): State>, - ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser, + ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser, ) -> Response { - match app_data.settings_service.get_public(opt_user_id).await { + match app_data.settings_service.get_public(maybe_user_id).await { Ok(public_settings) => Json(responses::OkResponseData { data: public_settings }).into_response(), Err(error) => error.into_response(), } diff --git a/src/web/api/server/v1/contexts/tag/handlers.rs b/src/web/api/server/v1/contexts/tag/handlers.rs index b5ee637c..b198fc7a 100644 --- a/src/web/api/server/v1/contexts/tag/handlers.rs +++ b/src/web/api/server/v1/contexts/tag/handlers.rs @@ -28,9 +28,9 @@ use crate::web::api::server::v1::responses::{self}; #[allow(clippy::unused_async)] pub async fn get_all_handler( State(app_data): State>, - ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser, + ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser, ) -> Response { - match app_data.tag_service.get_tags(opt_user_id).await { + match app_data.tag_service.get_tags(maybe_user_id).await { Ok(tags) => Json(responses::OkResponseData { data: tags }).into_response(), Err(error) => error.into_response(), } diff --git a/src/web/api/server/v1/contexts/torrent/handlers.rs b/src/web/api/server/v1/contexts/torrent/handlers.rs index 243f1307..ffdc6240 100644 --- a/src/web/api/server/v1/contexts/torrent/handlers.rs +++ b/src/web/api/server/v1/contexts/torrent/handlers.rs @@ -68,7 +68,7 @@ impl InfoHashParam { #[allow(clippy::unused_async)] pub async fn download_torrent_handler( State(app_data): State>, - ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser, + ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser, Path(info_hash): Path, ) -> Response { let Ok(info_hash) = InfoHash::from_str(&info_hash.lowercase()) else { @@ -78,12 +78,12 @@ pub async fn download_torrent_handler( debug!("Downloading torrent: {:?}", info_hash.to_hex_string()); if let Some(redirect_response) = - redirect_to_download_url_using_canonical_info_hash_if_needed(&app_data, &info_hash, opt_user_id).await + redirect_to_download_url_using_canonical_info_hash_if_needed(&app_data, &info_hash, maybe_user_id).await { debug!("Redirecting to URL with canonical info-hash"); redirect_response } else { - let torrent = match app_data.torrent_service.get_torrent(&info_hash, opt_user_id).await { + let torrent = match app_data.torrent_service.get_torrent(&info_hash, maybe_user_id).await { Ok(torrent) => torrent, Err(error) => return error.into_response(), }; @@ -103,9 +103,13 @@ pub async fn download_torrent_handler( async fn redirect_to_download_url_using_canonical_info_hash_if_needed( app_data: &Arc, info_hash: &InfoHash, - opt_user_id: Option, + maybe_user_id: Option, ) -> Option { - match app_data.torrent_service.get_canonical_info_hash(info_hash, opt_user_id).await { + match app_data + .torrent_service + .get_canonical_info_hash(info_hash, maybe_user_id) + .await + { Ok(Some(canonical_info_hash)) => { if canonical_info_hash != *info_hash { return Some( @@ -134,11 +138,11 @@ async fn redirect_to_download_url_using_canonical_info_hash_if_needed( pub async fn get_torrents_handler( State(app_data): State>, Query(criteria): Query, - ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser, + ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser, ) -> Response { match app_data .torrent_service - .generate_torrent_info_listing(&criteria, opt_user_id) + .generate_torrent_info_listing(&criteria, maybe_user_id) .await { Ok(torrents_response) => Json(OkResponseData { data: torrents_response }).into_response(), @@ -157,7 +161,7 @@ pub async fn get_torrents_handler( #[allow(clippy::unused_async)] pub async fn get_torrent_info_handler( State(app_data): State>, - ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser, + ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser, Path(info_hash): Path, ) -> Response { let Ok(info_hash) = InfoHash::from_str(&info_hash.lowercase()) else { @@ -165,11 +169,11 @@ pub async fn get_torrent_info_handler( }; if let Some(redirect_response) = - redirect_to_details_url_using_canonical_info_hash_if_needed(&app_data, &info_hash, opt_user_id).await + redirect_to_details_url_using_canonical_info_hash_if_needed(&app_data, &info_hash, maybe_user_id).await { redirect_response } else { - match app_data.torrent_service.get_torrent_info(&info_hash, opt_user_id).await { + match app_data.torrent_service.get_torrent_info(&info_hash, maybe_user_id).await { Ok(torrent_response) => Json(OkResponseData { data: torrent_response }).into_response(), Err(error) => error.into_response(), } @@ -179,9 +183,13 @@ pub async fn get_torrent_info_handler( async fn redirect_to_details_url_using_canonical_info_hash_if_needed( app_data: &Arc, info_hash: &InfoHash, - opt_user_id: Option, + maybe_user_id: Option, ) -> Option { - match app_data.torrent_service.get_canonical_info_hash(info_hash, opt_user_id).await { + match app_data + .torrent_service + .get_canonical_info_hash(info_hash, maybe_user_id) + .await + { Ok(Some(canonical_info_hash)) => { if canonical_info_hash != *info_hash { return Some(