From 0d3ef9a3b74b8ae1d3c9ad3eaef7d4e1ec8de336 Mon Sep 17 00:00:00 2001 From: Paul Makles <paulmakles@gmail.com> Date: Sat, 9 Jan 2021 12:58:15 +0000 Subject: [PATCH] Refractor WS; remove - fom names. --- src/database/entities/user.rs | 65 ++-------------------------------- src/notifications/mod.rs | 1 + src/notifications/payload.rs | 63 ++++++++++++++++++++++++++++++++ src/notifications/websocket.rs | 5 +-- src/routes/onboard/complete.rs | 2 +- 5 files changed, 70 insertions(+), 66 deletions(-) create mode 100644 src/notifications/payload.rs diff --git a/src/database/entities/user.rs b/src/database/entities/user.rs index 9bfa8bf..f36f7b6 100644 --- a/src/database/entities/user.rs +++ b/src/database/entities/user.rs @@ -1,14 +1,6 @@ +use crate::database::get_collection; use crate::database::guards::reference::Ref; -use crate::{ - database::get_collection, - notifications::events::ClientboundNotification, - util::result::{Error, Result}, -}; -use futures::StreamExt; -use mongodb::{ - bson::{doc, from_bson, Bson}, - options::FindOptions, -}; +use mongodb::bson::{doc, from_bson, Bson}; use rauth::auth::Session; use rocket::http::Status; use rocket::request::{self, FromRequest, Outcome, Request}; @@ -81,57 +73,4 @@ impl User { id: self.id.to_string(), } } - - pub async fn generate_ready_payload(self) -> Result<ClientboundNotification> { - let mut users = vec![]; - - if let Some(relationships) = &self.relations { - let user_ids: Vec<String> = relationships - .iter() - .map(|relationship| relationship.id.clone()) - .collect(); - - let mut cursor = get_collection("users") - .find( - doc! { - "_id": { - "$in": user_ids - } - }, - FindOptions::builder() - .projection(doc! { "_id": 1, "username": 1 }) - .build(), - ) - .await - .map_err(|_| Error::DatabaseError { - operation: "find", - with: "users", - })?; - - while let Some(result) = cursor.next().await { - if let Ok(doc) = result { - let mut user: User = - from_bson(Bson::Document(doc)).map_err(|_| Error::DatabaseError { - operation: "from_bson", - with: "user", - })?; - - user.relationship = Some( - relationships - .iter() - .find(|x| user.id == x.id) - .ok_or_else(|| Error::InternalError)? - .status - .clone(), - ); - - users.push(user); - } - } - } - - users.push(self); - - Ok(ClientboundNotification::Ready { users }) - } } diff --git a/src/notifications/mod.rs b/src/notifications/mod.rs index 22f7bf4..577bb11 100644 --- a/src/notifications/mod.rs +++ b/src/notifications/mod.rs @@ -1,4 +1,5 @@ pub mod events; pub mod hive; +pub mod payload; pub mod subscriptions; pub mod websocket; diff --git a/src/notifications/payload.rs b/src/notifications/payload.rs new file mode 100644 index 0000000..0769e57 --- /dev/null +++ b/src/notifications/payload.rs @@ -0,0 +1,63 @@ +use crate::notifications::events::ClientboundNotification; +use crate::{ + database::{entities::User, get_collection}, + util::result::{Error, Result}, +}; +use futures::StreamExt; +use mongodb::{ + bson::{doc, from_bson, Bson}, + options::FindOptions, +}; + +pub async fn generate_ready(user: User) -> Result<ClientboundNotification> { + let mut users = vec![]; + + if let Some(relationships) = &user.relations { + let user_ids: Vec<String> = relationships + .iter() + .map(|relationship| relationship.id.clone()) + .collect(); + + let mut cursor = get_collection("users") + .find( + doc! { + "_id": { + "$in": user_ids + } + }, + FindOptions::builder() + .projection(doc! { "_id": 1, "username": 1 }) + .build(), + ) + .await + .map_err(|_| Error::DatabaseError { + operation: "find", + with: "users", + })?; + + while let Some(result) = cursor.next().await { + if let Ok(doc) = result { + let mut user: User = + from_bson(Bson::Document(doc)).map_err(|_| Error::DatabaseError { + operation: "from_bson", + with: "user", + })?; + + user.relationship = Some( + relationships + .iter() + .find(|x| user.id == x.id) + .ok_or_else(|| Error::InternalError)? + .status + .clone(), + ); + + users.push(user); + } + } + } + + users.push(user); + + Ok(ClientboundNotification::Ready { users }) +} diff --git a/src/notifications/websocket.rs b/src/notifications/websocket.rs index 9845c6c..8f5f467 100644 --- a/src/notifications/websocket.rs +++ b/src/notifications/websocket.rs @@ -97,8 +97,9 @@ async fn accept(stream: TcpStream) { ) { send(ClientboundNotification::Authenticated); - match task::block_on(user.generate_ready_payload()) - { + match task::block_on( + super::payload::generate_ready(user), + ) { Ok(payload) => { send(payload); } diff --git a/src/routes/onboard/complete.rs b/src/routes/onboard/complete.rs index 857d7c6..ffd9e75 100644 --- a/src/routes/onboard/complete.rs +++ b/src/routes/onboard/complete.rs @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize}; use validator::Validate; lazy_static! { - static ref RE_USERNAME: Regex = Regex::new(r"^[a-zA-Z0-9-_]+$").unwrap(); + static ref RE_USERNAME: Regex = Regex::new(r"^[a-zA-Z0-9_]+$").unwrap(); } #[derive(Validate, Serialize, Deserialize)] -- GitLab