Skip to content

Commit

Permalink
fix: thanks clippy, you're a chill guy
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleeym committed Jan 28, 2025
1 parent aa04bc4 commit 0f5e197
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 66 deletions.
15 changes: 9 additions & 6 deletions src/auth/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl GithubClient {
ApiError::InternalError
})?;

Ok(github_login_attempts::create(
github_login_attempts::create(
ip,
body.device_code,
body.interval,
Expand All @@ -104,7 +104,7 @@ impl GithubClient {
&body.user_code,
&mut *pool,
)
.await?)
.await
}

pub async fn poll_github(
Expand Down Expand Up @@ -184,10 +184,10 @@ impl GithubClient {
return Err(ApiError::InternalError);
}

Ok(resp.json::<GitHubFetchedUser>().await.map_err(|e| {
resp.json::<GitHubFetchedUser>().await.map_err(|e| {
log::error!("Failed to create GitHubFetchedUser: {}", e);
ApiError::InternalError
})?)
})
}

pub async fn get_installation(&self, token: &str) -> Result<GitHubFetchedUser, ApiError> {
Expand Down Expand Up @@ -222,15 +222,18 @@ impl GithubClient {
let repos = match body.get("repositories").and_then(|r| r.as_array()) {
None => {
return Err(ApiError::InternalError);
},
}
Some(r) => r,
};

if repos.len() != 1 {
return Err(ApiError::InternalError);
}

let owner = repos[0].get("owner").ok_or(ApiError::InternalError)?.clone();
let owner = repos[0]
.get("owner")
.ok_or(ApiError::InternalError)?
.clone();

serde_json::from_value(owner).map_err(|e| {
log::error!("Failed to create GitHubFetchedUser: {}", e);
Expand Down
6 changes: 3 additions & 3 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ pub async fn maybe_cli(data: &AppData) -> anyhow::Result<bool> {
}
JobCommand::CleanupDownloads => {
let mut conn = data.db().acquire().await?;
jobs::cleanup_downloads::cleanup_downloads(&mut *conn).await?;
jobs::cleanup_downloads::cleanup_downloads(&mut conn).await?;

Ok(true)
}
JobCommand::LogoutDeveloper { username } => {
let mut conn = data.db().acquire().await?;
jobs::logout_user::logout_user(&username, &mut *conn).await?;
jobs::logout_user::logout_user(&username, &mut conn).await?;

Ok(true)
}
JobCommand::CleanupTokens => {
let mut conn = data.db().acquire().await?;
jobs::token_cleanup::token_cleanup(&mut *conn).await?;
jobs::token_cleanup::token_cleanup(&mut conn).await?;

Ok(true)
}
Expand Down
32 changes: 16 additions & 16 deletions src/database/repository/developers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async fn insert_github(
username: &str,
conn: &mut PgConnection,
) -> Result<Developer, ApiError> {
Ok(sqlx::query_as!(
sqlx::query_as!(
Developer,
"INSERT INTO developers(username, display_name, github_user_id)
VALUES ($1, $1, $2)
Expand All @@ -140,11 +140,11 @@ async fn insert_github(
.map_err(|e| {
log::error!("Failed to insert developer: {}", e);
ApiError::DbError
})?)
})
}

pub async fn get_one(id: i32, conn: &mut PgConnection) -> Result<Option<Developer>, ApiError> {
Ok(sqlx::query_as!(
sqlx::query_as!(
Developer,
"SELECT
id,
Expand All @@ -162,14 +162,14 @@ pub async fn get_one(id: i32, conn: &mut PgConnection) -> Result<Option<Develope
.map_err(|e| {
log::error!("Failed to fetch developer {}: {}", id, e);
ApiError::DbError
})?)
})
}

pub async fn get_one_by_username(
username: &str,
conn: &mut PgConnection,
) -> Result<Option<Developer>, ApiError> {
Ok(sqlx::query_as!(
sqlx::query_as!(
Developer,
"SELECT
id,
Expand All @@ -187,14 +187,14 @@ pub async fn get_one_by_username(
.map_err(|e| {
log::error!("Failed to fetch developer {}: {}", username, e);
ApiError::DbError
})?)
})
}

pub async fn get_all_for_mod(
mod_id: &str,
conn: &mut PgConnection,
) -> Result<Vec<ModDeveloper>, ApiError> {
Ok(sqlx::query_as!(
sqlx::query_as!(
ModDeveloper,
"SELECT
dev.id,
Expand All @@ -211,7 +211,7 @@ pub async fn get_all_for_mod(
.map_err(|e| {
log::error!("Failed to fetch developers for mod {}: {}", mod_id, e);
ApiError::DbError
})?)
})
}

pub async fn get_all_for_mods(
Expand Down Expand Up @@ -323,7 +323,7 @@ pub async fn get_owner_for_mod(
mod_id: &str,
conn: &mut PgConnection,
) -> Result<Developer, ApiError> {
Ok(sqlx::query_as!(
sqlx::query_as!(
Developer,
"SELECT
dev.id,
Expand All @@ -349,7 +349,7 @@ pub async fn get_owner_for_mod(
log::error!("Failed to fetch owner for mod {}", mod_id);
ApiError::InternalError
}
})?)
})
}

pub async fn update_status(
Expand All @@ -358,7 +358,7 @@ pub async fn update_status(
admin: bool,
conn: &mut PgConnection,
) -> Result<Developer, ApiError> {
Ok(sqlx::query_as!(
sqlx::query_as!(
Developer,
"UPDATE developers
SET admin = $1,
Expand All @@ -380,15 +380,15 @@ pub async fn update_status(
.map_err(|e| {
log::error!("Failed to update developer {}: {}", dev_id, e);
ApiError::DbError
})?)
})
}

pub async fn update_profile(
dev_id: i32,
display_name: &str,
conn: &mut PgConnection,
) -> Result<Developer, ApiError> {
Ok(sqlx::query_as!(
sqlx::query_as!(
Developer,
"UPDATE developers
SET display_name = $1
Expand All @@ -408,15 +408,15 @@ pub async fn update_profile(
.map_err(|e| {
log::error!("Failed to update profile for {}: {}", dev_id, e);
ApiError::DbError
})?)
})
}

pub async fn find_by_refresh_token(
uuid: Uuid,
conn: &mut PgConnection,
) -> Result<Option<Developer>, ApiError> {
let hash = sha256::digest(uuid.to_string());
Ok(sqlx::query_as!(
sqlx::query_as!(
Developer,
"SELECT
d.id,
Expand All @@ -436,5 +436,5 @@ pub async fn find_by_refresh_token(
.map_err(|e| {
log::error!("Failed to search for developer by refresh token: {}", e);
ApiError::DbError
})?)
})
}
12 changes: 6 additions & 6 deletions src/database/repository/github_login_attempts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub async fn get_one_by_ip(
ip: IpNetwork,
conn: &mut PgConnection,
) -> Result<Option<StoredLoginAttempt>, ApiError> {
Ok(sqlx::query_as!(
sqlx::query_as!(
StoredLoginAttempt,
"SELECT
uid as uuid,
Expand All @@ -30,14 +30,14 @@ pub async fn get_one_by_ip(
.map_err(|e| {
log::error!("Failed to fetch existing login attempt: {}", e);
ApiError::DbError
})?)
})
}

pub async fn get_one_by_uuid(
uuid: Uuid,
pool: &mut PgConnection,
) -> Result<Option<StoredLoginAttempt>, ApiError> {
Ok(sqlx::query_as!(
sqlx::query_as!(
StoredLoginAttempt,
"SELECT
uid as uuid,
Expand All @@ -58,7 +58,7 @@ pub async fn get_one_by_uuid(
.map_err(|e| {
log::error!("Failed to fetch GitHub login attempt: {}", e);
ApiError::DbError
})?)
})
}

pub async fn create(
Expand All @@ -70,7 +70,7 @@ pub async fn create(
user_code: &str,
pool: &mut PgConnection,
) -> Result<StoredLoginAttempt, ApiError> {
Ok(sqlx::query_as!(
sqlx::query_as!(
StoredLoginAttempt,
"INSERT INTO github_login_attempts
(ip, device_code, interval, expires_in, challenge_uri, user_code) VALUES
Expand All @@ -97,7 +97,7 @@ pub async fn create(
.map_err(|e| {
log::error!("Failed to insert new GitHub login attempt: {}", e);
ApiError::DbError
})?)
})
}

pub async fn poll_now(uuid: Uuid, conn: &mut PgConnection) -> Result<(), ApiError> {
Expand Down
2 changes: 1 addition & 1 deletion src/database/repository/mod_downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub async fn create(
mod_version_id,
e
);
return ApiError::DbError;
ApiError::DbError
})?;

Ok(result.rows_affected() > 0)
Expand Down
3 changes: 1 addition & 2 deletions src/database/repository/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ pub async fn get_logo(id: &str, conn: &mut PgConnection) -> Result<Option<Vec<u8
log::error!("Failed to fetch mod logo for {}: {}", id, e);
ApiError::DbError
})?
.map(|optional| optional.image)
.flatten();
.and_then(|optional| optional.image);

// Empty vec is basically no image
if vec.as_ref().is_some_and(|v| v.is_empty()) {
Expand Down
6 changes: 3 additions & 3 deletions src/endpoints/auth/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ pub async fn start_github_web_login(data: web::Data<AppData>) -> Result<impl Res
Ok(web::Json(ApiResponse {
error: "".into(),
payload: format!(
"https://github.com/login/oauth/authorize?client_id={}&redirect_uri={}&scope=read:user&state={}",
"https://github.com/login/oauth/authorize?client_id={}&redirect_uri={}/login/github/callback&scope=read:user&state={}",
data.github().client_id(),
format!("{}/login/github/callback", data.front_url()),
secret.to_string()
data.front_url(),
secret
),
}))
}
Expand Down
8 changes: 4 additions & 4 deletions src/endpoints/mod_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ pub async fn create_version(
let approved_count = ModVersion::get_accepted_count(&json.id, &mut pool).await?;

if dev.verified && approved_count != 0 {
let owner = developers::get_owner_for_mod(&json.id, &mut *pool).await?;
let owner = developers::get_owner_for_mod(&json.id, &mut pool).await?;

NewModVersionAcceptedEvent {
id: json.id.clone(),
Expand All @@ -315,7 +315,7 @@ pub async fn create_version(
base_url: data.app_url().to_string(),
}
.to_discord_webhook()
.send(&data.webhook_url());
.send(data.webhook_url());
}
Ok(HttpResponse::NoContent())
}
Expand Down Expand Up @@ -400,7 +400,7 @@ pub async fn update_version(
base_url: data.app_url().to_string(),
}
.to_discord_webhook()
.send(&data.webhook_url());
.send(data.webhook_url());
} else {
NewModVersionAcceptedEvent {
id: version.mod_id,
Expand All @@ -411,7 +411,7 @@ pub async fn update_version(
base_url: data.app_url().to_string(),
}
.to_discord_webhook()
.send(&data.webhook_url());
.send(data.webhook_url());
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/extractors/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Auth {

match self.developer.as_ref().is_some_and(|dev| dev.admin) {
false => Err(ApiError::Forbidden),
true => Ok(())
true => Ok(()),
}
}
}
Expand Down Expand Up @@ -105,17 +105,14 @@ impl FromRequest for Auth {

fn parse_token(map: &HeaderMap) -> Option<Uuid> {
map.get("Authorization")
.map(|header| header.to_str().ok())
.flatten()
.map(|str| -> Option<&str> {
.and_then(|header| header.to_str().ok())
.and_then(|str| -> Option<&str> {
let split = str.split(' ').collect::<Vec<&str>>();
if split.len() != 2 || split[0] != "Bearer" {
None
} else {
Some(split[1])
}
})
.flatten()
.map(|str| Uuid::try_parse(str).ok())
.flatten()
.and_then(|str| Uuid::try_parse(str).ok())
}
Loading

0 comments on commit 0f5e197

Please sign in to comment.