feat(bots): implement bot routes

This commit is contained in:
Paul Makles
2022-02-10 15:54:41 +00:00
parent d4aff072a8
commit a8d5fcec9e
12 changed files with 206 additions and 58 deletions

View File

@@ -1,29 +1,46 @@
use revolt_quark::{EmptyResponse, Result};
use revolt_quark::{models::{User, Channel}, EmptyResponse, Error, Ref, Result, Db, perms, ServerPermission, ChannelPermission};
use rocket::serde::json::Json;
use serde::Deserialize;
#[derive(Deserialize)]
pub struct ServerId {
server: String,
}
#[derive(Deserialize)]
pub struct GroupId {
group: String,
}
#[derive(Deserialize)]
#[serde(untagged)]
pub enum Destination {
Server(ServerId),
Group(GroupId),
Server { server: String },
Group { group: String },
}
#[post("/<target>/invite", data = "<dest>")]
pub async fn invite_bot(
/*user: UserRef, target: Ref,*/ target: String,
dest: Json<Destination>,
) -> Result<EmptyResponse> {
todo!()
pub async fn invite_bot(db: &Db, user: User, target: Ref, dest: Json<Destination>) -> Result<EmptyResponse> {
if user.bot.is_some() {
return Err(Error::IsBot);
}
let bot = target.as_bot(db).await?;
if !bot.public && bot.owner != user.id {
return Err(Error::BotIsPrivate)
}
match dest.into_inner() {
Destination::Server { server } => {
let server = db.fetch_server(&server).await?;
if !perms(&user).server(&server).calc_server(db).await.get_manage_server() {
return Err(Error::MissingPermission { permission: ServerPermission::ManageServer as i32 })
}
db.insert_member(&server.id, &bot.id).await.map(|_| EmptyResponse)
},
Destination::Group{ group } => {
let channel = db.fetch_channel(&group).await?;
if !perms(&user).channel(&channel).calc_channel(db).await.get_invite_others() {
return Err(Error::MissingPermission { permission: ChannelPermission::InviteOthers as i32 })
}
if let Channel::Group { id, .. } = channel {
db.add_user_to_group(&id, &bot.id).await.map(|_| EmptyResponse)
} else {
Err(Error::InvalidOperation)
}
}
}
}