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,11 +1,11 @@
use revolt_quark::{
models::{Bot, User},
Db, Error, Ref, Result,
};
use rocket::serde::json::Json;
use revolt_database::Database;
use revolt_models::Bot;
use revolt_quark::{models::User, Db, Error, Ref, Result};
use rocket::{serde::json::Json, State};
use serde::Serialize;
/// # Bot Response
/// TODO: move to revolt-models
#[derive(Serialize, JsonSchema)]
pub struct BotResponse {
/// Bot object
@@ -19,18 +19,23 @@ pub struct BotResponse {
/// Fetch details of a bot you own by its id.
#[openapi(tag = "Bots")]
#[get("/<target>")]
pub async fn fetch_bot(db: &Db, user: User, target: Ref) -> Result<Json<BotResponse>> {
pub async fn fetch_bot(
legacy_db: &Db,
db: &State<Database>,
user: User,
target: Ref,
) -> Result<Json<BotResponse>> {
if user.bot.is_some() {
return Err(Error::IsBot);
}
let bot = target.as_bot(db).await?;
let bot = db.fetch_bot(&target.id).await.map_err(Error::from_core)?;
if bot.owner != user.id {
return Err(Error::NotFound);
}
Ok(Json(BotResponse {
user: db.fetch_user(&bot.id).await?.foreign(),
bot,
user: legacy_db.fetch_user(&bot.id).await?.foreign(),
bot: bot.into(),
}))
}