Update POST me/friend endpoint to take username instead of UUID #31
2 changed files with 24 additions and 4 deletions
|
@ -1,4 +1,3 @@
|
||||||
use ::uuid::Uuid;
|
|
||||||
use actix_web::{HttpRequest, HttpResponse, get, post, web};
|
use actix_web::{HttpRequest, HttpResponse, get, post, web};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
@ -9,7 +8,7 @@ use crate::{
|
||||||
api::v1::auth::check_access_token,
|
api::v1::auth::check_access_token,
|
||||||
error::Error,
|
error::Error,
|
||||||
objects::Me,
|
objects::Me,
|
||||||
utils::{get_auth_header, global_checks},
|
utils::{get_auth_header, global_checks, user_uuid_from_username}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Returns a list of users that are your friends
|
/// Returns a list of users that are your friends
|
||||||
|
@ -34,7 +33,7 @@ pub async fn get(req: HttpRequest, data: web::Data<Data>) -> Result<HttpResponse
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct UserReq {
|
struct UserReq {
|
||||||
uuid: Uuid,
|
username: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `POST /api/v1/me/friends` Send friend request
|
/// `POST /api/v1/me/friends` Send friend request
|
||||||
|
@ -74,7 +73,8 @@ pub async fn post(
|
||||||
|
|
||||||
let me = Me::get(&mut conn, uuid).await?;
|
let me = Me::get(&mut conn, uuid).await?;
|
||||||
|
|
||||||
me.add_friend(&mut conn, json.uuid).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())
|
Ok(HttpResponse::Ok().finish())
|
||||||
}
|
}
|
||||||
|
|
20
src/utils.rs
20
src/utils.rs
|
@ -168,6 +168,26 @@ pub async fn user_uuid_from_identifier(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn user_uuid_from_username(
|
||||||
|
conn: &mut Conn,
|
||||||
|
username: &String,
|
||||||
|
) -> Result<Uuid, Error> {
|
||||||
|
if USERNAME_REGEX.is_match(username) {
|
||||||
|
use users::dsl;
|
||||||
|
let user_uuid = dsl::users
|
||||||
|
.filter(dsl::username.eq(username))
|
||||||
|
.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> {
|
pub async fn global_checks(data: &Data, user_uuid: Uuid) -> Result<(), Error> {
|
||||||
if data.config.instance.require_email_verification {
|
if data.config.instance.require_email_verification {
|
||||||
let mut conn = data.pool.get().await?;
|
let mut conn = data.pool.get().await?;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue