feat(sync): implement settings and unreads fetch

This commit is contained in:
Paul Makles
2022-02-10 16:55:59 +00:00
parent a8d5fcec9e
commit f676071641
7 changed files with 146 additions and 37 deletions

View File

@@ -1,4 +1,7 @@
use revolt_quark::{models::{User, Channel}, EmptyResponse, Error, Ref, Result, Db, perms, ServerPermission, ChannelPermission};
use revolt_quark::{
models::{Channel, User},
perms, ChannelPermission, Db, EmptyResponse, Error, Ref, Result, ServerPermission,
};
use rocket::serde::json::Json;
use serde::Deserialize;
@@ -11,33 +14,56 @@ pub enum Destination {
}
#[post("/<target>/invite", data = "<dest>")]
pub async fn invite_bot(db: &Db, user: User, target: Ref, dest: Json<Destination>) -> Result<EmptyResponse> {
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)
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 })
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 } => {
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 !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)
db.add_user_to_group(&id, &bot.id)
.await
.map(|_| EmptyResponse)
} else {
Err(Error::InvalidOperation)
}