mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 05:26:59 +00:00
Formatted code
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
use crate::{database::get_collection, notifications::events::ClientboundNotification, util::result::{Error, Result}};
|
||||
use crate::database::guards::reference::Ref;
|
||||
use mongodb::{bson::{doc, from_bson, Bson}, options::FindOptions};
|
||||
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 rauth::auth::Session;
|
||||
use rocket::http::Status;
|
||||
use rocket::request::{self, FromRequest, Outcome, Request};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use futures::StreamExt;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub enum RelationshipStatus {
|
||||
@@ -35,7 +42,7 @@ pub struct User {
|
||||
|
||||
// ? This should never be pushed to the collection.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub relationship: Option<RelationshipStatus>
|
||||
pub relationship: Option<RelationshipStatus>,
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
@@ -77,13 +84,13 @@ impl User {
|
||||
|
||||
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! {
|
||||
@@ -93,23 +100,29 @@ impl User {
|
||||
},
|
||||
FindOptions::builder()
|
||||
.projection(doc! { "_id": 1, "username": 1 })
|
||||
.build()
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "find", with: "users" })?;
|
||||
|
||||
.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" })?;
|
||||
|
||||
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()
|
||||
.clone(),
|
||||
);
|
||||
|
||||
users.push(user);
|
||||
|
||||
@@ -33,7 +33,7 @@ pub enum ClientboundNotification {
|
||||
Error(WebSocketError),
|
||||
Authenticated,
|
||||
Ready {
|
||||
users: Vec<User>
|
||||
users: Vec<User>,
|
||||
},
|
||||
|
||||
/*MessageCreate {
|
||||
|
||||
@@ -97,11 +97,10 @@ async fn accept(stream: TcpStream) {
|
||||
) {
|
||||
send(ClientboundNotification::Authenticated);
|
||||
|
||||
match task::block_on(
|
||||
user.generate_ready_payload()
|
||||
) {
|
||||
match task::block_on(user.generate_ready_payload())
|
||||
{
|
||||
Ok(payload) => {
|
||||
send(payload);
|
||||
send(payload);
|
||||
}
|
||||
Err(_) => {
|
||||
send(ClientboundNotification::Error(
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
use crate::util::variables::{DISABLE_REGISTRATION, HCAPTCHA_SITEKEY, USE_EMAIL, USE_HCAPTCHA, EXTERNAL_WS_URL};
|
||||
use crate::util::variables::{
|
||||
DISABLE_REGISTRATION, EXTERNAL_WS_URL, HCAPTCHA_SITEKEY, USE_EMAIL, USE_HCAPTCHA,
|
||||
};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
@@ -13,29 +13,39 @@ use crate::{
|
||||
};
|
||||
use futures::try_join;
|
||||
use mongodb::bson::doc;
|
||||
use mongodb::options::{FindOneOptions, Collation};
|
||||
use mongodb::options::{Collation, FindOneOptions};
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[put("/<username>/friend")]
|
||||
pub async fn req(user: User, username: String) -> Result<JsonValue> {
|
||||
let col = get_collection("users");
|
||||
let doc = col.find_one(
|
||||
doc! {
|
||||
"username": username
|
||||
let doc = col
|
||||
.find_one(
|
||||
doc! {
|
||||
"username": username
|
||||
},
|
||||
FindOneOptions::builder()
|
||||
.collation(Collation::builder().locale("en").strength(2).build())
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "find_one",
|
||||
with: "user",
|
||||
})?
|
||||
.ok_or_else(|| Error::UnknownUser)?;
|
||||
|
||||
let target_id = doc.get_str("_id").map_err(|_| Error::DatabaseError {
|
||||
operation: "get_str(_id)",
|
||||
with: "user",
|
||||
})?;
|
||||
|
||||
match get_relationship(
|
||||
&user,
|
||||
&Ref {
|
||||
id: target_id.to_string(),
|
||||
},
|
||||
FindOneOptions::builder()
|
||||
.collation(Collation::builder().locale("en").strength(2).build())
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "find_one", with: "user" })?
|
||||
.ok_or_else(|| Error::UnknownUser)?;
|
||||
|
||||
let target_id = doc
|
||||
.get_str("_id")
|
||||
.map_err(|_| Error::DatabaseError { operation: "get_str(_id)", with: "user" })?;
|
||||
|
||||
match get_relationship(&user, &Ref { id: target_id.to_string() }) {
|
||||
) {
|
||||
RelationshipStatus::User => return Err(Error::NoEffect),
|
||||
RelationshipStatus::Friend => return Err(Error::AlreadyFriends),
|
||||
RelationshipStatus::Outgoing => return Err(Error::AlreadySentRequest),
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use rocket::Route;
|
||||
|
||||
mod add_friend;
|
||||
mod get_avatar;
|
||||
mod block_user;
|
||||
mod fetch_dms;
|
||||
mod fetch_relationship;
|
||||
mod fetch_relationships;
|
||||
mod fetch_user;
|
||||
mod get_avatar;
|
||||
mod open_dm;
|
||||
mod remove_friend;
|
||||
mod unblock_user;
|
||||
@@ -16,11 +16,9 @@ pub fn routes() -> Vec<Route> {
|
||||
// User Information
|
||||
fetch_user::req,
|
||||
get_avatar::req,
|
||||
|
||||
// Direct Messaging
|
||||
fetch_dms::req,
|
||||
open_dm::req,
|
||||
|
||||
// Relationships
|
||||
fetch_relationships::req,
|
||||
fetch_relationship::req,
|
||||
|
||||
@@ -75,7 +75,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
with: "user",
|
||||
}),
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => Err(Error::NoEffect),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
}
|
||||
_ => Err(Error::InternalError),
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => Err(Error::NoEffect),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user