feat(core): permissions query, finish bots impl

This commit is contained in:
Paul Makles
2023-08-05 16:14:47 +01:00
parent 9f3c1036d0
commit a681df04bd
20 changed files with 481 additions and 173 deletions

View File

@@ -1,31 +1,16 @@
use revolt_quark::{
models::{Bot, User},
Db, Error, Result,
};
use futures::future::join_all;
use revolt_database::{Database, User};
use revolt_models::v0::OwnedBotsResponse;
use revolt_result::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<Bot>,
/// User objects
users: Vec<User>,
}
use rocket::State;
/// # 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<Json<OwnedBotsResponse>> {
if user.bot.is_some() {
return Err(Error::IsBot);
}
pub async fn fetch_owned_bots(db: &State<Database>, user: User) -> Result<Json<OwnedBotsResponse>> {
let mut bots = db.fetch_bots_by_user(&user.id).await?;
let user_ids = bots
.iter()
@@ -38,5 +23,8 @@ pub async fn fetch_owned_bots(db: &Db, user: User) -> Result<Json<OwnedBotsRespo
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 }))
Ok(Json(OwnedBotsResponse {
users: join_all(users.into_iter().map(|user| user.into_self())).await,
bots: bots.into_iter().map(|bot| bot.into()).collect(),
}))
}