feat: move me endpoint to /me and add /me/servers
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful

This commit is contained in:
Radical 2025-05-27 07:46:10 +00:00
parent b8cf21903e
commit 39d01bb0d0
7 changed files with 61 additions and 8 deletions

View file

View file

96
src/api/v1/me/mod.rs Normal file
View file

@ -0,0 +1,96 @@
use actix_multipart::form::{MultipartForm, json::Json as MpJson, tempfile::TempFile};
use actix_web::{get, patch, web, HttpRequest, HttpResponse, Scope};
use serde::Deserialize;
use crate::{
Data, api::v1::auth::check_access_token, error::Error, structs::Me, utils::get_auth_header,
};
mod servers;
pub fn web() -> Scope {
web::scope("/me")
.service(get)
.service(update)
}
#[get("")]
pub async fn get(req: HttpRequest, data: web::Data<Data>) -> Result<HttpResponse, Error> {
let headers = req.headers();
let auth_header = get_auth_header(headers)?;
let mut conn = data.pool.get().await?;
let uuid = check_access_token(auth_header, &mut conn).await?;
let me = Me::get(&mut conn, uuid).await?;
Ok(HttpResponse::Ok().json(me))
}
#[derive(Debug, Deserialize)]
struct NewInfo {
username: Option<String>,
display_name: Option<String>,
password: Option<String>,
email: Option<String>,
}
#[derive(Debug, MultipartForm)]
struct UploadForm {
#[multipart(limit = "100MB")]
avatar: Option<TempFile>,
json: Option<MpJson<NewInfo>>,
}
#[patch("")]
pub async fn update(
req: HttpRequest,
MultipartForm(form): MultipartForm<UploadForm>,
data: web::Data<Data>,
) -> Result<HttpResponse, Error> {
let headers = req.headers();
let auth_header = get_auth_header(headers)?;
let mut conn = data.pool.get().await?;
let uuid = check_access_token(auth_header, &mut conn).await?;
let mut me = Me::get(&mut conn, uuid).await?;
if let Some(avatar) = form.avatar {
let bytes = tokio::fs::read(avatar.file).await?;
let byte_slice: &[u8] = &bytes;
me.set_avatar(
&data.bunny_cdn,
&mut conn,
data.config.bunny.cdn_url.clone(),
byte_slice.into(),
)
.await?;
}
if let Some(new_info) = form.json {
if let Some(username) = &new_info.username {
todo!();
}
if let Some(display_name) = &new_info.display_name {
todo!();
}
if let Some(password) = &new_info.password {
todo!();
}
if let Some(email) = &new_info.email {
todo!();
}
}
Ok(HttpResponse::Ok().finish())
}

35
src/api/v1/me/servers.rs Normal file
View file

@ -0,0 +1,35 @@
//! Contains endpoint related to guild memberships
use actix_web::{get, web, HttpRequest, HttpResponse};
use crate::{api::v1::auth::check_access_token, error::Error, structs::Me, utils::get_auth_header, Data};
/// `GET /api/v1/me/servers` Returns all guild memberships in a list
///
/// Example Response:
/// ```
/// json!({
/// "uuid": "22006503-fb01-46e6-8e0e-70336dac6c63",
/// "nickname": "This field is nullable",
/// "user_uuid": "522bca17-de63-4706-9d18-0971867ad1e0",
/// "guild_uuid": "0911e468-3e9e-47bf-8381-59b30e8b68a8"
/// });
/// ```
/// NOTE: UUIDs in this response are made using `uuidgen`, UUIDs made by the actual backend will be UUIDv7 and have extractable timestamps
#[get("/servers")]
pub async fn get(req: HttpRequest, data: web::Data<Data>) -> Result<HttpResponse, Error> {
let headers = req.headers();
let auth_header = get_auth_header(headers)?;
let mut conn = data.pool.get().await?;
let uuid = check_access_token(auth_header, &mut conn).await?;
let me = Me::get(&mut conn, uuid).await?;
let memberships = me.fetch_memberships(&mut conn).await?;
Ok(HttpResponse::Ok().json(memberships))
}