chore: update everything to work with utoipa

This commit is contained in:
Zomatree
2025-11-13 23:06:41 +00:00
parent 27ea7345ea
commit ac60b2c795
148 changed files with 1200 additions and 1117 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ use rocket::State;
/// # Accept Friend Request
///
/// Accept another user's friend request.
#[openapi(tag = "Relationships")]
#[utoipa::path(tag = "Relationships")]
#[put("/<target>/friend")]
pub async fn add(
db: &State<Database>,
+1 -1
View File
@@ -8,7 +8,7 @@ use rocket::State;
/// # Block User
///
/// Block another user by their id.
#[openapi(tag = "Relationships")]
#[utoipa::path(tag = "Relationships")]
#[put("/<target>/block")]
pub async fn block(
db: &State<Database>,
@@ -15,7 +15,7 @@ use validator::Validate;
pub static RE_USERNAME: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\p{L}|[\d_.-])+$").unwrap());
/// # Username Information
#[derive(Validate, Serialize, Deserialize, JsonSchema)]
#[derive(Validate, Serialize, Deserialize, ToSchema)]
pub struct DataChangeUsername {
/// New username
#[validate(length(min = 2, max = 32), regex = "RE_USERNAME")]
@@ -28,7 +28,7 @@ pub struct DataChangeUsername {
/// # Change Username
///
/// Change your username.
#[openapi(tag = "User Information")]
#[utoipa::path(tag = "User Information")]
#[patch("/@me/username", data = "<data>")]
pub async fn change_username(
db: &State<Database>,
+1 -1
View File
@@ -9,7 +9,7 @@ use validator::Validate;
/// # Edit User
///
/// Edit currently authenticated user.
#[openapi(tag = "User Information")]
#[utoipa::path(tag = "User Information")]
#[patch("/<target>", data = "<data>")]
pub async fn edit(
db: &State<Database>,
+1 -1
View File
@@ -6,7 +6,7 @@ use rocket::{serde::json::Json, State};
/// # Fetch Direct Message Channels
///
/// This fetches your direct messages, including any DM and group DM conversations.
#[openapi(tag = "Direct Messaging")]
#[utoipa::path(tag = "Direct Messaging")]
#[get("/dms")]
pub async fn direct_messages(db: &State<Database>, user: User) -> Result<Json<Vec<v0::Channel>>> {
db.find_direct_messages(&user.id)
@@ -12,7 +12,7 @@ use rocket::{serde::json::Json, State};
/// Retrieve a user's profile data.
///
/// Will fail if you do not have permission to access the other user's profile.
#[openapi(tag = "User Information")]
#[utoipa::path(tag = "User Information")]
#[get("/<target>/profile")]
pub async fn profile(
db: &State<Database>,
+1 -1
View File
@@ -6,7 +6,7 @@ use rocket::serde::json::Json;
/// # Fetch Self
///
/// Retrieve your user information.
#[openapi(tag = "User Information")]
#[utoipa::path(tag = "User Information")]
#[get("/@me")]
pub async fn fetch(user: User) -> Result<Json<v0::User>> {
Ok(Json(user.into_self(false).await))
+6 -2
View File
@@ -11,9 +11,13 @@ use rocket::{serde::json::Json, State};
/// # Fetch User
///
/// Retrieve a user's information.
#[openapi(tag = "User Information")]
#[utoipa::path(tag = "User Information")]
#[get("/<target>")]
pub async fn fetch(db: &State<Database>, user: User, target: Reference<'_>) -> Result<Json<v0::User>> {
pub async fn fetch(
db: &State<Database>,
user: User,
target: Reference<'_>,
) -> Result<Json<v0::User>> {
if user.id == target.id {
return Ok(Json(user.into_self(false).await));
}
@@ -6,7 +6,7 @@ use rocket::{serde::json::Json, State};
/// # Fetch User Flags
///
/// Retrieve a user's flags.
#[openapi(tag = "User Information")]
#[utoipa::path(tag = "User Information")]
#[get("/<target>/flags")]
pub async fn fetch_user_flags(
db: &State<Database>,
+1 -1
View File
@@ -11,7 +11,7 @@ use rocket::State;
/// # Fetch Mutual Friends, Servers, Groups and DMs
///
/// Retrieve a list of mutual friends, servers, groups and DMs with another user.
#[openapi(tag = "Relationships")]
#[utoipa::path(tag = "Relationships")]
#[get("/<target>/mutual")]
pub async fn mutual(
db: &State<Database>,
@@ -1,10 +1,9 @@
use revolt_rocket_okapi::revolt_okapi::openapi3::{self, MediaType, RefOr};
use rocket::http::ContentType;
use rocket::response::{self, Responder};
use rocket::{Request, Response};
use schemars::schema::{InstanceType, SchemaObject, SingleOrVec};
pub struct CachedFile((ContentType, Vec<u8>));
#[derive(ToSchema)]
pub struct CachedFile(Vec<u8>);
pub static CACHE_CONTROL: &str = "public, max-age=31536000, immutable";
@@ -16,50 +15,15 @@ impl<'r> Responder<'r, 'static> for CachedFile {
}
}
impl revolt_rocket_okapi::response::OpenApiResponderInner for CachedFile {
fn responses(
_gen: &mut revolt_rocket_okapi::gen::OpenApiGenerator,
) -> std::result::Result<openapi3::Responses, revolt_rocket_okapi::OpenApiError> {
let mut responses = schemars::Map::new();
let mut content = schemars::Map::new();
content.insert(
"image/png".to_owned(),
MediaType {
schema: Some(SchemaObject {
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::String))),
format: Some("binary".to_owned()),
..Default::default()
}),
..Default::default()
},
);
responses.insert(
"200".to_string(),
RefOr::Object(openapi3::Response {
description: "Default Avatar Picture".to_string(),
content,
..Default::default()
}),
);
Ok(openapi3::Responses {
responses,
..Default::default()
})
}
}
/// # Fetch Default Avatar
///
/// This returns a default avatar based on the given id.
#[openapi(tag = "User Information")]
#[utoipa::path(tag = "User Information")]
#[get("/<target>/default_avatar")]
pub async fn default_avatar(target: String) -> CachedFile {
CachedFile((
pub async fn default_avatar(target: String) -> (ContentType, CachedFile) {
(
ContentType::PNG,
match target.chars().last().unwrap() {
CachedFile(match target.chars().last().unwrap() {
'0' | '1' | '2' | '3' | 'S' | 'Z' => include_bytes!("avatars/2.png").to_vec(),
'4' | '5' | '6' | '7' | 'T' => include_bytes!("avatars/3.png").to_vec(),
'8' | '9' | 'A' | 'B' => include_bytes!("avatars/4.png").to_vec(),
@@ -67,6 +31,6 @@ pub async fn default_avatar(target: String) -> CachedFile {
'G' | 'H' | 'J' | 'K' | 'W' => include_bytes!("avatars/6.png").to_vec(),
'M' | 'N' | 'P' | 'Q' | 'X' => include_bytes!("avatars/7.png").to_vec(),
_ => include_bytes!("avatars/1.png").to_vec(),
},
))
}),
)
}
+24 -3
View File
@@ -1,4 +1,3 @@
use revolt_rocket_okapi::revolt_okapi::openapi3::OpenApi;
use rocket::Route;
mod add_friend;
@@ -17,8 +16,30 @@ mod remove_friend;
mod send_friend_request;
mod unblock_user;
pub fn routes() -> (Vec<Route>, OpenApi) {
openapi_get_routes_spec![
#[derive(OpenApi)]
#[openapi(
paths(
fetch_self::fetch,
fetch_user::fetch,
fetch_user_flags::fetch_user_flags,
edit_user::edit,
change_username::change_username,
get_default_avatar::default_avatar,
fetch_profile::profile,
fetch_dms::direct_messages,
open_dm::open_dm,
find_mutual::mutual,
add_friend::add,
remove_friend::remove,
block_user::block,
unblock_user::unblock,
send_friend_request::send_friend_request,
)
)]
pub struct ApiDoc;
pub fn routes() -> Vec<Route> {
routes![
// User Information
fetch_self::fetch,
fetch_user::fetch,
+1 -1
View File
@@ -12,7 +12,7 @@ use rocket::{serde::json::Json, State};
/// Open a DM with another user.
///
/// If the target is oneself, a saved messages channel is returned.
#[openapi(tag = "Direct Messaging")]
#[utoipa::path(tag = "Direct Messaging")]
#[get("/<target>/dm")]
pub async fn open_dm(
db: &State<Database>,
@@ -8,7 +8,7 @@ use rocket::State;
/// # Deny Friend Request / Remove Friend
///
/// Denies another user's friend request or removes an existing friend.
#[openapi(tag = "Relationships")]
#[utoipa::path(tag = "Relationships")]
#[delete("/<target>/friend")]
pub async fn remove(
db: &State<Database>,
@@ -8,7 +8,7 @@ use rocket::State;
/// # Send Friend Request
///
/// Send a friend request to another user.
#[openapi(tag = "Relationships")]
#[utoipa::path(tag = "Relationships")]
#[post("/friend", data = "<data>")]
pub async fn send_friend_request(
db: &State<Database>,
@@ -8,7 +8,7 @@ use rocket::State;
/// # Unblock User
///
/// Unblock another user by their id.
#[openapi(tag = "Relationships")]
#[utoipa::path(tag = "Relationships")]
#[delete("/<target>/block")]
pub async fn unblock(
db: &State<Database>,