forked from jmug/stoatchat
feat: update bot routes to match old spec
This commit is contained in:
@@ -3,13 +3,23 @@ use revolt_quark::{
|
|||||||
Db, Error, Ref, Result,
|
Db, Error, Ref, Result,
|
||||||
};
|
};
|
||||||
use rocket::serde::json::Json;
|
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 Bot
|
||||||
///
|
///
|
||||||
/// Fetch details of a bot you own by its id.
|
/// Fetch details of a bot you own by its id.
|
||||||
#[openapi(tag = "Bots")]
|
#[openapi(tag = "Bots")]
|
||||||
#[get("/<target>")]
|
#[get("/<target>")]
|
||||||
pub async fn fetch_bot(db: &Db, user: User, target: Ref) -> Result<Json<Bot>> {
|
pub async fn fetch_bot(db: &Db, user: User, target: Ref) -> Result<Json<BotResponse>> {
|
||||||
if user.bot.is_some() {
|
if user.bot.is_some() {
|
||||||
return Err(Error::IsBot);
|
return Err(Error::IsBot);
|
||||||
}
|
}
|
||||||
@@ -19,5 +29,8 @@ pub async fn fetch_bot(db: &Db, user: User, target: Ref) -> Result<Json<Bot>> {
|
|||||||
return Err(Error::NotFound);
|
return Err(Error::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Json(bot))
|
Ok(Json(BotResponse {
|
||||||
|
user: db.fetch_user(&bot.id).await?.foreign(),
|
||||||
|
bot,
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,16 +3,40 @@ use revolt_quark::{
|
|||||||
Db, Error, Result,
|
Db, Error, Result,
|
||||||
};
|
};
|
||||||
use rocket::serde::json::Json;
|
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<Bot>,
|
||||||
|
/// User objects
|
||||||
|
users: Vec<User>,
|
||||||
|
}
|
||||||
|
|
||||||
/// # Fetch Owned Bots
|
/// # Fetch Owned Bots
|
||||||
///
|
///
|
||||||
/// Fetch all of the bots that you have control over.
|
/// Fetch all of the bots that you have control over.
|
||||||
#[openapi(tag = "Bots")]
|
#[openapi(tag = "Bots")]
|
||||||
#[get("/@me")]
|
#[get("/@me")]
|
||||||
pub async fn fetch_owned_bots(db: &Db, user: User) -> Result<Json<Vec<Bot>>> {
|
pub async fn fetch_owned_bots(db: &Db, user: User) -> Result<Json<OwnedBotsResponse>> {
|
||||||
if user.bot.is_some() {
|
if user.bot.is_some() {
|
||||||
return Err(Error::IsBot);
|
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::<Vec<String>>();
|
||||||
|
|
||||||
|
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 }))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user