Merge branch 'master' into 'master'
Code cleanup/formatting See merge request revolt/delta!2
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,11 +97,10 @@ 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);
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
send(ClientboundNotification::Error(
|
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 mongodb::bson::doc;
|
||||||
use rocket_contrib::json::JsonValue;
|
use rocket_contrib::json::JsonValue;
|
||||||
|
|||||||
@@ -13,29 +13,39 @@ 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
|
||||||
doc! {
|
.find_one(
|
||||||
"username": username
|
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::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,
|
||||||
|
|||||||
@@ -16,10 +16,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
let col = get_collection("users");
|
let col = get_collection("users");
|
||||||
|
|
||||||
match get_relationship(&user, &target) {
|
match get_relationship(&user, &target) {
|
||||||
RelationshipStatus::Blocked
|
|
||||||
| RelationshipStatus::BlockedOther
|
|
||||||
| RelationshipStatus::User
|
|
||||||
| RelationshipStatus::None => Err(Error::NoEffect),
|
|
||||||
RelationshipStatus::Friend
|
RelationshipStatus::Friend
|
||||||
| RelationshipStatus::Outgoing
|
| RelationshipStatus::Outgoing
|
||||||
| RelationshipStatus::Incoming => {
|
| RelationshipStatus::Incoming => {
|
||||||
@@ -80,5 +76,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_ => Err(Error::NoEffect),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,12 +16,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
let col = get_collection("users");
|
let col = get_collection("users");
|
||||||
|
|
||||||
match get_relationship(&user, &target) {
|
match get_relationship(&user, &target) {
|
||||||
RelationshipStatus::None
|
|
||||||
| RelationshipStatus::User
|
|
||||||
| RelationshipStatus::BlockedOther
|
|
||||||
| RelationshipStatus::Incoming
|
|
||||||
| RelationshipStatus::Outgoing
|
|
||||||
| RelationshipStatus::Friend => Err(Error::NoEffect),
|
|
||||||
RelationshipStatus::Blocked => {
|
RelationshipStatus::Blocked => {
|
||||||
match get_relationship(&target.fetch_user().await?, &user.as_ref()) {
|
match get_relationship(&target.fetch_user().await?, &user.as_ref()) {
|
||||||
RelationshipStatus::Blocked => {
|
RelationshipStatus::Blocked => {
|
||||||
@@ -115,5 +109,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
_ => Err(Error::InternalError),
|
_ => Err(Error::InternalError),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_ => Err(Error::NoEffect),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,12 +42,8 @@ lazy_static! {
|
|||||||
pub fn preflight_checks() {
|
pub fn preflight_checks() {
|
||||||
if *USE_EMAIL == false {
|
if *USE_EMAIL == false {
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
{
|
if !env::var("REVOLT_UNSAFE_NO_EMAIL").map_or(false, |v| v == *"1") {
|
||||||
if !env::var("REVOLT_UNSAFE_NO_EMAIL").map_or(false, |v| v == *"1") {
|
panic!("Running in production without email is not recommended, set REVOLT_UNSAFE_NO_EMAIL=1 to override.");
|
||||||
panic!(
|
|
||||||
"Not letting you run this in production, set REVOLT_UNSAFE_NO_EMAIL=1 to run."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
@@ -56,10 +52,8 @@ pub fn preflight_checks() {
|
|||||||
|
|
||||||
if *USE_HCAPTCHA == false {
|
if *USE_HCAPTCHA == false {
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
{
|
if !env::var("REVOLT_UNSAFE_NO_CAPTCHA").map_or(false, |v| v == *"1") {
|
||||||
if !env::var("REVOLT_UNSAFE_NO_CAPTCHA").map_or(false, |v| v == *"1") {
|
panic!("Running in production without CAPTCHA is not recommended, set REVOLT_UNSAFE_NO_CAPTCHA=1 to override.");
|
||||||
panic!("Not letting you run this in production, set REVOLT_UNSAFE_NO_CAPTCHA=1 to run.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
|
|||||||
Reference in New Issue
Block a user