forked from jmug/stoatchat
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 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 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 {
|
||||||
@@ -35,7 +42,7 @@ pub struct User {
|
|||||||
|
|
||||||
// ? This should never be pushed to the collection.
|
// ? This should never be pushed to the collection.
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub relationship: Option<RelationshipStatus>
|
pub relationship: Option<RelationshipStatus>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::async_trait]
|
#[rocket::async_trait]
|
||||||
@@ -93,15 +100,21 @@ impl User {
|
|||||||
},
|
},
|
||||||
FindOptions::builder()
|
FindOptions::builder()
|
||||||
.projection(doc! { "_id": 1, "username": 1 })
|
.projection(doc! { "_id": 1, "username": 1 })
|
||||||
.build()
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| Error::DatabaseError { operation: "find", with: "users" })?;
|
.map_err(|_| Error::DatabaseError {
|
||||||
|
operation: "find",
|
||||||
|
with: "users",
|
||||||
|
})?;
|
||||||
|
|
||||||
while let Some(result) = cursor.next().await {
|
while let Some(result) = cursor.next().await {
|
||||||
if let Ok(doc) = result {
|
if let Ok(doc) = result {
|
||||||
let mut user: User = from_bson(Bson::Document(doc))
|
let mut user: User =
|
||||||
.map_err(|_| Error::DatabaseError { operation: "from_bson", with: "user" })?;
|
from_bson(Bson::Document(doc)).map_err(|_| Error::DatabaseError {
|
||||||
|
operation: "from_bson",
|
||||||
|
with: "user",
|
||||||
|
})?;
|
||||||
|
|
||||||
user.relationship = Some(
|
user.relationship = Some(
|
||||||
relationships
|
relationships
|
||||||
@@ -109,7 +122,7 @@ impl User {
|
|||||||
.find(|x| user.id == x.id)
|
.find(|x| user.id == x.id)
|
||||||
.ok_or_else(|| Error::InternalError)?
|
.ok_or_else(|| Error::InternalError)?
|
||||||
.status
|
.status
|
||||||
.clone()
|
.clone(),
|
||||||
);
|
);
|
||||||
|
|
||||||
users.push(user);
|
users.push(user);
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ pub enum ClientboundNotification {
|
|||||||
Error(WebSocketError),
|
Error(WebSocketError),
|
||||||
Authenticated,
|
Authenticated,
|
||||||
Ready {
|
Ready {
|
||||||
users: Vec<User>
|
users: Vec<User>,
|
||||||
},
|
},
|
||||||
|
|
||||||
/*MessageCreate {
|
/*MessageCreate {
|
||||||
|
|||||||
@@ -97,9 +97,8 @@ async fn accept(stream: TcpStream) {
|
|||||||
) {
|
) {
|
||||||
send(ClientboundNotification::Authenticated);
|
send(ClientboundNotification::Authenticated);
|
||||||
|
|
||||||
match task::block_on(
|
match task::block_on(user.generate_ready_payload())
|
||||||
user.generate_ready_payload()
|
{
|
||||||
) {
|
|
||||||
Ok(payload) => {
|
Ok(payload) => {
|
||||||
send(payload);
|
send(payload);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 mongodb::bson::doc;
|
||||||
use rocket_contrib::json::JsonValue;
|
use rocket_contrib::json::JsonValue;
|
||||||
|
|||||||
@@ -13,13 +13,14 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use futures::try_join;
|
use futures::try_join;
|
||||||
use mongodb::bson::doc;
|
use mongodb::bson::doc;
|
||||||
use mongodb::options::{FindOneOptions, Collation};
|
use mongodb::options::{Collation, FindOneOptions};
|
||||||
use rocket_contrib::json::JsonValue;
|
use rocket_contrib::json::JsonValue;
|
||||||
|
|
||||||
#[put("/<username>/friend")]
|
#[put("/<username>/friend")]
|
||||||
pub async fn req(user: User, username: String) -> Result<JsonValue> {
|
pub async fn req(user: User, username: String) -> Result<JsonValue> {
|
||||||
let col = get_collection("users");
|
let col = get_collection("users");
|
||||||
let doc = col.find_one(
|
let doc = col
|
||||||
|
.find_one(
|
||||||
doc! {
|
doc! {
|
||||||
"username": username
|
"username": username
|
||||||
},
|
},
|
||||||
@@ -28,14 +29,23 @@ pub async fn req(user: User, username: String) -> Result<JsonValue> {
|
|||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| Error::DatabaseError { operation: "find_one", with: "user" })?
|
.map_err(|_| Error::DatabaseError {
|
||||||
|
operation: "find_one",
|
||||||
|
with: "user",
|
||||||
|
})?
|
||||||
.ok_or_else(|| Error::UnknownUser)?;
|
.ok_or_else(|| Error::UnknownUser)?;
|
||||||
|
|
||||||
let target_id = doc
|
let target_id = doc.get_str("_id").map_err(|_| Error::DatabaseError {
|
||||||
.get_str("_id")
|
operation: "get_str(_id)",
|
||||||
.map_err(|_| Error::DatabaseError { operation: "get_str(_id)", with: "user" })?;
|
with: "user",
|
||||||
|
})?;
|
||||||
|
|
||||||
match get_relationship(&user, &Ref { id: target_id.to_string() }) {
|
match get_relationship(
|
||||||
|
&user,
|
||||||
|
&Ref {
|
||||||
|
id: target_id.to_string(),
|
||||||
|
},
|
||||||
|
) {
|
||||||
RelationshipStatus::User => return Err(Error::NoEffect),
|
RelationshipStatus::User => return Err(Error::NoEffect),
|
||||||
RelationshipStatus::Friend => return Err(Error::AlreadyFriends),
|
RelationshipStatus::Friend => return Err(Error::AlreadyFriends),
|
||||||
RelationshipStatus::Outgoing => return Err(Error::AlreadySentRequest),
|
RelationshipStatus::Outgoing => return Err(Error::AlreadySentRequest),
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
use rocket::Route;
|
use rocket::Route;
|
||||||
|
|
||||||
mod add_friend;
|
mod add_friend;
|
||||||
mod get_avatar;
|
|
||||||
mod block_user;
|
mod block_user;
|
||||||
mod fetch_dms;
|
mod fetch_dms;
|
||||||
mod fetch_relationship;
|
mod fetch_relationship;
|
||||||
mod fetch_relationships;
|
mod fetch_relationships;
|
||||||
mod fetch_user;
|
mod fetch_user;
|
||||||
|
mod get_avatar;
|
||||||
mod open_dm;
|
mod open_dm;
|
||||||
mod remove_friend;
|
mod remove_friend;
|
||||||
mod unblock_user;
|
mod unblock_user;
|
||||||
@@ -16,11 +16,9 @@ pub fn routes() -> Vec<Route> {
|
|||||||
// User Information
|
// User Information
|
||||||
fetch_user::req,
|
fetch_user::req,
|
||||||
get_avatar::req,
|
get_avatar::req,
|
||||||
|
|
||||||
// Direct Messaging
|
// Direct Messaging
|
||||||
fetch_dms::req,
|
fetch_dms::req,
|
||||||
open_dm::req,
|
open_dm::req,
|
||||||
|
|
||||||
// Relationships
|
// Relationships
|
||||||
fetch_relationships::req,
|
fetch_relationships::req,
|
||||||
fetch_relationship::req,
|
fetch_relationship::req,
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
with: "user",
|
with: "user",
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
_ => Err(Error::NoEffect),
|
_ => Err(Error::NoEffect),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
}
|
}
|
||||||
_ => Err(Error::InternalError),
|
_ => Err(Error::InternalError),
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
_ => Err(Error::NoEffect),
|
_ => Err(Error::NoEffect),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user