forked from jmug/stoatchat
chore(refactor): use strong typing for fetch public bot
This commit is contained in:
@@ -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")]
|
||||
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?;
|
||||
if !bot.public {
|
||||
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?;
|
||||
|
||||
Ok(json!({
|
||||
"_id": bot.id,
|
||||
"username": user.username,
|
||||
"avatar": user.avatar,
|
||||
"description": user.profile.map(|p| p.content)
|
||||
Ok(Json(PublicBot {
|
||||
id: bot.id,
|
||||
username: user.username,
|
||||
avatar: user.avatar,
|
||||
description: user.profile.map(|p| p.content).flatten(),
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user