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

Send out unset in event.

parent b47067b3
Branches
No related merge requests found
Pipeline #1043 passed with stage
in 9 minutes and 36 seconds
...@@ -106,6 +106,7 @@ impl Channel { ...@@ -106,6 +106,7 @@ impl Channel {
ClientboundNotification::ChannelUpdate { ClientboundNotification::ChannelUpdate {
id: id.clone(), id: id.clone(),
data, data,
clear: None
} }
.publish(id) .publish(id)
.await .await
...@@ -195,6 +196,12 @@ impl Channel { ...@@ -195,6 +196,12 @@ impl Channel {
.publish(id.to_string()) .publish(id.to_string())
.await .await
.ok(); .ok();
if let Channel::Group { icon, .. } = self {
if let Some(attachment) = icon {
attachment.delete().await?;
}
}
Ok(()) Ok(())
} }
......
...@@ -30,6 +30,19 @@ pub enum ServerboundNotification { ...@@ -30,6 +30,19 @@ pub enum ServerboundNotification {
EndTyping { channel: String }, EndTyping { channel: String },
} }
#[derive(Serialize, Deserialize, Debug)]
pub enum RemoveUserField {
ProfileContent,
ProfileBackground,
StatusText,
Avatar
}
#[derive(Serialize, Deserialize, Debug)]
pub enum RemoveChannelField {
Icon
}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum ClientboundNotification { pub enum ClientboundNotification {
...@@ -53,6 +66,8 @@ pub enum ClientboundNotification { ...@@ -53,6 +66,8 @@ pub enum ClientboundNotification {
ChannelUpdate { ChannelUpdate {
id: String, id: String,
data: JsonValue, data: JsonValue,
#[serde(skip_serializing_if = "Option::is_none")]
clear: Option<RemoveChannelField>
}, },
ChannelGroupJoin { ChannelGroupJoin {
id: String, id: String,
...@@ -77,6 +92,8 @@ pub enum ClientboundNotification { ...@@ -77,6 +92,8 @@ pub enum ClientboundNotification {
UserUpdate { UserUpdate {
id: String, id: String,
data: JsonValue, data: JsonValue,
#[serde(skip_serializing_if = "Option::is_none")]
clear: Option<RemoveUserField>
}, },
UserRelationship { UserRelationship {
id: String, id: String,
......
use crate::database::*; use crate::{database::*, notifications::events::RemoveChannelField};
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result};
...@@ -17,15 +17,16 @@ pub struct Data { ...@@ -17,15 +17,16 @@ pub struct Data {
description: Option<String>, description: Option<String>,
#[validate(length(min = 1, max = 128))] #[validate(length(min = 1, max = 128))]
icon: Option<String>, icon: Option<String>,
remove: Option<RemoveChannelField>
} }
#[patch("/<target>", data = "<info>")] #[patch("/<target>", data = "<data>")]
pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<()> { pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
let info = info.into_inner(); let data = data.into_inner();
info.validate() data.validate()
.map_err(|error| Error::FailedValidation { error })?; .map_err(|error| Error::FailedValidation { error })?;
if info.name.is_none() && info.description.is_none() && info.icon.is_none() { if data.name.is_none() && data.description.is_none() && data.icon.is_none() && data.remove.is_none() {
return Ok(()); return Ok(());
} }
...@@ -42,16 +43,27 @@ pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<()> { ...@@ -42,16 +43,27 @@ pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<()> {
match &target { match &target {
Channel::Group { id, icon, .. } => { Channel::Group { id, icon, .. } => {
let mut set = doc! {}; let mut set = doc! {};
if let Some(name) = &info.name { let mut unset = doc! {};
let mut remove_icon = false;
if let Some(remove) = &data.remove {
match remove {
RemoveChannelField::Icon => {
unset.insert("icon", 1);
remove_icon = true;
}
}
}
if let Some(name) = &data.name {
set.insert("name", name); set.insert("name", name);
} }
if let Some(description) = info.description { if let Some(description) = &data.description {
set.insert("description", description); set.insert("description", description);
} }
let mut remove_icon = false; if let Some(attachment_id) = &data.icon {
if let Some(attachment_id) = info.icon {
let attachment = File::find_and_use(&attachment_id, "icons", "object", &user.id).await?; let attachment = File::find_and_use(&attachment_id, "icons", "object", &user.id).await?;
set.insert( set.insert(
"icon", "icon",
...@@ -64,10 +76,19 @@ pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<()> { ...@@ -64,10 +76,19 @@ pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<()> {
remove_icon = true; remove_icon = true;
} }
let mut operations = doc! {};
if set.len() > 0 {
operations.insert("$set", &set);
}
if unset.len() > 0 {
operations.insert("$unset", unset);
}
get_collection("channels") get_collection("channels")
.update_one( .update_one(
doc! { "_id": &id }, doc! { "_id": &id },
doc! { "$set": &set }, operations,
None None
) )
.await .await
...@@ -76,12 +97,13 @@ pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<()> { ...@@ -76,12 +97,13 @@ pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<()> {
ClientboundNotification::ChannelUpdate { ClientboundNotification::ChannelUpdate {
id: id.clone(), id: id.clone(),
data: json!(set), data: json!(set),
clear: data.remove
} }
.publish(id.clone()) .publish(id.clone())
.await .await
.ok(); .ok();
if let Some(name) = info.name { if let Some(name) = data.name {
Message::create( Message::create(
"00000000000000000000000000".to_string(), "00000000000000000000000000".to_string(),
id.clone(), id.clone(),
......
...@@ -58,6 +58,7 @@ pub async fn req( ...@@ -58,6 +58,7 @@ pub async fn req(
ClientboundNotification::UserUpdate { ClientboundNotification::UserUpdate {
id: user.id.clone(), id: user.id.clone(),
data: json!(data.0), data: json!(data.0),
clear: None
} }
.publish(user.id.clone()) .publish(user.id.clone())
.await .await
......
use crate::database::*; use crate::{database::*, notifications::events::RemoveUserField};
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result};
...@@ -17,14 +17,6 @@ pub struct UserProfileData { ...@@ -17,14 +17,6 @@ pub struct UserProfileData {
background: Option<String>, background: Option<String>,
} }
#[derive(Serialize, Deserialize)]
pub enum RemoveField {
ProfileContent,
ProfileBackground,
StatusText,
Avatar
}
#[derive(Validate, Serialize, Deserialize)] #[derive(Validate, Serialize, Deserialize)]
pub struct Data { pub struct Data {
#[validate] #[validate]
...@@ -33,15 +25,12 @@ pub struct Data { ...@@ -33,15 +25,12 @@ pub struct Data {
profile: Option<UserProfileData>, profile: Option<UserProfileData>,
#[validate(length(min = 1, max = 128))] #[validate(length(min = 1, max = 128))]
avatar: Option<String>, avatar: Option<String>,
remove: Option<RemoveField> remove: Option<RemoveUserField>
} }
#[patch("/<_ignore_id>", data = "<data>")] #[patch("/<_ignore_id>", data = "<data>")]
pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()> { pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()> {
let mut data = data.into_inner(); let mut data = data.into_inner();
if data.status.is_none() && data.profile.is_none() && data.avatar.is_none() {
return Ok(());
}
data.validate() data.validate()
.map_err(|error| Error::FailedValidation { error })?; .map_err(|error| Error::FailedValidation { error })?;
...@@ -52,19 +41,19 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()> ...@@ -52,19 +41,19 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
let mut remove_background = false; let mut remove_background = false;
let mut remove_avatar = false; let mut remove_avatar = false;
if let Some(remove) = data.remove { if let Some(remove) = &data.remove {
match remove { match remove {
RemoveField::ProfileContent => { RemoveUserField::ProfileContent => {
unset.insert("profile.content", 1); unset.insert("profile.content", 1);
}, },
RemoveField::ProfileBackground => { RemoveUserField::ProfileBackground => {
unset.insert("profile.background", 1); unset.insert("profile.background", 1);
remove_background = true; remove_background = true;
} }
RemoveField::StatusText => { RemoveUserField::StatusText => {
unset.insert("status.text", 1); unset.insert("status.text", 1);
}, },
RemoveField::Avatar => { RemoveUserField::Avatar => {
unset.insert("avatar", 1); unset.insert("avatar", 1);
remove_avatar = true; remove_avatar = true;
} }
...@@ -117,18 +106,28 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()> ...@@ -117,18 +106,28 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
None None
}; };
let mut operations = doc! {};
if set.len() > 0 {
operations.insert("$set", set);
}
if unset.len() > 0 {
operations.insert("$unset", unset);
}
get_collection("users") get_collection("users")
.update_one(doc! { "_id": &user.id }, doc! { "$set": set }, None) .update_one(doc! { "_id": &user.id }, operations, None)
.await .await
.map_err(|_| Error::DatabaseError { .map_err(|_| Error::DatabaseError {
operation: "update_one", operation: "update_one",
with: "user", with: "user",
})?; })?;
if let Some(status) = data.status { if let Some(status) = &data.status {
ClientboundNotification::UserUpdate { ClientboundNotification::UserUpdate {
id: user.id.clone(), id: user.id.clone(),
data: json!({ "status": status }), data: json!({ "status": status }),
clear: None
} }
.publish(user.id.clone()) .publish(user.id.clone())
.await .await
...@@ -139,6 +138,18 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()> ...@@ -139,6 +138,18 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
ClientboundNotification::UserUpdate { ClientboundNotification::UserUpdate {
id: user.id.clone(), id: user.id.clone(),
data: json!({ "avatar": avatar }), data: json!({ "avatar": avatar }),
clear: None
}
.publish(user.id.clone())
.await
.ok();
}
if let Some(clear) = data.remove {
ClientboundNotification::UserUpdate {
id: user.id.clone(),
data: json!({}),
clear: Some(clear)
} }
.publish(user.id.clone()) .publish(user.id.clone())
.await .await
......
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