From 78c42fd4125b516b0af5d159509faf89b4ab9fea Mon Sep 17 00:00:00 2001 From: Paul <paulmakles@gmail.com> Date: Fri, 2 Apr 2021 21:55:02 +0100 Subject: [PATCH] Fetch profile route, push badges and status. --- Cargo.lock | 4 +++- Cargo.toml | 2 +- src/database/entities/user.rs | 24 +++++++++++++++++++++++- src/notifications/payload.rs | 2 +- src/routes/channels/message_send.rs | 2 +- src/routes/root.rs | 2 +- src/routes/users/fetch_profile.rs | 24 ++++++++++++++++++++++++ src/routes/users/mod.rs | 2 ++ 8 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 src/routes/users/fetch_profile.rs diff --git a/Cargo.lock b/Cargo.lock index b9b1ca7..bc76ea8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1068,6 +1068,8 @@ checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35" [[package]] name = "hive_pubsub" version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ac4635b1e9faf304ccb33c2f0edbc615030e40d249183281fc788ef20ef5c76" dependencies = [ "futures", "many-to-many", @@ -2473,7 +2475,7 @@ dependencies = [ [[package]] name = "revolt" -version = "0.4.0-alpha.2" +version = "0.4.0-alpha.3" dependencies = [ "async-std", "async-tungstenite", diff --git a/Cargo.toml b/Cargo.toml index 2bd5f10..cc28e92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "revolt" -version = "0.4.0-alpha.2" +version = "0.4.0-alpha.3" authors = ["Paul Makles <paulmakles@gmail.com>"] edition = "2018" diff --git a/src/database/entities/user.rs b/src/database/entities/user.rs index dc70322..81d87c7 100644 --- a/src/database/entities/user.rs +++ b/src/database/entities/user.rs @@ -25,6 +25,20 @@ pub struct Relationship { pub status: RelationshipStatus, } +/* +pub enum Badge { + Developer = 1 +} +*/ + +#[derive(Serialize, Deserialize, Debug)] +#[serde(tag = "type")] +pub enum UserStatus { + Text { + text: String + } +} + #[derive(Serialize, Deserialize, Debug)] pub struct User { #[serde(rename = "_id")] @@ -33,6 +47,13 @@ pub struct User { #[serde(skip_serializing_if = "Option::is_none")] pub relations: Option<Vec<Relationship>>, + #[serde(skip_serializing_if = "Option::is_none")] + pub badges: Option<i32>, + #[serde(skip_serializing_if = "Option::is_none")] + pub status: Option<Vec<UserStatus>>, + #[serde(skip_serializing_if = "Option::is_none")] + pub profile: Option<String>, + // ? This should never be pushed to the collection. #[serde(skip_serializing_if = "Option::is_none")] pub relationship: Option<RelationshipStatus>, @@ -65,6 +86,7 @@ impl User { self.online = Some(is_online(&self.id)); } + self.profile = None; self } @@ -73,7 +95,7 @@ impl User { if username == "revolt" && username == "admin" { return Ok(true) } - + if get_collection("users") .find_one( doc! { diff --git a/src/notifications/payload.rs b/src/notifications/payload.rs index 5a8d612..73cf789 100644 --- a/src/notifications/payload.rs +++ b/src/notifications/payload.rs @@ -77,7 +77,7 @@ pub async fn generate_ready(mut user: User) -> Result<ClientboundNotification> { } }, FindOptions::builder() - .projection(doc! { "_id": 1, "username": 1 }) + .projection(doc! { "_id": 1, "username": 1, "badges": 1, "status": 1 }) .build(), ) .await diff --git a/src/routes/channels/message_send.rs b/src/routes/channels/message_send.rs index 6803ca5..626fba9 100644 --- a/src/routes/channels/message_send.rs +++ b/src/routes/channels/message_send.rs @@ -32,11 +32,11 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal } let target = target.fetch_channel().await?; - let perm = permissions::PermissionCalculator::new(&user) .with_channel(&target) .for_channel() .await?; + if !perm.get_send_message() { Err(Error::MissingPermission)? } diff --git a/src/routes/root.rs b/src/routes/root.rs index b8cc6c9..18c9c53 100644 --- a/src/routes/root.rs +++ b/src/routes/root.rs @@ -9,7 +9,7 @@ use rocket_contrib::json::JsonValue; #[get("/")] pub async fn root() -> JsonValue { json!({ - "revolt": "0.4.0-alpha.2", + "revolt": "0.4.0-alpha.3", "features": { "registration": !*DISABLE_REGISTRATION, "captcha": { diff --git a/src/routes/users/fetch_profile.rs b/src/routes/users/fetch_profile.rs new file mode 100644 index 0000000..7e2b6b9 --- /dev/null +++ b/src/routes/users/fetch_profile.rs @@ -0,0 +1,24 @@ +use crate::database::*; +use crate::util::result::{Error, Result}; + +use mongodb::bson::doc; +use rocket_contrib::json::JsonValue; + +#[get("/<target>/profile")] +pub async fn req(user: User, target: Ref) -> Result<JsonValue> { + let target = target.fetch_user().await?; + let perm = permissions::PermissionCalculator::new(&user) + .with_user(&target) + .for_user_given() + .await?; + + if !perm.get_view_profile() { + Err(Error::MissingPermission)? + } + + if target.profile.is_some() { + Ok(json!({ "profile": target.profile })) + } else { + Ok(json!({})) + } +} diff --git a/src/routes/users/mod.rs b/src/routes/users/mod.rs index 163978f..6945bcb 100644 --- a/src/routes/users/mod.rs +++ b/src/routes/users/mod.rs @@ -3,6 +3,7 @@ use rocket::Route; mod add_friend; mod block_user; mod fetch_dms; +mod fetch_profile; mod fetch_relationship; mod fetch_relationships; mod fetch_user; @@ -19,6 +20,7 @@ pub fn routes() -> Vec<Route> { fetch_user::req, get_default_avatar::req, get_avatar::req, + fetch_profile::req, // Direct Messaging fetch_dms::req, open_dm::req, -- GitLab