chore(refactor): use strong typing for fetch public bot

This commit is contained in:
Paul Makles
2022-03-03 20:05:41 +00:00
parent 8d8bcff31f
commit 465d766254

View File

@@ -1,9 +1,21 @@
use revolt_quark::{Db, Error, Ref, Result}; use revolt_quark::{models::File, Db, Error, Ref, Result};
use serde_json::Value; use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct PublicBot {
#[serde(rename = "_id")]
id: String,
username: String,
#[serde(skip_serializing_if = "Option::is_none")]
avatar: Option<File>,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
}
#[get("/<target>/invite")] #[get("/<target>/invite")]
pub async fn fetch_public_bot(db: &Db, target: Ref) -> Result<Value> { pub async fn fetch_public_bot(db: &Db, target: Ref) -> Result<Json<PublicBot>> {
let bot = target.as_bot(db).await?; let bot = target.as_bot(db).await?;
if !bot.public { if !bot.public {
return Err(Error::NotFound); return Err(Error::NotFound);
@@ -11,10 +23,10 @@ pub async fn fetch_public_bot(db: &Db, target: Ref) -> Result<Value> {
let user = db.fetch_user(&bot.id).await?; let user = db.fetch_user(&bot.id).await?;
Ok(json!({ Ok(Json(PublicBot {
"_id": bot.id, id: bot.id,
"username": user.username, username: user.username,
"avatar": user.avatar, avatar: user.avatar,
"description": user.profile.map(|p| p.content) description: user.profile.map(|p| p.content).flatten(),
})) }))
} }