Format code.
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use rauth::auth::{Auth, Session};
|
||||
use regex::Regex;
|
||||
use mongodb::bson::doc;
|
||||
use rocket::State;
|
||||
use validator::Validate;
|
||||
use rocket_contrib::json::Json;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
|
||||
// ! FIXME: should be global somewhere; maybe use config(?)
|
||||
lazy_static! {
|
||||
@@ -24,10 +24,15 @@ pub struct Data {
|
||||
}
|
||||
|
||||
#[patch("/username", data = "<data>")]
|
||||
pub async fn req(auth: State<'_, Auth>, session: Session, user: User, data: Json<Data>) -> Result<()> {
|
||||
pub async fn req(
|
||||
auth: State<'_, Auth>,
|
||||
session: Session,
|
||||
user: User,
|
||||
data: Json<Data>,
|
||||
) -> Result<()> {
|
||||
data.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
|
||||
auth.verify_password(&session, data.password.clone())
|
||||
.await
|
||||
.map_err(|_| Error::InvalidCredentials)?;
|
||||
@@ -35,24 +40,23 @@ pub async fn req(auth: State<'_, Auth>, session: Session, user: User, data: Json
|
||||
let mut set = doc! {};
|
||||
if let Some(username) = &data.username {
|
||||
if User::is_username_taken(&username).await? {
|
||||
return Err(Error::UsernameTaken)
|
||||
return Err(Error::UsernameTaken);
|
||||
}
|
||||
|
||||
set.insert("username", username.clone());
|
||||
}
|
||||
|
||||
|
||||
get_collection("users")
|
||||
.update_one(
|
||||
doc! { "_id": &user.id },
|
||||
doc! { "$set": set },
|
||||
None
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "user" })?;
|
||||
.update_one(doc! { "_id": &user.id }, doc! { "$set": set }, None)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "user",
|
||||
})?;
|
||||
|
||||
ClientboundNotification::UserUpdate {
|
||||
id: user.id.clone(),
|
||||
data: json!(data.0)
|
||||
data: json!(data.0),
|
||||
}
|
||||
.publish(user.id.clone())
|
||||
.await
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use mongodb::bson::{doc, to_document};
|
||||
use validator::Validate;
|
||||
use rocket_contrib::json::Json;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Validate, Serialize, Deserialize)]
|
||||
pub struct Data {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
status: Option<UserStatus>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
profile: Option<UserProfile>
|
||||
profile: Option<UserProfile>,
|
||||
}
|
||||
|
||||
#[patch("/", data = "<data>")]
|
||||
pub async fn req(user: User, data: Json<Data>) -> Result<()> {
|
||||
data.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
|
||||
get_collection("users")
|
||||
.update_one(
|
||||
doc! { "_id": &user.id },
|
||||
@@ -31,7 +31,7 @@ pub async fn req(user: User, data: Json<Data>) -> Result<()> {
|
||||
|
||||
ClientboundNotification::UserUpdate {
|
||||
id: user.id.clone(),
|
||||
data: json!(data.0)
|
||||
data: json!(data.0),
|
||||
}
|
||||
.publish(user.id.clone())
|
||||
.await
|
||||
|
||||
@@ -11,7 +11,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
.with_user(&target)
|
||||
.for_user_given()
|
||||
.await?;
|
||||
|
||||
|
||||
if !perm.get_view_profile() {
|
||||
Err(Error::MissingPermission)?
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user