Skip to content
Snippets Groups Projects
Commit cac21ce2 authored by insert's avatar insert
Browse files

Fix import and cargo fmt

parent 0640f68f
No related merge requests found
Pipeline #411 passed with stage
in 2 minutes and 16 seconds
use rauth::auth::Session; use rauth::auth::Session;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use snafu::Snafu; use snafu::Snafu;
use hive_pubsub::PubSub;
use crate::database::entities::RelationshipStatus; use crate::database::entities::RelationshipStatus;
...@@ -89,7 +88,6 @@ pub enum ClientboundNotification { ...@@ -89,7 +88,6 @@ pub enum ClientboundNotification {
GuildDelete { GuildDelete {
id: String, id: String,
},*/ },*/
UserRelationship { UserRelationship {
id: String, id: String,
user: String, user: String,
......
pub mod events; pub mod events;
pub mod hive; pub mod hive;
pub mod websocket;
pub mod subscriptions; pub mod subscriptions;
pub mod websocket;
use crate::database::get_collection; use crate::database::get_collection;
use crate::{database::entities::User, util::variables::WS_HOST};
use crate::database::guards::reference::Ref; use crate::database::guards::reference::Ref;
use crate::util::variables::WS_HOST;
use super::subscriptions; use super::subscriptions;
...@@ -10,15 +10,18 @@ use async_tungstenite::tungstenite::Message; ...@@ -10,15 +10,18 @@ use async_tungstenite::tungstenite::Message;
use futures::channel::mpsc::{unbounded, UnboundedSender}; use futures::channel::mpsc::{unbounded, UnboundedSender};
use futures::stream::TryStreamExt; use futures::stream::TryStreamExt;
use futures::{pin_mut, prelude::*}; use futures::{pin_mut, prelude::*};
use hive_pubsub::PubSub;
use log::{debug, info}; use log::{debug, info};
use many_to_many::ManyToMany; use many_to_many::ManyToMany;
use rauth::auth::{Auth, Session}; use rauth::auth::{Auth, Session};
use std::collections::HashMap; use std::collections::HashMap;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::{Arc, Mutex, RwLock}; use std::sync::{Arc, Mutex, RwLock};
use hive_pubsub::PubSub;
use super::{events::{ClientboundNotification, ServerboundNotification, WebSocketError}, hive::get_hive}; use super::{
events::{ClientboundNotification, ServerboundNotification, WebSocketError},
hive::get_hive,
};
type Tx = UnboundedSender<Message>; type Tx = UnboundedSender<Message>;
type PeerMap = Arc<Mutex<HashMap<SocketAddr, Tx>>>; type PeerMap = Arc<Mutex<HashMap<SocketAddr, Tx>>>;
...@@ -54,9 +57,7 @@ async fn accept(stream: TcpStream) { ...@@ -54,9 +57,7 @@ async fn accept(stream: TcpStream) {
CONNECTIONS.lock().unwrap().insert(addr, tx.clone()); CONNECTIONS.lock().unwrap().insert(addr, tx.clone());
let send = |notification: ClientboundNotification| { let send = |notification: ClientboundNotification| {
if let Ok(response) = serde_json::to_string( if let Ok(response) = serde_json::to_string(&notification) {
&notification,
) {
if let Err(_) = tx.unbounded_send(Message::Text(response)) { if let Err(_) = tx.unbounded_send(Message::Text(response)) {
debug!("Failed unbounded_send to websocket stream."); debug!("Failed unbounded_send to websocket stream.");
} }
...@@ -71,8 +72,10 @@ async fn accept(stream: TcpStream) { ...@@ -71,8 +72,10 @@ async fn accept(stream: TcpStream) {
match notification { match notification {
ServerboundNotification::Authenticate(new_session) => { ServerboundNotification::Authenticate(new_session) => {
if session.is_some() { if session.is_some() {
send(ClientboundNotification::Error(WebSocketError::AlreadyAuthenticated)); send(ClientboundNotification::Error(
return future::ok(()) WebSocketError::AlreadyAuthenticated,
));
return future::ok(());
} }
match task::block_on( match task::block_on(
...@@ -80,29 +83,41 @@ async fn accept(stream: TcpStream) { ...@@ -80,29 +83,41 @@ async fn accept(stream: TcpStream) {
) { ) {
Ok(validated_session) => { Ok(validated_session) => {
match task::block_on( match task::block_on(
Ref { id: validated_session.user_id.clone() } Ref {
.fetch_user() id: validated_session.user_id.clone(),
}
.fetch_user(),
) { ) {
Ok(user) => { Ok(user) => {
if let Ok(mut map) = USERS.write() { if let Ok(mut map) = USERS.write() {
map.insert(validated_session.user_id.clone(), addr); map.insert(validated_session.user_id.clone(), addr);
session = Some(validated_session); session = Some(validated_session);
if let Ok(_) = task::block_on(subscriptions::generate_subscriptions(&user)) { if let Ok(_) = task::block_on(
subscriptions::generate_subscriptions(&user),
) {
send(ClientboundNotification::Authenticated); send(ClientboundNotification::Authenticated);
} else { } else {
send(ClientboundNotification::Error(WebSocketError::InternalError)); send(ClientboundNotification::Error(
WebSocketError::InternalError,
));
} }
} else { } else {
send(ClientboundNotification::Error(WebSocketError::InternalError)); send(ClientboundNotification::Error(
WebSocketError::InternalError,
));
} }
}, }
Err(_) => { Err(_) => {
send(ClientboundNotification::Error(WebSocketError::OnboardingNotFinished)); send(ClientboundNotification::Error(
WebSocketError::OnboardingNotFinished,
));
} }
} }
} }
Err(_) => { Err(_) => {
send(ClientboundNotification::Error(WebSocketError::InvalidSession)); send(ClientboundNotification::Error(
WebSocketError::InvalidSession,
));
} }
} }
} }
......
use crate::{notifications::{events::ClientboundNotification, hive}, util::result::Result};
use crate::{ use crate::{
database::{ database::{
entities::{RelationshipStatus, User}, entities::{RelationshipStatus, User},
...@@ -8,6 +7,10 @@ use crate::{ ...@@ -8,6 +7,10 @@ use crate::{
}, },
util::result::Error, util::result::Error,
}; };
use crate::{
notifications::{events::ClientboundNotification, hive},
util::result::Result,
};
use futures::try_join; use futures::try_join;
use mongodb::bson::doc; use mongodb::bson::doc;
use rocket_contrib::json::JsonValue; use rocket_contrib::json::JsonValue;
...@@ -55,16 +58,19 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { ...@@ -55,16 +58,19 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
id: user.id.clone(), id: user.id.clone(),
user: target.id.clone(), user: target.id.clone(),
status: RelationshipStatus::Friend status: RelationshipStatus::Friend
}.publish(user.id.clone()), }
.publish(user.id.clone()),
ClientboundNotification::UserRelationship { ClientboundNotification::UserRelationship {
id: target.id.clone(), id: target.id.clone(),
user: user.id.clone(), user: user.id.clone(),
status: RelationshipStatus::Friend status: RelationshipStatus::Friend
}.publish(target.id.clone()) }
).ok(); .publish(target.id.clone())
)
.ok();
Ok(json!({ "status": "Friend" })) Ok(json!({ "status": "Friend" }))
}, }
Err(_) => Err(Error::DatabaseError { Err(_) => Err(Error::DatabaseError {
operation: "update_one", operation: "update_one",
with: "user", with: "user",
...@@ -108,19 +114,22 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { ...@@ -108,19 +114,22 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
id: user.id.clone(), id: user.id.clone(),
user: target.id.clone(), user: target.id.clone(),
status: RelationshipStatus::Outgoing status: RelationshipStatus::Outgoing
}.publish(user.id.clone()), }
.publish(user.id.clone()),
ClientboundNotification::UserRelationship { ClientboundNotification::UserRelationship {
id: target.id.clone(), id: target.id.clone(),
user: user.id.clone(), user: user.id.clone(),
status: RelationshipStatus::Incoming status: RelationshipStatus::Incoming
}.publish(target.id.clone()) }
).ok(); .publish(target.id.clone())
)
.ok();
hive::subscribe_if_exists(user.id.clone(), target.id.clone()).ok(); hive::subscribe_if_exists(user.id.clone(), target.id.clone()).ok();
hive::subscribe_if_exists(target.id.clone(), user.id.clone()).ok(); hive::subscribe_if_exists(target.id.clone(), user.id.clone()).ok();
Ok(json!({ "status": "Outgoing" })) Ok(json!({ "status": "Outgoing" }))
}, }
Err(_) => Err(Error::DatabaseError { Err(_) => Err(Error::DatabaseError {
operation: "update_one", operation: "update_one",
with: "user", with: "user",
......
use crate::{notifications::{events::ClientboundNotification, hive}, util::result::Result};
use crate::{ use crate::{
database::entities::RelationshipStatus, database::entities::User, database::get_collection, database::entities::RelationshipStatus, database::entities::User, database::get_collection,
database::guards::reference::Ref, database::permissions::get_relationship, util::result::Error, database::guards::reference::Ref, database::permissions::get_relationship, util::result::Error,
}; };
use crate::{
notifications::{events::ClientboundNotification, hive},
util::result::Result,
};
use futures::try_join; use futures::try_join;
use mongodb::bson::doc; use mongodb::bson::doc;
use rocket_contrib::json::JsonValue; use rocket_contrib::json::JsonValue;
...@@ -35,8 +38,11 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { ...@@ -35,8 +38,11 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
ClientboundNotification::UserRelationship { ClientboundNotification::UserRelationship {
id: user.id.clone(), id: user.id.clone(),
user: target.id.clone(), user: target.id.clone(),
status: RelationshipStatus::Blocked status: RelationshipStatus::Blocked,
}.publish(user.id.clone()).await.ok(); }
.publish(user.id.clone())
.await
.ok();
Ok(json!({ "status": "Blocked" })) Ok(json!({ "status": "Blocked" }))
} }
...@@ -77,19 +83,22 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { ...@@ -77,19 +83,22 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
id: user.id.clone(), id: user.id.clone(),
user: target.id.clone(), user: target.id.clone(),
status: RelationshipStatus::Blocked status: RelationshipStatus::Blocked
}.publish(user.id.clone()), }
.publish(user.id.clone()),
ClientboundNotification::UserRelationship { ClientboundNotification::UserRelationship {
id: target.id.clone(), id: target.id.clone(),
user: user.id.clone(), user: user.id.clone(),
status: RelationshipStatus::BlockedOther status: RelationshipStatus::BlockedOther
}.publish(target.id.clone()) }
).ok(); .publish(target.id.clone())
)
.ok();
hive::subscribe_if_exists(user.id.clone(), target.id.clone()).ok(); hive::subscribe_if_exists(user.id.clone(), target.id.clone()).ok();
hive::subscribe_if_exists(target.id.clone(), user.id.clone()).ok(); hive::subscribe_if_exists(target.id.clone(), user.id.clone()).ok();
Ok(json!({ "status": "Blocked" })) Ok(json!({ "status": "Blocked" }))
}, }
Err(_) => Err(Error::DatabaseError { Err(_) => Err(Error::DatabaseError {
operation: "update_one", operation: "update_one",
with: "user", with: "user",
...@@ -131,16 +140,19 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { ...@@ -131,16 +140,19 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
id: user.id.clone(), id: user.id.clone(),
user: target.id.clone(), user: target.id.clone(),
status: RelationshipStatus::Blocked status: RelationshipStatus::Blocked
}.publish(user.id.clone()), }
.publish(user.id.clone()),
ClientboundNotification::UserRelationship { ClientboundNotification::UserRelationship {
id: target.id.clone(), id: target.id.clone(),
user: user.id.clone(), user: user.id.clone(),
status: RelationshipStatus::BlockedOther status: RelationshipStatus::BlockedOther
}.publish(target.id.clone()) }
).ok(); .publish(target.id.clone())
)
.ok();
Ok(json!({ "status": "Blocked" })) Ok(json!({ "status": "Blocked" }))
}, }
Err(_) => Err(Error::DatabaseError { Err(_) => Err(Error::DatabaseError {
operation: "update_one", operation: "update_one",
with: "user", with: "user",
......
use crate::database::entities::{Channel, User}; use crate::database::entities::User;
use crate::database::get_collection; use crate::database::get_collection;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result};
use futures::StreamExt; use futures::StreamExt;
use mongodb::bson::{doc, from_bson, Bson}; use mongodb::bson::doc;
use rocket_contrib::json::JsonValue; use rocket_contrib::json::JsonValue;
#[get("/dms")] #[get("/dms")]
......
use crate::{notifications::{hive, events::ClientboundNotification}, util::result::Result};
use crate::{ use crate::{
database::entities::RelationshipStatus, database::entities::User, database::get_collection, database::entities::RelationshipStatus, database::entities::User, database::get_collection,
database::guards::reference::Ref, database::permissions::get_relationship, util::result::Error, database::guards::reference::Ref, database::permissions::get_relationship, util::result::Error,
}; };
use crate::{
notifications::{events::ClientboundNotification, hive},
util::result::Result,
};
use futures::try_join; use futures::try_join;
use hive_pubsub::PubSub;
use mongodb::bson::doc; use mongodb::bson::doc;
use rocket_contrib::json::JsonValue; use rocket_contrib::json::JsonValue;
use hive_pubsub::PubSub;
#[delete("/<target>/friend")] #[delete("/<target>/friend")]
pub async fn req(user: User, target: Ref) -> Result<JsonValue> { pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
...@@ -54,20 +57,23 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { ...@@ -54,20 +57,23 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
id: user.id.clone(), id: user.id.clone(),
user: target.id.clone(), user: target.id.clone(),
status: RelationshipStatus::None status: RelationshipStatus::None
}.publish(user.id.clone()), }
.publish(user.id.clone()),
ClientboundNotification::UserRelationship { ClientboundNotification::UserRelationship {
id: target.id.clone(), id: target.id.clone(),
user: user.id.clone(), user: user.id.clone(),
status: RelationshipStatus::None status: RelationshipStatus::None
}.publish(target.id.clone()) }
).ok(); .publish(target.id.clone())
)
.ok();
let hive = hive::get_hive(); let hive = hive::get_hive();
hive.unsubscribe(&user.id, &target.id).ok(); hive.unsubscribe(&user.id, &target.id).ok();
hive.unsubscribe(&target.id, &user.id).ok(); hive.unsubscribe(&target.id, &user.id).ok();
Ok(json!({ "status": "None" })) Ok(json!({ "status": "None" }))
}, }
Err(_) => Err(Error::DatabaseError { Err(_) => Err(Error::DatabaseError {
operation: "update_one", operation: "update_one",
with: "user", with: "user",
......
use crate::{notifications::{events::ClientboundNotification, hive}, util::result::Result};
use crate::{ use crate::{
database::entities::RelationshipStatus, database::entities::User, database::get_collection, database::entities::RelationshipStatus, database::entities::User, database::get_collection,
database::guards::reference::Ref, database::permissions::get_relationship, util::result::Error, database::guards::reference::Ref, database::permissions::get_relationship, util::result::Error,
}; };
use crate::{
notifications::{events::ClientboundNotification, hive},
util::result::Result,
};
use futures::try_join; use futures::try_join;
use hive_pubsub::PubSub;
use mongodb::bson::doc; use mongodb::bson::doc;
use rocket_contrib::json::JsonValue; use rocket_contrib::json::JsonValue;
...@@ -42,8 +46,11 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { ...@@ -42,8 +46,11 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
ClientboundNotification::UserRelationship { ClientboundNotification::UserRelationship {
id: user.id.clone(), id: user.id.clone(),
user: target.id.clone(), user: target.id.clone(),
status: RelationshipStatus::BlockedOther status: RelationshipStatus::BlockedOther,
}.publish(user.id.clone()).await.ok(); }
.publish(user.id.clone())
.await
.ok();
Ok(json!({ "status": "BlockedOther" })) Ok(json!({ "status": "BlockedOther" }))
} }
...@@ -82,20 +89,23 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { ...@@ -82,20 +89,23 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
id: user.id.clone(), id: user.id.clone(),
user: target.id.clone(), user: target.id.clone(),
status: RelationshipStatus::None status: RelationshipStatus::None
}.publish(user.id.clone()), }
.publish(user.id.clone()),
ClientboundNotification::UserRelationship { ClientboundNotification::UserRelationship {
id: target.id.clone(), id: target.id.clone(),
user: user.id.clone(), user: user.id.clone(),
status: RelationshipStatus::None status: RelationshipStatus::None
}.publish(target.id.clone()) }
).ok(); .publish(target.id.clone())
)
.ok();
let hive = hive::get_hive(); let hive = hive::get_hive();
hive.unsubscribe(&user.id, &target.id).ok(); hive.unsubscribe(&user.id, &target.id).ok();
hive.unsubscribe(&target.id, &user.id).ok(); hive.unsubscribe(&target.id, &user.id).ok();
Ok(json!({ "status": "None" })) Ok(json!({ "status": "None" }))
}, }
Err(_) => Err(Error::DatabaseError { Err(_) => Err(Error::DatabaseError {
operation: "update_one", operation: "update_one",
with: "user", with: "user",
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment