forked from jmug/stoatchat
New perm concept, reference, and adding routes.
This commit is contained in:
42
src/routes/users/fetch_dms.rs
Normal file
42
src/routes/users/fetch_dms.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use crate::database::entities::{Channel, User};
|
||||
use mongodb::bson::{Bson, doc, from_bson};
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::database::get_collection;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use futures::StreamExt;
|
||||
|
||||
#[get("/dms")]
|
||||
pub async fn req(user: User) -> Result<JsonValue> {
|
||||
let mut cursor = get_collection("channels")
|
||||
.find(
|
||||
doc! {
|
||||
"$or": [
|
||||
{
|
||||
"type": "DirectMessage",
|
||||
"active": true
|
||||
},
|
||||
{
|
||||
"type": "Group"
|
||||
}
|
||||
],
|
||||
"recipients": user.id
|
||||
},
|
||||
None
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "find", with: "channels" })?;
|
||||
|
||||
let mut channels: Vec<Channel> = vec![];
|
||||
while let Some(result) = cursor.next().await {
|
||||
if let Ok(doc) = result {
|
||||
channels.push(
|
||||
from_bson(Bson::Document(doc))
|
||||
.map_err(|_| Error::DatabaseError { operation: "from_bson", with: "channel" })?
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(json!(
|
||||
channels
|
||||
))
|
||||
}
|
||||
@@ -1,7 +1,22 @@
|
||||
use crate::util::result::Result;
|
||||
use crate::database::guards::reference::Ref;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::database::entities::User;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[get("/<id>")]
|
||||
pub async fn req(id: String) -> Result<String> {
|
||||
println!("{}", id);
|
||||
Ok("LETS FUCKING GOOOO".to_string())
|
||||
#[get("/<target>")]
|
||||
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
let mut target = target.fetch_user().await?;
|
||||
|
||||
if user.id != target.id {
|
||||
// Check whether we are allowed to fetch this user.
|
||||
let perm = crate::database::permissions::temp_calc_perm(&user, &target).await;
|
||||
if !perm.get_access() {
|
||||
Err(Error::LabelMe)?
|
||||
}
|
||||
|
||||
// Only return user relationships if the target is the caller.
|
||||
target.relations = None;
|
||||
}
|
||||
|
||||
Ok(json!(target))
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use rocket::Route;
|
||||
|
||||
mod fetch_user;
|
||||
mod fetch_dms;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes! [
|
||||
fetch_user::req
|
||||
fetch_user::req,
|
||||
fetch_dms::req
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user