Make ready payload send all users we have a relationship with.
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
use crate::database::get_collection;
|
use crate::{database::get_collection, notifications::events::ClientboundNotification, util::result::{Error, Result}};
|
||||||
use crate::database::guards::reference::Ref;
|
use crate::database::guards::reference::Ref;
|
||||||
use mongodb::bson::{doc, from_bson, Bson};
|
use mongodb::{bson::{doc, from_bson, Bson}, options::FindOptions};
|
||||||
use rauth::auth::Session;
|
use rauth::auth::Session;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use rocket::request::{self, FromRequest, Outcome, Request};
|
use rocket::request::{self, FromRequest, Outcome, Request};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use futures::StreamExt;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub enum RelationshipStatus {
|
pub enum RelationshipStatus {
|
||||||
@@ -31,6 +32,10 @@ pub struct User {
|
|||||||
pub username: String,
|
pub username: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub relations: Option<Vec<Relationship>>,
|
pub relations: Option<Vec<Relationship>>,
|
||||||
|
|
||||||
|
// ? This should never be pushed to the collection.
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub relationship: Option<RelationshipStatus>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::async_trait]
|
#[rocket::async_trait]
|
||||||
@@ -69,4 +74,51 @@ impl User {
|
|||||||
id: self.id.to_string(),
|
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 })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ pub enum ClientboundNotification {
|
|||||||
Error(WebSocketError),
|
Error(WebSocketError),
|
||||||
Authenticated,
|
Authenticated,
|
||||||
Ready {
|
Ready {
|
||||||
user: User
|
users: Vec<User>
|
||||||
},
|
},
|
||||||
|
|
||||||
/*MessageCreate {
|
/*MessageCreate {
|
||||||
|
|||||||
@@ -96,7 +96,19 @@ async fn accept(stream: TcpStream) {
|
|||||||
subscriptions::generate_subscriptions(&user),
|
subscriptions::generate_subscriptions(&user),
|
||||||
) {
|
) {
|
||||||
send(ClientboundNotification::Authenticated);
|
send(ClientboundNotification::Authenticated);
|
||||||
send(ClientboundNotification::Ready { user });
|
|
||||||
|
match task::block_on(
|
||||||
|
user.generate_ready_payload()
|
||||||
|
) {
|
||||||
|
Ok(payload) => {
|
||||||
|
send(payload);
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
send(ClientboundNotification::Error(
|
||||||
|
WebSocketError::InternalError,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
send(ClientboundNotification::Error(
|
send(ClientboundNotification::Error(
|
||||||
WebSocketError::InternalError,
|
WebSocketError::InternalError,
|
||||||
|
|||||||
Reference in New Issue
Block a user