Skip to content

Commit 93b15ac

Browse files
committed
refactor: [#615] renamed variables to maybe_user_id
1 parent e614e2f commit 93b15ac

File tree

13 files changed

+62
-52
lines changed

13 files changed

+62
-52
lines changed

src/services/about.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ impl Service {
2424
///
2525
/// * The user does not have the required permissions.
2626
/// * There is an error authorizing the action.
27-
pub async fn get_about_page(&self, opt_user_id: Option<UserId>) -> Result<String, ServiceError> {
27+
pub async fn get_about_page(&self, maybe_user_id: Option<UserId>) -> Result<String, ServiceError> {
2828
self.authorization_service
29-
.authorize(ACTION::GetAboutPage, opt_user_id)
29+
.authorize(ACTION::GetAboutPage, maybe_user_id)
3030
.await?;
3131

3232
let html = r#"
@@ -58,9 +58,9 @@ impl Service {
5858
///
5959
/// * The user does not have the required permissions.
6060
/// * There is an error authorizing the action.
61-
pub async fn get_license_page(&self, opt_user_id: Option<UserId>) -> Result<String, ServiceError> {
61+
pub async fn get_license_page(&self, maybe_user_id: Option<UserId>) -> Result<String, ServiceError> {
6262
self.authorization_service
63-
.authorize(ACTION::GetLicensePage, opt_user_id)
63+
.authorize(ACTION::GetLicensePage, maybe_user_id)
6464
.await?;
6565

6666
let html = r#"

src/services/authorization.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ impl Service {
7474
/// Will return an error if:
7575
/// - The user is not authorized to perform the action.
7676
77-
pub async fn authorize(&self, action: ACTION, opt_user_id: Option<UserId>) -> std::result::Result<(), ServiceError> {
78-
let role = self.get_role(opt_user_id).await;
77+
pub async fn authorize(&self, action: ACTION, maybe_user_id: Option<UserId>) -> std::result::Result<(), ServiceError> {
78+
let role = self.get_role(maybe_user_id).await;
7979

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

src/services/category.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ impl Service {
8787
///
8888
/// * The user does not have the required permissions.
8989
/// * There is a database error retrieving the categories.
90-
pub async fn get_categories(&self, opt_user_id: Option<UserId>) -> Result<Vec<Category>, ServiceError> {
90+
pub async fn get_categories(&self, maybe_user_id: Option<UserId>) -> Result<Vec<Category>, ServiceError> {
9191
self.authorization_service
92-
.authorize(ACTION::GetCategories, opt_user_id)
92+
.authorize(ACTION::GetCategories, maybe_user_id)
9393
.await?;
9494

9595
self.category_repository

src/services/proxy.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ impl Service {
3939
/// * The image is too big.
4040
/// * The user quota is met.
4141
#[allow(clippy::missing_panics_doc)]
42-
pub async fn get_image_by_url(&self, url: &str, opt_user_id: Option<UserId>) -> Result<Bytes, Error> {
42+
pub async fn get_image_by_url(&self, url: &str, maybe_user_id: Option<UserId>) -> Result<Bytes, Error> {
4343
self.authorization_service
44-
.authorize(ACTION::GetImageByUrl, opt_user_id)
44+
.authorize(ACTION::GetImageByUrl, maybe_user_id)
4545
.await
4646
.map_err(|_| Error::Unauthenticated)?;
4747

48-
// 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
49-
self.image_cache_service.get_image_by_url(url, opt_user_id.unwrap()).await
48+
// 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
49+
self.image_cache_service.get_image_by_url(url, maybe_user_id.unwrap()).await
5050
}
5151
}

src/services/settings.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ impl Service {
6262
/// # Errors
6363
///
6464
/// It returns an error if the user does not have the required permissions.
65-
pub async fn get_public(&self, opt_user_id: Option<UserId>) -> Result<ConfigurationPublic, ServiceError> {
65+
pub async fn get_public(&self, maybe_user_id: Option<UserId>) -> Result<ConfigurationPublic, ServiceError> {
6666
self.authorization_service
67-
.authorize(ACTION::GetPublicSettings, opt_user_id)
67+
.authorize(ACTION::GetPublicSettings, maybe_user_id)
6868
.await?;
6969

7070
let settings_lock = self.configuration.get_all().await;

src/services/tag.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ impl Service {
7777
///
7878
/// * The user does not have the required permissions.
7979
/// * There is a database error retrieving the tags.
80-
pub async fn get_tags(&self, opt_user_id: Option<UserId>) -> Result<Vec<TorrentTag>, ServiceError> {
81-
self.authorization_service.authorize(ACTION::GetTags, opt_user_id).await?;
80+
pub async fn get_tags(&self, maybe_user_id: Option<UserId>) -> Result<Vec<TorrentTag>, ServiceError> {
81+
self.authorization_service.authorize(ACTION::GetTags, maybe_user_id).await?;
8282

8383
self.tag_repository.get_all().await.map_err(|_| ServiceError::DatabaseError)
8484
}

src/services/torrent.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,10 @@ impl Index {
262262
///
263263
/// This function will return an error if unable to get the torrent from the
264264
/// database.
265-
pub async fn get_torrent(&self, info_hash: &InfoHash, opt_user_id: Option<UserId>) -> Result<Torrent, ServiceError> {
266-
self.authorization_service.authorize(ACTION::GetTorrent, opt_user_id).await?;
265+
pub async fn get_torrent(&self, info_hash: &InfoHash, maybe_user_id: Option<UserId>) -> Result<Torrent, ServiceError> {
266+
self.authorization_service
267+
.authorize(ACTION::GetTorrent, maybe_user_id)
268+
.await?;
267269

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

@@ -275,7 +277,7 @@ impl Index {
275277

276278
if !tracker_is_private {
277279
torrent.include_url_as_main_tracker(&tracker_url);
278-
} else if let Some(authenticated_user_id) = opt_user_id {
280+
} else if let Some(authenticated_user_id) = maybe_user_id {
279281
let personal_announce_url = self.tracker_service.get_personal_announce_url(authenticated_user_id).await?;
280282
torrent.include_url_as_main_tracker(&personal_announce_url);
281283
} else {
@@ -331,16 +333,16 @@ impl Index {
331333
pub async fn get_torrent_info(
332334
&self,
333335
info_hash: &InfoHash,
334-
opt_user_id: Option<UserId>,
336+
maybe_user_id: Option<UserId>,
335337
) -> Result<TorrentResponse, ServiceError> {
336338
self.authorization_service
337-
.authorize(ACTION::GetTorrentInfo, opt_user_id)
339+
.authorize(ACTION::GetTorrentInfo, maybe_user_id)
338340
.await?;
339341

340342
let torrent_listing = self.torrent_listing_generator.one_torrent_by_info_hash(info_hash).await?;
341343

342344
let torrent_response = self
343-
.build_full_torrent_response(torrent_listing, info_hash, opt_user_id)
345+
.build_full_torrent_response(torrent_listing, info_hash, maybe_user_id)
344346
.await?;
345347

346348
Ok(torrent_response)
@@ -354,10 +356,10 @@ impl Index {
354356
pub async fn generate_torrent_info_listing(
355357
&self,
356358
request: &ListingRequest,
357-
opt_user_id: Option<UserId>,
359+
maybe_user_id: Option<UserId>,
358360
) -> Result<TorrentsResponse, ServiceError> {
359361
self.authorization_service
360-
.authorize(ACTION::GenerateTorrentInfoListing, opt_user_id)
362+
.authorize(ACTION::GenerateTorrentInfoListing, maybe_user_id)
361363
.await?;
362364

363365
let torrent_listing_specification = self.listing_specification_from_user_request(request).await;
@@ -484,7 +486,7 @@ impl Index {
484486
&self,
485487
torrent_listing: TorrentListing,
486488
info_hash: &InfoHash,
487-
opt_user_id: Option<UserId>,
489+
maybe_user_id: Option<UserId>,
488490
) -> Result<TorrentResponse, ServiceError> {
489491
let torrent_id: i64 = torrent_listing.torrent_id;
490492

@@ -515,7 +517,7 @@ impl Index {
515517

516518
if self.tracker_is_private().await {
517519
// Add main tracker URL
518-
match opt_user_id {
520+
match maybe_user_id {
519521
Some(user_id) => {
520522
let personal_announce_url = self.tracker_service.get_personal_announce_url(user_id).await?;
521523

@@ -568,10 +570,10 @@ impl Index {
568570
pub async fn get_canonical_info_hash(
569571
&self,
570572
info_hash: &InfoHash,
571-
opt_user_id: Option<UserId>,
573+
maybe_user_id: Option<UserId>,
572574
) -> Result<Option<InfoHash>, ServiceError> {
573575
self.authorization_service
574-
.authorize(ACTION::GetCanonicalInfoHash, opt_user_id)
576+
.authorize(ACTION::GetCanonicalInfoHash, maybe_user_id)
575577
.await?;
576578

577579
self.torrent_info_hash_repository

src/web/api/server/v1/contexts/about/handlers.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use crate::web::api::server::v1::extractors::optional_user_id::ExtractOptionalLo
1212
#[allow(clippy::unused_async)]
1313
pub async fn about_page_handler(
1414
State(app_data): State<Arc<AppData>>,
15-
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
15+
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
1616
) -> Response {
17-
match app_data.about_service.get_about_page(opt_user_id).await {
17+
match app_data.about_service.get_about_page(maybe_user_id).await {
1818
Ok(html) => (StatusCode::OK, [(header::CONTENT_TYPE, "text/html; charset=utf-8")], html).into_response(),
1919
Err(error) => error.into_response(),
2020
}
@@ -23,9 +23,9 @@ pub async fn about_page_handler(
2323
#[allow(clippy::unused_async)]
2424
pub async fn license_page_handler(
2525
State(app_data): State<Arc<AppData>>,
26-
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
26+
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
2727
) -> Response {
28-
match app_data.about_service.get_license_page(opt_user_id).await {
28+
match app_data.about_service.get_license_page(maybe_user_id).await {
2929
Ok(html) => (StatusCode::OK, [(header::CONTENT_TYPE, "text/html; charset=utf-8")], html)
3030
.into_response()
3131
.into_response(),

src/web/api/server/v1/contexts/category/handlers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ use crate::web::api::server::v1::responses::{self};
2828
#[allow(clippy::unused_async)]
2929
pub async fn get_all_handler(
3030
State(app_data): State<Arc<AppData>>,
31-
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
31+
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
3232
) -> Response {
33-
match app_data.category_service.get_categories(opt_user_id).await {
33+
match app_data.category_service.get_categories(maybe_user_id).await {
3434
Ok(categories) => {
3535
let categories: Vec<Category> = categories.into_iter().map(Category::from).collect();
3636
Json(responses::OkResponseData { data: categories }).into_response()

src/web/api/server/v1/contexts/proxy/handlers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::web::api::server::v1::extractors::optional_user_id::ExtractOptionalLo
1414
#[allow(clippy::unused_async)]
1515
pub async fn get_proxy_image_handler(
1616
State(app_data): State<Arc<AppData>>,
17-
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
17+
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
1818
Path(url): Path<String>,
1919
) -> Response {
2020
// 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(
2727
// Get image URL from URL path parameter.
2828
let image_url = urlencoding::decode(&url).unwrap_or_default().into_owned();
2929

30-
match app_data.proxy_service.get_image_by_url(&image_url, opt_user_id).await {
30+
match app_data.proxy_service.get_image_by_url(&image_url, maybe_user_id).await {
3131
Ok(image_bytes) => {
3232
// Returns the cached image.
3333
png_image(image_bytes)

src/web/api/server/v1/contexts/settings/handlers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ pub async fn get_all_handler(
3333
#[allow(clippy::unused_async)]
3434
pub async fn get_public_handler(
3535
State(app_data): State<Arc<AppData>>,
36-
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
36+
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
3737
) -> Response {
38-
match app_data.settings_service.get_public(opt_user_id).await {
38+
match app_data.settings_service.get_public(maybe_user_id).await {
3939
Ok(public_settings) => Json(responses::OkResponseData { data: public_settings }).into_response(),
4040
Err(error) => error.into_response(),
4141
}

src/web/api/server/v1/contexts/tag/handlers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ use crate::web::api::server::v1::responses::{self};
2828
#[allow(clippy::unused_async)]
2929
pub async fn get_all_handler(
3030
State(app_data): State<Arc<AppData>>,
31-
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
31+
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
3232
) -> Response {
33-
match app_data.tag_service.get_tags(opt_user_id).await {
33+
match app_data.tag_service.get_tags(maybe_user_id).await {
3434
Ok(tags) => Json(responses::OkResponseData { data: tags }).into_response(),
3535
Err(error) => error.into_response(),
3636
}

src/web/api/server/v1/contexts/torrent/handlers.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl InfoHashParam {
6868
#[allow(clippy::unused_async)]
6969
pub async fn download_torrent_handler(
7070
State(app_data): State<Arc<AppData>>,
71-
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
71+
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
7272
Path(info_hash): Path<InfoHashParam>,
7373
) -> Response {
7474
let Ok(info_hash) = InfoHash::from_str(&info_hash.lowercase()) else {
@@ -78,12 +78,12 @@ pub async fn download_torrent_handler(
7878
debug!("Downloading torrent: {:?}", info_hash.to_hex_string());
7979

8080
if let Some(redirect_response) =
81-
redirect_to_download_url_using_canonical_info_hash_if_needed(&app_data, &info_hash, opt_user_id).await
81+
redirect_to_download_url_using_canonical_info_hash_if_needed(&app_data, &info_hash, maybe_user_id).await
8282
{
8383
debug!("Redirecting to URL with canonical info-hash");
8484
redirect_response
8585
} else {
86-
let torrent = match app_data.torrent_service.get_torrent(&info_hash, opt_user_id).await {
86+
let torrent = match app_data.torrent_service.get_torrent(&info_hash, maybe_user_id).await {
8787
Ok(torrent) => torrent,
8888
Err(error) => return error.into_response(),
8989
};
@@ -103,9 +103,13 @@ pub async fn download_torrent_handler(
103103
async fn redirect_to_download_url_using_canonical_info_hash_if_needed(
104104
app_data: &Arc<AppData>,
105105
info_hash: &InfoHash,
106-
opt_user_id: Option<i64>,
106+
maybe_user_id: Option<i64>,
107107
) -> Option<Response> {
108-
match app_data.torrent_service.get_canonical_info_hash(info_hash, opt_user_id).await {
108+
match app_data
109+
.torrent_service
110+
.get_canonical_info_hash(info_hash, maybe_user_id)
111+
.await
112+
{
109113
Ok(Some(canonical_info_hash)) => {
110114
if canonical_info_hash != *info_hash {
111115
return Some(
@@ -134,11 +138,11 @@ async fn redirect_to_download_url_using_canonical_info_hash_if_needed(
134138
pub async fn get_torrents_handler(
135139
State(app_data): State<Arc<AppData>>,
136140
Query(criteria): Query<ListingRequest>,
137-
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
141+
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
138142
) -> Response {
139143
match app_data
140144
.torrent_service
141-
.generate_torrent_info_listing(&criteria, opt_user_id)
145+
.generate_torrent_info_listing(&criteria, maybe_user_id)
142146
.await
143147
{
144148
Ok(torrents_response) => Json(OkResponseData { data: torrents_response }).into_response(),
@@ -157,19 +161,19 @@ pub async fn get_torrents_handler(
157161
#[allow(clippy::unused_async)]
158162
pub async fn get_torrent_info_handler(
159163
State(app_data): State<Arc<AppData>>,
160-
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
164+
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
161165
Path(info_hash): Path<InfoHashParam>,
162166
) -> Response {
163167
let Ok(info_hash) = InfoHash::from_str(&info_hash.lowercase()) else {
164168
return errors::Request::InvalidInfoHashParam.into_response();
165169
};
166170

167171
if let Some(redirect_response) =
168-
redirect_to_details_url_using_canonical_info_hash_if_needed(&app_data, &info_hash, opt_user_id).await
172+
redirect_to_details_url_using_canonical_info_hash_if_needed(&app_data, &info_hash, maybe_user_id).await
169173
{
170174
redirect_response
171175
} else {
172-
match app_data.torrent_service.get_torrent_info(&info_hash, opt_user_id).await {
176+
match app_data.torrent_service.get_torrent_info(&info_hash, maybe_user_id).await {
173177
Ok(torrent_response) => Json(OkResponseData { data: torrent_response }).into_response(),
174178
Err(error) => error.into_response(),
175179
}
@@ -179,9 +183,13 @@ pub async fn get_torrent_info_handler(
179183
async fn redirect_to_details_url_using_canonical_info_hash_if_needed(
180184
app_data: &Arc<AppData>,
181185
info_hash: &InfoHash,
182-
opt_user_id: Option<i64>,
186+
maybe_user_id: Option<i64>,
183187
) -> Option<Response> {
184-
match app_data.torrent_service.get_canonical_info_hash(info_hash, opt_user_id).await {
188+
match app_data
189+
.torrent_service
190+
.get_canonical_info_hash(info_hash, maybe_user_id)
191+
.await
192+
{
185193
Ok(Some(canonical_info_hash)) => {
186194
if canonical_info_hash != *info_hash {
187195
return Some(

0 commit comments

Comments
 (0)