feat(bots): implement bot routes
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
use crate::util::regex::RE_USERNAME;
|
use crate::util::{regex::RE_USERNAME, variables::MAX_BOT_COUNT};
|
||||||
|
|
||||||
use revolt_quark::Result;
|
use nanoid::nanoid;
|
||||||
|
use revolt_quark::{
|
||||||
|
models::{Bot, User},
|
||||||
|
Db, Error, Result,
|
||||||
|
};
|
||||||
|
|
||||||
use rocket::serde::json::{Json, Value};
|
use rocket::serde::json::Json;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use ulid::Ulid;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
|
||||||
#[derive(Validate, Serialize, Deserialize)]
|
#[derive(Validate, Serialize, Deserialize)]
|
||||||
@@ -13,6 +18,26 @@ pub struct Data {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[post("/create", data = "<info>")]
|
#[post("/create", data = "<info>")]
|
||||||
pub async fn create_bot(/* user: User ,*/ info: Json<Data>) -> Result<Value> {
|
pub async fn create_bot(db: &Db, user: User, info: Json<Data>) -> Result<Json<Bot>> {
|
||||||
todo!()
|
if user.bot.is_some() {
|
||||||
|
return Err(Error::IsBot);
|
||||||
|
}
|
||||||
|
|
||||||
|
let info = info.into_inner();
|
||||||
|
info.validate()
|
||||||
|
.map_err(|error| Error::FailedValidation { error })?;
|
||||||
|
|
||||||
|
if db.get_number_of_bots_by_user(&user.id).await? >= *MAX_BOT_COUNT {
|
||||||
|
return Err(Error::ReachedMaximumBots);
|
||||||
|
}
|
||||||
|
|
||||||
|
let bot = Bot {
|
||||||
|
id: Ulid::new().to_string(),
|
||||||
|
owner: user.id,
|
||||||
|
token: nanoid!(64),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
db.insert_bot(&bot).await?;
|
||||||
|
Ok(Json(bot))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,16 @@
|
|||||||
use rauth::util::EmptyResponse;
|
use rauth::util::EmptyResponse;
|
||||||
use revolt_quark::Result;
|
use revolt_quark::{models::User, Db, Error, Ref, Result};
|
||||||
|
|
||||||
#[delete("/<target>")]
|
#[delete("/<target>")]
|
||||||
pub async fn delete_bot(/*user: UserRef, target: Ref*/ target: String,) -> Result<EmptyResponse> {
|
pub async fn delete_bot(db: &Db, user: User, target: Ref) -> Result<EmptyResponse> {
|
||||||
todo!()
|
if user.bot.is_some() {
|
||||||
|
return Err(Error::IsBot);
|
||||||
|
}
|
||||||
|
|
||||||
|
let bot = target.as_bot(db).await?;
|
||||||
|
if bot.owner != user.id {
|
||||||
|
return Err(Error::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.delete_bot(&bot.id).await.map(|_| EmptyResponse)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
use crate::util::regex::RE_USERNAME;
|
use crate::util::regex::RE_USERNAME;
|
||||||
|
|
||||||
use revolt_quark::{models::bot::FieldsBot, EmptyResponse, Result};
|
use revolt_quark::{
|
||||||
|
models::{
|
||||||
|
bot::{FieldsBot, PartialBot},
|
||||||
|
Bot, User,
|
||||||
|
},
|
||||||
|
Db, Error, Ref, Result,
|
||||||
|
};
|
||||||
|
|
||||||
use mongodb::bson::doc;
|
use mongodb::bson::doc;
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
@@ -15,13 +21,67 @@ pub struct Data {
|
|||||||
public: Option<bool>,
|
public: Option<bool>,
|
||||||
analytics: Option<bool>,
|
analytics: Option<bool>,
|
||||||
interactions_url: Option<String>,
|
interactions_url: Option<String>,
|
||||||
remove: Option<FieldsBot>,
|
#[validate(length(min = 1))]
|
||||||
|
remove: Option<Vec<FieldsBot>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[patch("/<target>", data = "<data>")]
|
#[patch("/<target>", data = "<data>")]
|
||||||
pub async fn edit_bot(
|
pub async fn edit_bot(db: &Db, user: User, target: Ref, data: Json<Data>) -> Result<Json<Bot>> {
|
||||||
/*user: UserRef, target: Ref,*/ target: String,
|
if user.bot.is_some() {
|
||||||
data: Json<Data>,
|
return Err(Error::IsBot);
|
||||||
) -> Result<EmptyResponse> {
|
}
|
||||||
todo!()
|
|
||||||
|
let data = data.into_inner();
|
||||||
|
data.validate()
|
||||||
|
.map_err(|error| Error::FailedValidation { error })?;
|
||||||
|
|
||||||
|
let mut bot = target.as_bot(db).await?;
|
||||||
|
if bot.owner != user.id {
|
||||||
|
return Err(Error::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(name) = data.name {
|
||||||
|
if db.is_username_taken(&name).await? {
|
||||||
|
return Err(Error::UsernameTaken);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut user = db.fetch_user(&bot.id).await?;
|
||||||
|
user.update_username(db, name).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if data.public.is_none()
|
||||||
|
|| data.analytics.is_none()
|
||||||
|
|| data.interactions_url.is_none()
|
||||||
|
|| data.remove.is_none()
|
||||||
|
{
|
||||||
|
return Ok(Json(bot));
|
||||||
|
}
|
||||||
|
|
||||||
|
let Data {
|
||||||
|
public,
|
||||||
|
analytics,
|
||||||
|
interactions_url,
|
||||||
|
remove,
|
||||||
|
..
|
||||||
|
} = data;
|
||||||
|
let mut partial = PartialBot {
|
||||||
|
public,
|
||||||
|
analytics,
|
||||||
|
interactions_url,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(remove) = &remove {
|
||||||
|
for field in remove {
|
||||||
|
bot.remove(field);
|
||||||
|
}
|
||||||
|
|
||||||
|
if remove.iter().any(|x| x == &FieldsBot::Token) {
|
||||||
|
partial.token = Some(bot.token.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
db.update_bot(&bot.id, &partial, remove.unwrap_or_else(Vec::new))
|
||||||
|
.await?;
|
||||||
|
Ok(Json(bot))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,19 @@
|
|||||||
use revolt_quark::Result;
|
use revolt_quark::{
|
||||||
|
models::{Bot, User},
|
||||||
use serde_json::Value;
|
Db, Error, Ref, Result,
|
||||||
|
};
|
||||||
|
use rocket::serde::json::Json;
|
||||||
|
|
||||||
#[get("/<target>")]
|
#[get("/<target>")]
|
||||||
pub async fn fetch_bot(/*user: UserRef, target: Ref*/ target: String) -> Result<Value> {
|
pub async fn fetch_bot(db: &Db, user: User, target: Ref) -> Result<Json<Bot>> {
|
||||||
todo!()
|
if user.bot.is_some() {
|
||||||
|
return Err(Error::IsBot);
|
||||||
|
}
|
||||||
|
|
||||||
|
let bot = target.as_bot(db).await?;
|
||||||
|
if bot.owner != user.id {
|
||||||
|
return Err(Error::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Json(bot))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,14 @@
|
|||||||
use revolt_quark::Result;
|
use revolt_quark::{
|
||||||
|
models::{Bot, User},
|
||||||
use serde_json::Value;
|
Db, Error, Result,
|
||||||
|
};
|
||||||
|
use rocket::serde::json::Json;
|
||||||
|
|
||||||
#[get("/@me")]
|
#[get("/@me")]
|
||||||
pub async fn fetch_owned_bots(/*user: UserRef*/) -> Result<Value> {
|
pub async fn fetch_owned_bots(db: &Db, user: User) -> Result<Json<Vec<Bot>>> {
|
||||||
todo!()
|
if user.bot.is_some() {
|
||||||
|
return Err(Error::IsBot);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.fetch_bots_by_user(&user.id).await.map(Json)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,20 @@
|
|||||||
use revolt_quark::Result;
|
use revolt_quark::{Db, Error, Ref, Result};
|
||||||
|
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
#[get("/<target>/invite")]
|
#[get("/<target>/invite")]
|
||||||
pub async fn fetch_public_bot(/*user: UserRef, target: Ref*/ target: String) -> Result<Value> {
|
pub async fn fetch_public_bot(db: &Db, target: Ref) -> Result<Value> {
|
||||||
todo!()
|
let bot = target.as_bot(db).await?;
|
||||||
|
if !bot.public {
|
||||||
|
return Err(Error::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
let user = db.fetch_user(&bot.id).await?;
|
||||||
|
|
||||||
|
Ok(json!({
|
||||||
|
"_id": bot.id,
|
||||||
|
"username": user.username,
|
||||||
|
"avatar": user.avatar,
|
||||||
|
"description": user.profile.map(|p| p.content)
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 rocket::serde::json::Json;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct ServerId {
|
|
||||||
server: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct GroupId {
|
|
||||||
group: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum Destination {
|
pub enum Destination {
|
||||||
Server(ServerId),
|
Server { server: String },
|
||||||
Group(GroupId),
|
Group { group: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/<target>/invite", data = "<dest>")]
|
#[post("/<target>/invite", data = "<dest>")]
|
||||||
pub async fn invite_bot(
|
pub async fn invite_bot(db: &Db, user: User, target: Ref, dest: Json<Destination>) -> Result<EmptyResponse> {
|
||||||
/*user: UserRef, target: Ref,*/ target: String,
|
if user.bot.is_some() {
|
||||||
dest: Json<Destination>,
|
return Err(Error::IsBot);
|
||||||
) -> Result<EmptyResponse> {
|
}
|
||||||
todo!()
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,18 @@
|
|||||||
use revolt_quark::{EmptyResponse, Error, Result, Ref, models::User, Db, perms};
|
use revolt_quark::{models::User, perms, Db, EmptyResponse, Error, Ref, Result};
|
||||||
|
|
||||||
#[put("/<target>/ack/<message>")]
|
#[put("/<target>/ack/<message>")]
|
||||||
pub async fn req(
|
pub async fn req(db: &Db, user: User, target: Ref, message: Ref) -> Result<EmptyResponse> {
|
||||||
db: &Db,
|
|
||||||
user: User, target: Ref, message: Ref
|
|
||||||
) -> Result<EmptyResponse> {
|
|
||||||
let channel = target.as_channel(db).await?;
|
let channel = target.as_channel(db).await?;
|
||||||
if !perms(&user).channel(&channel).calc_channel(db).await.get_view() {
|
if !perms(&user)
|
||||||
return Err(Error::NotFound)
|
.channel(&channel)
|
||||||
|
.calc_channel(db)
|
||||||
|
.await
|
||||||
|
.get_view()
|
||||||
|
{
|
||||||
|
return Err(Error::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
db.acknowledge_message(channel.id(), &user.id, &message.id).await.map(|_| EmptyResponse)
|
db.acknowledge_message(channel.id(), &user.id, &message.id)
|
||||||
|
.await
|
||||||
|
.map(|_| EmptyResponse)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,5 +10,5 @@ struct CreateUserResponse {
|
|||||||
|
|
||||||
#[post("/<_target>/join_call")]
|
#[post("/<_target>/join_call")]
|
||||||
pub async fn req(_user: User, _target: Ref) -> Result<Value> {
|
pub async fn req(_user: User, _target: Ref) -> Result<Value> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ pub struct Data {
|
|||||||
nickname: Option<String>,
|
nickname: Option<String>,
|
||||||
avatar: Option<String>,
|
avatar: Option<String>,
|
||||||
roles: Option<Vec<String>>,
|
roles: Option<Vec<String>>,
|
||||||
remove: Option<FieldsMember>,
|
#[validate(length(min = 1))]
|
||||||
|
remove: Option<Vec<FieldsMember>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[patch("/<server>/members/<target>", data = "<data>")]
|
#[patch("/<server>/members/<target>", data = "<data>")]
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ pub struct Data {
|
|||||||
colour: Option<String>,
|
colour: Option<String>,
|
||||||
hoist: Option<bool>,
|
hoist: Option<bool>,
|
||||||
rank: Option<i64>,
|
rank: Option<i64>,
|
||||||
// remove: Option<FieldsRole>,
|
// #[validate(length(min = 1))]
|
||||||
|
// remove: Option<Vec<FieldsRole>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[patch("/<target>/roles/<role_id>", data = "<data>")]
|
#[patch("/<target>/roles/<role_id>", data = "<data>")]
|
||||||
|
|||||||
@@ -17,9 +17,11 @@ pub struct Data {
|
|||||||
banner: Option<String>,
|
banner: Option<String>,
|
||||||
categories: Option<Vec<Category>>,
|
categories: Option<Vec<Category>>,
|
||||||
system_messages: Option<SystemMessageChannels>,
|
system_messages: Option<SystemMessageChannels>,
|
||||||
remove: Option<FieldsServer>,
|
|
||||||
nsfw: Option<bool>,
|
nsfw: Option<bool>,
|
||||||
analytics: Option<bool>,
|
analytics: Option<bool>,
|
||||||
|
|
||||||
|
#[validate(length(min = 1))]
|
||||||
|
remove: Option<Vec<FieldsServer>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[patch("/<target>", data = "<data>")]
|
#[patch("/<target>", data = "<data>")]
|
||||||
|
|||||||
Reference in New Issue
Block a user