mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 05:26:59 +00:00
Add fetch owned, public and specific bots for #8.
This commit is contained in:
@@ -214,7 +214,7 @@ impl User {
|
||||
},
|
||||
FindOptions::builder()
|
||||
.projection(
|
||||
doc! { "_id": 1, "username": 1, "avatar": 1, "badges": 1, "status": 1 },
|
||||
doc! { "_id": 1, "username": 1, "avatar": 1, "badges": 1, "status": 1, "flags": 1, "bot": 1 },
|
||||
)
|
||||
.build(),
|
||||
)
|
||||
|
||||
@@ -63,32 +63,32 @@ impl<'r> FromRequest<'r> for User {
|
||||
},
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
let session: Session = request.guard::<Session>().await.unwrap();
|
||||
|
||||
if let Ok(result) = get_collection("users")
|
||||
.find_one(
|
||||
doc! {
|
||||
"_id": &session.user_id
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
{
|
||||
if let Some(doc) = result {
|
||||
Outcome::Success(from_document(doc).unwrap())
|
||||
} else {
|
||||
Outcome::Failure((Status::Forbidden, rauth::util::Error::InvalidSession))
|
||||
}
|
||||
} else {
|
||||
Outcome::Failure((
|
||||
Status::InternalServerError,
|
||||
rauth::util::Error::DatabaseError {
|
||||
operation: "find_one",
|
||||
with: "user",
|
||||
},
|
||||
))
|
||||
let session: Session = request.guard::<Session>().await.unwrap();
|
||||
|
||||
if let Ok(result) = get_collection("users")
|
||||
.find_one(
|
||||
doc! {
|
||||
"_id": &session.user_id
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
{
|
||||
if let Some(doc) = result {
|
||||
Outcome::Success(from_document(doc).unwrap())
|
||||
} else {
|
||||
Outcome::Failure((Status::Forbidden, rauth::util::Error::InvalidSession))
|
||||
}
|
||||
} else {
|
||||
Outcome::Failure((
|
||||
Status::InternalServerError,
|
||||
rauth::util::Error::DatabaseError {
|
||||
operation: "find_one",
|
||||
with: "user",
|
||||
},
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
22
src/routes/bots/fetch.rs
Normal file
22
src/routes/bots/fetch.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
#[get("/<target>")]
|
||||
pub async fn fetch_bot(user: User, target: Ref) -> Result<Value> {
|
||||
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
|
||||
}))
|
||||
}
|
||||
52
src/routes/bots/fetch_owned.rs
Normal file
52
src/routes/bots/fetch_owned.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, 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> {
|
||||
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
|
||||
}))
|
||||
}
|
||||
24
src/routes/bots/fetch_public.rs
Normal file
24
src/routes/bots/fetch_public.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
#[get("/<target>/invite")]
|
||||
pub async fn fetch_public_bot(user: User, target: Ref) -> Result<Value> {
|
||||
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)
|
||||
}))
|
||||
}
|
||||
@@ -2,10 +2,16 @@ use rocket::Route;
|
||||
|
||||
mod create;
|
||||
mod invite;
|
||||
mod fetch_public;
|
||||
mod fetch;
|
||||
mod fetch_owned;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![
|
||||
create::create_bot,
|
||||
invite::invite_bot
|
||||
invite::invite_bot,
|
||||
fetch_public::fetch_public_bot,
|
||||
fetch::fetch_bot,
|
||||
fetch_owned::fetch_owned_bots,
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user