chore(thanos): strip down codebase to just API routes

This commit is contained in:
Paul Makles
2022-01-27 14:16:30 +00:00
parent ab4c3f913e
commit 0fdb749199
116 changed files with 893 additions and 10025 deletions

View File

@@ -1,13 +1,9 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::variables::MAX_BOT_COUNT;
use crate::util::regex::RE_USERNAME;
use mongodb::bson::{doc, to_document};
use revolt_quark::Result;
use rocket::serde::json::{Json, Value};
use serde::{Deserialize, Serialize};
use ulid::Ulid;
use nanoid::nanoid;
use validator::Validate;
#[derive(Validate, Serialize, Deserialize)]
@@ -17,73 +13,6 @@ pub struct Data {
}
#[post("/create", data = "<info>")]
pub async fn create_bot(user: User, info: Json<Data>) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let info = info.into_inner();
info.validate()
.map_err(|error| Error::FailedValidation { error })?;
if get_collection("bots")
.count_documents(
doc! {
"owner": &user.id
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "count_documents",
with: "bots",
})? as usize >= *MAX_BOT_COUNT {
return Err(Error::ReachedMaximumBots)
}
let id = Ulid::new().to_string();
let token = nanoid!(64);
let bot = Bot {
id: id.clone(),
owner: user.id.clone(),
token,
public: false,
analytics: false,
discoverable: false,
interactions_url: None
};
if User::is_username_taken(&info.name).await? {
return Err(Error::UsernameTaken);
}
get_collection("users")
.insert_one(
doc! {
"_id": &id,
"username": &info.name,
"bot": {
"owner": &user.id
}
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "insert_one",
with: "user",
})?;
get_collection("bots")
.insert_one(
to_document(&bot).map_err(|_| Error::DatabaseError { with: "bot", operation: "to_document" })?,
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "insert_one",
with: "user",
})?;
Ok(json!(bot))
pub async fn create_bot(/* user: User ,*/ info: Json<Data>) -> Result<Value> {
todo!()
}

View File

@@ -1,67 +1,7 @@
use crate::database::*;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, EmptyResponse, Result};
use mongodb::bson::doc;
use revolt_quark::Result;
use rauth::util::EmptyResponse;
#[delete("/<target>")]
pub async fn delete_bot(user: User, target: Ref) -> Result<EmptyResponse> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let bot = target.fetch_bot().await?;
if bot.owner != user.id {
return Err(Error::MissingPermission);
}
let username = format!("Deleted User {}", &bot.id);
get_collection("users")
.update_one(
doc! {
"_id": &bot.id
},
doc! {
"$set": {
"username": &username,
"flags": 2
},
"$unset": {
"avatar": 1,
"status": 1,
"profile": 1
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
with: "user",
operation: "update_one"
})?;
ClientboundNotification::UserUpdate {
id: target.id.clone(),
data: json!({
"username": username,
"flags": 2
}),
clear: None,
}
.publish_as_user(target.id.clone());
get_collection("bots")
.delete_one(
doc! {
"_id": &bot.id
},
None
)
.await
.map_err(|_| Error::DatabaseError {
with: "bot",
operation: "delete_one"
})?;
Ok(EmptyResponse {})
pub async fn delete_bot(/*user: UserRef, target: Ref*/ target: String) -> Result<EmptyResponse> {
todo!()
}

View File

@@ -1,8 +1,7 @@
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::RemoveBotField};
use crate::util::regex::RE_USERNAME;
use revolt_quark::{Result, models::bot::FieldsBot, EmptyResponse};
use mongodb::bson::doc;
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
@@ -16,105 +15,10 @@ pub struct Data {
public: Option<bool>,
analytics: Option<bool>,
interactions_url: Option<String>,
remove: Option<RemoveBotField>,
remove: Option<FieldsBot>,
}
#[patch("/<target>", data = "<data>")]
pub async fn edit_bot(user: User, target: Ref, data: Json<Data>) -> Result<EmptyResponse> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let data = data.into_inner();
data.validate()
.map_err(|error| Error::FailedValidation { error })?;
if data.name.is_none()
&& data.public.is_none()
&& data.analytics.is_none()
&& data.interactions_url.is_none()
&& data.remove.is_none()
{
return Ok(EmptyResponse {});
}
let bot = target.fetch_bot().await?;
if bot.owner != user.id {
return Err(Error::MissingPermission);
}
if let Some(name) = &data.name {
if User::is_username_taken(&name).await? {
return Err(Error::UsernameTaken);
}
get_collection("users")
.update_one(
doc! { "_id": &target.id },
doc! {
"$set": {
"username": name
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "user",
})?;
ClientboundNotification::UserUpdate {
id: target.id.clone(),
data: json!({
"username": name
}),
clear: None,
}
.publish_as_user(target.id.clone());
}
let mut set = doc! {};
let mut unset = doc! {};
if let Some(remove) = &data.remove {
match remove {
RemoveBotField::InteractionsURL => {
unset.insert("interactions_url", 1);
}
}
}
if let Some(public) = &data.public {
set.insert("public", public);
}
if let Some(analytics) = &data.analytics {
set.insert("analytics", analytics);
}
if let Some(interactions_url) = &data.interactions_url {
set.insert("interactions_url", interactions_url);
}
let mut operations = doc! {};
if set.len() > 0 {
operations.insert("$set", &set);
}
if unset.len() > 0 {
operations.insert("$unset", unset);
}
if operations.len() > 0 {
get_collection("bots")
.update_one(doc! { "_id": &target.id }, operations, None)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "bot",
})?;
}
Ok(EmptyResponse {})
pub async fn edit_bot(/*user: UserRef, target: Ref,*/ target: String, data: Json<Data>) -> Result<EmptyResponse> {
todo!()
}

View File

@@ -1,26 +1,8 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use revolt_quark::Result;
use serde_json::Value;
#[get("/<target>")]
pub async fn fetch_bot(user: User, target: Ref) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let bot = target.fetch_bot().await?;
if !bot.public {
if bot.owner != user.id {
return Err(Error::BotIsPrivate);
}
}
let user = Ref::from_unchecked(bot.id.clone()).fetch_user().await?;
Ok(json!({
"bot": bot,
"user": user
}))
pub async fn fetch_bot(/*user: UserRef, target: Ref*/ target: String) -> Result<Value> {
todo!()
}

View File

@@ -1,56 +1,8 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use revolt_quark::Result;
use futures::StreamExt;
use mongodb::bson::{Document, doc, from_document};
use serde_json::Value;
#[get("/@me")]
pub async fn fetch_owned_bots(user: User) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let bots = get_collection("bots")
.find(
doc! {
"owner": &user.id
},
None
)
.await
.map_err(|_| Error::DatabaseError {
with: "bots",
operation: "find"
})?
.filter_map(async move |s| s.ok())
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| from_document(x).ok())
.collect::<Vec<Bot>>();
let users = get_collection("users")
.find(
doc! {
"bot.owner": &user.id
},
None
)
.await
.map_err(|_| Error::DatabaseError {
with: "users",
operation: "find"
})?
.filter_map(async move |s| s.ok())
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| from_document(x).ok())
.collect::<Vec<User>>();
Ok(json!({
"bots": bots,
"users": users
}))
pub async fn fetch_owned_bots(/*user: UserRef*/) -> Result<Value> {
todo!()
}

View File

@@ -1,28 +1,8 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use revolt_quark::Result;
use serde_json::Value;
#[get("/<target>/invite")]
pub async fn fetch_public_bot(user: User, target: Ref) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let bot = target.fetch_bot().await?;
if !bot.public {
if bot.owner != user.id {
return Err(Error::BotIsPrivate);
}
}
let user = Ref::from_unchecked(bot.id.clone()).fetch_user().await?;
Ok(json!({
"_id": bot.id,
"username": user.username,
"avatar": user.avatar,
"description": user.profile.map(|p| p.content)
}))
pub async fn fetch_public_bot(/*user: UserRef, target: Ref*/ target: String) -> Result<Value> {
todo!()
}

View File

@@ -1,5 +1,4 @@
use crate::database::*;
use crate::util::result::{Error, Result, EmptyResponse};
use revolt_quark::{EmptyResponse, Result};
use rocket::serde::json::Json;
use serde::Deserialize;
@@ -22,49 +21,6 @@ pub enum Destination {
}
#[post("/<target>/invite", data = "<dest>")]
pub async fn invite_bot(user: User, target: Ref, dest: Json<Destination>) -> Result<EmptyResponse> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let bot = target.fetch_bot().await?;
if !bot.public {
if bot.owner != user.id {
return Err(Error::BotIsPrivate);
}
}
match dest.into_inner() {
Destination::Server(ServerId { server }) => {
let server = Ref::from(server)?.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_server(&server)
.for_server()
.await?;
if !perm.get_manage_server() {
Err(Error::MissingPermission)?
}
server.join_member(&bot.id).await?;
Ok(EmptyResponse {})
}
Destination::Group(GroupId { group }) => {
let channel = Ref::from(group)?.fetch_channel().await?;
let perm = permissions::PermissionCalculator::new(&user)
.with_channel(&channel)
.for_channel()
.await?;
if !perm.get_invite_others() {
Err(Error::MissingPermission)?
}
channel.add_to_group(bot.id, user.id).await?;
Ok(EmptyResponse {})
}
}
pub async fn invite_bot(/*user: UserRef, target: Ref,*/ target: String, dest: Json<Destination>) -> Result<EmptyResponse> {
todo!()
}