Skip to content

Commit

Permalink
refactor: [#615] renamed variables to maybe_user_id
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed Aug 3, 2024
1 parent e614e2f commit 93b15ac
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 52 deletions.
8 changes: 4 additions & 4 deletions src/services/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserId>) -> Result<String, ServiceError> {
pub async fn get_about_page(&self, maybe_user_id: Option<UserId>) -> Result<String, ServiceError> {
self.authorization_service
.authorize(ACTION::GetAboutPage, opt_user_id)
.authorize(ACTION::GetAboutPage, maybe_user_id)
.await?;

let html = r#"
Expand Down Expand Up @@ -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<UserId>) -> Result<String, ServiceError> {
pub async fn get_license_page(&self, maybe_user_id: Option<UserId>) -> Result<String, ServiceError> {
self.authorization_service
.authorize(ACTION::GetLicensePage, opt_user_id)
.authorize(ACTION::GetLicensePage, maybe_user_id)
.await?;

let html = r#"
Expand Down
4 changes: 2 additions & 2 deletions src/services/authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserId>) -> std::result::Result<(), ServiceError> {
let role = self.get_role(opt_user_id).await;
pub async fn authorize(&self, action: ACTION, maybe_user_id: Option<UserId>) -> std::result::Result<(), ServiceError> {
let role = self.get_role(maybe_user_id).await;

let enforcer = self.casbin_enforcer.enforcer.read().await;

Expand Down
4 changes: 2 additions & 2 deletions src/services/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserId>) -> Result<Vec<Category>, ServiceError> {
pub async fn get_categories(&self, maybe_user_id: Option<UserId>) -> Result<Vec<Category>, ServiceError> {
self.authorization_service
.authorize(ACTION::GetCategories, opt_user_id)
.authorize(ACTION::GetCategories, maybe_user_id)
.await?;

self.category_repository
Expand Down
8 changes: 4 additions & 4 deletions src/services/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserId>) -> Result<Bytes, Error> {
pub async fn get_image_by_url(&self, url: &str, maybe_user_id: Option<UserId>) -> Result<Bytes, Error> {
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
}
}
4 changes: 2 additions & 2 deletions src/services/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserId>) -> Result<ConfigurationPublic, ServiceError> {
pub async fn get_public(&self, maybe_user_id: Option<UserId>) -> Result<ConfigurationPublic, ServiceError> {
self.authorization_service
.authorize(ACTION::GetPublicSettings, opt_user_id)
.authorize(ACTION::GetPublicSettings, maybe_user_id)
.await?;

let settings_lock = self.configuration.get_all().await;
Expand Down
4 changes: 2 additions & 2 deletions src/services/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserId>) -> Result<Vec<TorrentTag>, ServiceError> {
self.authorization_service.authorize(ACTION::GetTags, opt_user_id).await?;
pub async fn get_tags(&self, maybe_user_id: Option<UserId>) -> Result<Vec<TorrentTag>, ServiceError> {
self.authorization_service.authorize(ACTION::GetTags, maybe_user_id).await?;

self.tag_repository.get_all().await.map_err(|_| ServiceError::DatabaseError)
}
Expand Down
26 changes: 14 additions & 12 deletions src/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserId>) -> Result<Torrent, ServiceError> {
self.authorization_service.authorize(ACTION::GetTorrent, opt_user_id).await?;
pub async fn get_torrent(&self, info_hash: &InfoHash, maybe_user_id: Option<UserId>) -> Result<Torrent, ServiceError> {
self.authorization_service
.authorize(ACTION::GetTorrent, maybe_user_id)
.await?;

let mut torrent = self.torrent_repository.get_by_info_hash(info_hash).await?;

Expand All @@ -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 {
Expand Down Expand Up @@ -331,16 +333,16 @@ impl Index {
pub async fn get_torrent_info(
&self,
info_hash: &InfoHash,
opt_user_id: Option<UserId>,
maybe_user_id: Option<UserId>,
) -> Result<TorrentResponse, ServiceError> {
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)
Expand All @@ -354,10 +356,10 @@ impl Index {
pub async fn generate_torrent_info_listing(
&self,
request: &ListingRequest,
opt_user_id: Option<UserId>,
maybe_user_id: Option<UserId>,
) -> Result<TorrentsResponse, ServiceError> {
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;
Expand Down Expand Up @@ -484,7 +486,7 @@ impl Index {
&self,
torrent_listing: TorrentListing,
info_hash: &InfoHash,
opt_user_id: Option<UserId>,
maybe_user_id: Option<UserId>,
) -> Result<TorrentResponse, ServiceError> {
let torrent_id: i64 = torrent_listing.torrent_id;

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

Expand Down Expand Up @@ -568,10 +570,10 @@ impl Index {
pub async fn get_canonical_info_hash(
&self,
info_hash: &InfoHash,
opt_user_id: Option<UserId>,
maybe_user_id: Option<UserId>,
) -> Result<Option<InfoHash>, ServiceError> {
self.authorization_service
.authorize(ACTION::GetCanonicalInfoHash, opt_user_id)
.authorize(ACTION::GetCanonicalInfoHash, maybe_user_id)
.await?;

self.torrent_info_hash_repository
Expand Down
8 changes: 4 additions & 4 deletions src/web/api/server/v1/contexts/about/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<AppData>>,
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(),
}
Expand All @@ -23,9 +23,9 @@ pub async fn about_page_handler(
#[allow(clippy::unused_async)]
pub async fn license_page_handler(
State(app_data): State<Arc<AppData>>,
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(),
Expand Down
4 changes: 2 additions & 2 deletions src/web/api/server/v1/contexts/category/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<AppData>>,
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<Category> = categories.into_iter().map(Category::from).collect();
Json(responses::OkResponseData { data: categories }).into_response()
Expand Down
4 changes: 2 additions & 2 deletions src/web/api/server/v1/contexts/proxy/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<AppData>>,
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
Path(url): Path<String>,
) -> Response {
// code-review: Handling status codes in the index-gui other tan OK is quite a pain.
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/web/api/server/v1/contexts/settings/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ pub async fn get_all_handler(
#[allow(clippy::unused_async)]
pub async fn get_public_handler(
State(app_data): State<Arc<AppData>>,
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(),
}
Expand Down
4 changes: 2 additions & 2 deletions src/web/api/server/v1/contexts/tag/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<AppData>>,
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(),
}
Expand Down
32 changes: 20 additions & 12 deletions src/web/api/server/v1/contexts/torrent/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl InfoHashParam {
#[allow(clippy::unused_async)]
pub async fn download_torrent_handler(
State(app_data): State<Arc<AppData>>,
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
Path(info_hash): Path<InfoHashParam>,
) -> Response {
let Ok(info_hash) = InfoHash::from_str(&info_hash.lowercase()) else {
Expand All @@ -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(),
};
Expand All @@ -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<AppData>,
info_hash: &InfoHash,
opt_user_id: Option<i64>,
maybe_user_id: Option<i64>,
) -> Option<Response> {
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(
Expand Down Expand Up @@ -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<Arc<AppData>>,
Query(criteria): Query<ListingRequest>,
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(),
Expand All @@ -157,19 +161,19 @@ pub async fn get_torrents_handler(
#[allow(clippy::unused_async)]
pub async fn get_torrent_info_handler(
State(app_data): State<Arc<AppData>>,
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
Path(info_hash): Path<InfoHashParam>,
) -> Response {
let Ok(info_hash) = InfoHash::from_str(&info_hash.lowercase()) else {
return errors::Request::InvalidInfoHashParam.into_response();
};

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(),
}
Expand All @@ -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<AppData>,
info_hash: &InfoHash,
opt_user_id: Option<i64>,
maybe_user_id: Option<i64>,
) -> Option<Response> {
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(
Expand Down

0 comments on commit 93b15ac

Please sign in to comment.