mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
Refractor WS; remove - fom names.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
pub mod events;
|
||||
pub mod hive;
|
||||
pub mod payload;
|
||||
pub mod subscriptions;
|
||||
pub mod websocket;
|
||||
|
||||
63
src/notifications/payload.rs
Normal file
63
src/notifications/payload.rs
Normal file
@@ -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 })
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user