Update POST me/friend endpoint to take username instead of UUID #31

Merged
radical merged 4 commits from friends-endpoint-username into main 2025-07-13 14:22:32 +00:00
2 changed files with 22 additions and 2 deletions
Showing only changes of commit d775723b7b - Show all commits

View file

@ -8,7 +8,7 @@ use crate::{
api::v1::auth::check_access_token,
error::Error,
objects::Me,
utils::{get_auth_header, global_checks, user_uuid_from_identifier},
utils::{get_auth_header, global_checks, user_uuid_from_username}
};
/// Returns a list of users that are your friends
@ -73,7 +73,7 @@ pub async fn post(
let me = Me::get(&mut conn, uuid).await?;
let target_uuid = user_uuid_from_identifier(&mut conn, &json.username).await?;
let target_uuid = user_uuid_from_username(&mut conn, &json.username).await?;
me.add_friend(&mut conn, target_uuid).await?;
Ok(HttpResponse::Ok().finish())

View file

@ -168,6 +168,26 @@ pub async fn user_uuid_from_identifier(
}
}
pub async fn user_uuid_from_username(
conn: &mut Conn,
identifier: &String,
) -> Result<Uuid, Error> {
if USERNAME_REGEX.is_match(identifier) {
use users::dsl;
let user_uuid = dsl::users
.filter(dsl::username.eq(identifier))
.select(dsl::uuid)
.get_result(conn)
.await?;
Ok(user_uuid)
} else {
Err(Error::BadRequest(
"Please provide a valid username".to_string(),
))
}
}
pub async fn global_checks(data: &Data, user_uuid: Uuid) -> Result<(), Error> {
if data.config.instance.require_email_verification {
let mut conn = data.pool.get().await?;