feat(delta): example implementation of new core models

This commit is contained in:
Paul Makles
2023-04-22 17:56:48 +01:00
parent 633eb78630
commit 403a94f70c
4 changed files with 58 additions and 55 deletions

View File

@@ -1,44 +1,32 @@
use revolt_quark::{
models::{File, User},
Db, Error, Ref, Result,
};
use revolt_database::Database;
use revolt_models::PublicBot;
use revolt_quark::{models::User, Db, Error, Ref, Result};
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
/// # Public Bot
#[derive(Serialize, Deserialize, JsonSchema)]
pub struct PublicBot {
/// Bot Id
#[serde(rename = "_id")]
id: String,
/// Bot Username
username: String,
/// Profile Avatar
#[serde(skip_serializing_if = "Option::is_none")]
avatar: Option<File>,
/// Profile Description
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
}
use rocket::State;
/// # Fetch Public Bot
///
/// Fetch details of a public (or owned) bot by its id.
#[openapi(tag = "Bots")]
#[get("/<target>/invite")]
pub async fn fetch_public_bot(db: &Db, user: Option<User>, target: Ref) -> Result<Json<PublicBot>> {
let bot = target.as_bot(db).await?;
pub async fn fetch_public_bot(
legacy_db: &Db,
db: &State<Database>,
user: Option<User>,
target: Ref,
) -> Result<Json<PublicBot>> {
let bot = db.fetch_bot(&target.id).await.map_err(Error::from_core)?;
if !bot.public && user.map_or(true, |x| x.id != bot.owner) {
return Err(Error::NotFound);
}
let user = db.fetch_user(&bot.id).await?;
let user = legacy_db.fetch_user(&bot.id).await?;
Ok(Json(PublicBot {
id: bot.id,
username: user.username,
avatar: user.avatar,
description: user.profile.and_then(|p| p.content),
}))
Ok(Json(PublicBot::from(
bot,
user.username,
user.avatar.map(|f| f.id),
user.profile.and_then(|p| p.content),
)))
}