mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
164 lines
4.5 KiB
Rust
164 lines
4.5 KiB
Rust
use crate::database::*;
|
|
use crate::notifications::events::ClientboundNotification;
|
|
use crate::util::result::{Error, Result};
|
|
|
|
use mongodb::bson::{doc, to_document};
|
|
use rocket_contrib::json::Json;
|
|
use serde::{Deserialize, Serialize};
|
|
use validator::Validate;
|
|
|
|
#[derive(Validate, Serialize, Deserialize, Debug)]
|
|
pub struct UserProfileData {
|
|
#[validate(length(min = 0, max = 2000))]
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
content: Option<String>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
#[validate(length(min = 1, max = 128))]
|
|
background: Option<String>,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
pub enum RemoveField {
|
|
ProfileContent,
|
|
ProfileBackground,
|
|
StatusText,
|
|
Avatar
|
|
}
|
|
|
|
#[derive(Validate, Serialize, Deserialize)]
|
|
pub struct Data {
|
|
#[validate]
|
|
status: Option<UserStatus>,
|
|
#[validate]
|
|
profile: Option<UserProfileData>,
|
|
#[validate(length(min = 1, max = 128))]
|
|
avatar: Option<String>,
|
|
remove: Option<RemoveField>
|
|
}
|
|
|
|
#[patch("/<_ignore_id>", data = "<data>")]
|
|
pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()> {
|
|
let mut data = data.into_inner();
|
|
if data.status.is_none() && data.profile.is_none() && data.avatar.is_none() {
|
|
return Ok(());
|
|
}
|
|
|
|
data.validate()
|
|
.map_err(|error| Error::FailedValidation { error })?;
|
|
|
|
let mut unset = doc! {};
|
|
let mut set = doc! {};
|
|
|
|
let mut remove_background = false;
|
|
let mut remove_avatar = false;
|
|
|
|
if let Some(remove) = data.remove {
|
|
match remove {
|
|
RemoveField::ProfileContent => {
|
|
unset.insert("profile.content", 1);
|
|
},
|
|
RemoveField::ProfileBackground => {
|
|
unset.insert("profile.background", 1);
|
|
remove_background = true;
|
|
}
|
|
RemoveField::StatusText => {
|
|
unset.insert("status.text", 1);
|
|
},
|
|
RemoveField::Avatar => {
|
|
unset.insert("avatar", 1);
|
|
remove_avatar = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if let Some(status) = &data.status {
|
|
set.insert(
|
|
"status",
|
|
to_document(&status).map_err(|_| Error::DatabaseError {
|
|
operation: "to_document",
|
|
with: "status",
|
|
})?,
|
|
);
|
|
}
|
|
|
|
if let Some(profile) = data.profile {
|
|
if let Some(content) = profile.content {
|
|
set.insert("profile.content", content);
|
|
}
|
|
|
|
if let Some(attachment_id) = profile.background {
|
|
let attachment = File::find_and_use(&attachment_id, "backgrounds", "user", &user.id).await?;
|
|
set.insert(
|
|
"profile.background",
|
|
to_document(&attachment).map_err(|_| Error::DatabaseError {
|
|
operation: "to_document",
|
|
with: "attachment",
|
|
})?,
|
|
);
|
|
|
|
remove_background = true;
|
|
}
|
|
}
|
|
|
|
let avatar = std::mem::replace(&mut data.avatar, None);
|
|
let attachment = if let Some(attachment_id) = avatar {
|
|
let attachment = File::find_and_use(&attachment_id, "avatars", "user", &user.id).await?;
|
|
set.insert(
|
|
"avatar",
|
|
to_document(&attachment).map_err(|_| Error::DatabaseError {
|
|
operation: "to_document",
|
|
with: "attachment",
|
|
})?,
|
|
);
|
|
|
|
remove_avatar = true;
|
|
Some(attachment)
|
|
} else {
|
|
None
|
|
};
|
|
|
|
get_collection("users")
|
|
.update_one(doc! { "_id": &user.id }, doc! { "$set": set }, None)
|
|
.await
|
|
.map_err(|_| Error::DatabaseError {
|
|
operation: "update_one",
|
|
with: "user",
|
|
})?;
|
|
|
|
if let Some(status) = data.status {
|
|
ClientboundNotification::UserUpdate {
|
|
id: user.id.clone(),
|
|
data: json!({ "status": status }),
|
|
}
|
|
.publish(user.id.clone())
|
|
.await
|
|
.ok();
|
|
}
|
|
|
|
if let Some(avatar) = attachment {
|
|
ClientboundNotification::UserUpdate {
|
|
id: user.id.clone(),
|
|
data: json!({ "avatar": avatar }),
|
|
}
|
|
.publish(user.id.clone())
|
|
.await
|
|
.ok();
|
|
}
|
|
|
|
if remove_avatar {
|
|
if let Some(old_avatar) = user.avatar {
|
|
old_avatar.delete().await?;
|
|
}
|
|
}
|
|
|
|
if remove_background {
|
|
if let Some(profile) = user.profile {
|
|
if let Some(old_background) = profile.background {
|
|
old_background.delete().await?;
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|