From e245b272fa6729c3c8fbf330f7876ec96f97d39a Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Sun, 20 Mar 2022 18:47:04 +0000 Subject: [PATCH] feat: update bot routes to match old spec --- src/routes/bots/fetch.rs | 17 +++++++++++++++-- src/routes/bots/fetch_owned.rs | 28 ++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/routes/bots/fetch.rs b/src/routes/bots/fetch.rs index c1759b3e..a6f94a4f 100644 --- a/src/routes/bots/fetch.rs +++ b/src/routes/bots/fetch.rs @@ -3,13 +3,23 @@ use revolt_quark::{ Db, Error, Ref, Result, }; use rocket::serde::json::Json; +use serde::Serialize; + +/// # Bot Response +#[derive(Serialize, JsonSchema)] +pub struct BotResponse { + /// Bot object + bot: Bot, + /// User object + user: User, +} /// # Fetch Bot /// /// Fetch details of a bot you own by its id. #[openapi(tag = "Bots")] #[get("/")] -pub async fn fetch_bot(db: &Db, user: User, target: Ref) -> Result> { +pub async fn fetch_bot(db: &Db, user: User, target: Ref) -> Result> { if user.bot.is_some() { return Err(Error::IsBot); } @@ -19,5 +29,8 @@ pub async fn fetch_bot(db: &Db, user: User, target: Ref) -> Result> { return Err(Error::NotFound); } - Ok(Json(bot)) + Ok(Json(BotResponse { + user: db.fetch_user(&bot.id).await?.foreign(), + bot, + })) } diff --git a/src/routes/bots/fetch_owned.rs b/src/routes/bots/fetch_owned.rs index 06a9e183..f437fb7a 100644 --- a/src/routes/bots/fetch_owned.rs +++ b/src/routes/bots/fetch_owned.rs @@ -3,16 +3,40 @@ use revolt_quark::{ Db, Error, Result, }; use rocket::serde::json::Json; +use serde::Serialize; + +/// # Owned Bots Response +/// +/// Both lists are sorted by their IDs. +#[derive(Serialize, JsonSchema)] +pub struct OwnedBotsResponse { + /// Bot objects + bots: Vec, + /// User objects + users: Vec, +} /// # Fetch Owned Bots /// /// Fetch all of the bots that you have control over. #[openapi(tag = "Bots")] #[get("/@me")] -pub async fn fetch_owned_bots(db: &Db, user: User) -> Result>> { +pub async fn fetch_owned_bots(db: &Db, user: User) -> Result> { if user.bot.is_some() { return Err(Error::IsBot); } - db.fetch_bots_by_user(&user.id).await.map(Json) + let mut bots = db.fetch_bots_by_user(&user.id).await?; + let user_ids = bots + .iter() + .map(|x| x.id.to_owned()) + .collect::>(); + + let mut users = db.fetch_users(&user_ids).await?; + + // Ensure the lists match up exactly. + bots.sort_by(|a, b| a.id.cmp(&b.id)); + users.sort_by(|a, b| a.id.cmp(&b.id)); + + Ok(Json(OwnedBotsResponse { users, bots })) }