forked from jmug/stoatchat
Fix block user route, send correct user struct.
Add route for fetching members. Cargo fmt on accident.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use crate::{database::*, notifications::events::RemoveChannelField};
|
||||
use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::{database::*, notifications::events::RemoveChannelField};
|
||||
|
||||
use mongodb::bson::{doc, to_document};
|
||||
use rocket_contrib::json::Json;
|
||||
@@ -17,7 +17,7 @@ pub struct Data {
|
||||
description: Option<String>,
|
||||
#[validate(length(min = 1, max = 128))]
|
||||
icon: Option<String>,
|
||||
remove: Option<RemoveChannelField>
|
||||
remove: Option<RemoveChannelField>,
|
||||
}
|
||||
|
||||
#[patch("/<target>", data = "<data>")]
|
||||
@@ -26,8 +26,12 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
|
||||
data.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
if data.name.is_none() && data.description.is_none() && data.icon.is_none() && data.remove.is_none() {
|
||||
return Ok(())
|
||||
if data.name.is_none()
|
||||
&& data.description.is_none()
|
||||
&& data.icon.is_none()
|
||||
&& data.remove.is_none()
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let target = target.fetch_channel().await?;
|
||||
@@ -64,7 +68,8 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
|
||||
}
|
||||
|
||||
if let Some(attachment_id) = &data.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(
|
||||
"icon",
|
||||
to_document(&attachment).map_err(|_| Error::DatabaseError {
|
||||
@@ -72,7 +77,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
|
||||
with: "attachment",
|
||||
})?,
|
||||
);
|
||||
|
||||
|
||||
remove_icon = true;
|
||||
}
|
||||
|
||||
@@ -80,26 +85,25 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
|
||||
if set.len() > 0 {
|
||||
operations.insert("$set", &set);
|
||||
}
|
||||
|
||||
|
||||
if unset.len() > 0 {
|
||||
operations.insert("$unset", unset);
|
||||
}
|
||||
|
||||
if operations.len() > 0 {
|
||||
get_collection("channels")
|
||||
.update_one(
|
||||
doc! { "_id": &id },
|
||||
operations,
|
||||
None
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "channel" })?;
|
||||
.update_one(doc! { "_id": &id }, operations, None)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "channel",
|
||||
})?;
|
||||
}
|
||||
|
||||
ClientboundNotification::ChannelUpdate {
|
||||
id: id.clone(),
|
||||
data: json!(set),
|
||||
clear: data.remove
|
||||
clear: data.remove,
|
||||
}
|
||||
.publish(id.clone());
|
||||
|
||||
@@ -107,10 +111,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
|
||||
Message::create(
|
||||
"00000000000000000000000000".to_string(),
|
||||
id.clone(),
|
||||
Content::SystemMessage(SystemMessage::ChannelRenamed {
|
||||
name,
|
||||
by: user.id,
|
||||
}),
|
||||
Content::SystemMessage(SystemMessage::ChannelRenamed { name, by: user.id }),
|
||||
)
|
||||
.publish(&target)
|
||||
.await
|
||||
|
||||
23
src/routes/channels/fetch_members.rs
Normal file
23
src/routes/channels/fetch_members.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[get("/<target>/members")]
|
||||
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
let target = target.fetch_channel().await?;
|
||||
|
||||
let perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_channel(&target)
|
||||
.for_channel()
|
||||
.await?;
|
||||
if !perm.get_view() {
|
||||
Err(Error::MissingPermission)?
|
||||
}
|
||||
|
||||
if let Channel::Group { recipients, .. } = target {
|
||||
Ok(json!(user.fetch_multiple_users(recipients).await?))
|
||||
} else {
|
||||
Err(Error::InvalidOperation)
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ use validator::Validate;
|
||||
#[derive(Serialize, Deserialize, FromFormValue)]
|
||||
pub enum Sort {
|
||||
Latest,
|
||||
Oldest
|
||||
Oldest,
|
||||
}
|
||||
|
||||
#[derive(Validate, Serialize, Deserialize, FromForm)]
|
||||
@@ -25,7 +25,7 @@ pub struct Options {
|
||||
before: Option<String>,
|
||||
#[validate(length(min = 26, max = 26))]
|
||||
after: Option<String>,
|
||||
sort: Option<Sort>
|
||||
sort: Option<Sort>,
|
||||
}
|
||||
|
||||
#[get("/<target>/messages?<options..>")]
|
||||
@@ -54,7 +54,11 @@ pub async fn req(user: User, target: Ref, options: Form<Options>) -> Result<Json
|
||||
query.insert("_id", doc! { "$gt": after });
|
||||
}
|
||||
|
||||
let sort = if let Sort::Latest = options.sort.as_ref().unwrap_or_else(|| &Sort::Latest) { -1 } else { 1 };
|
||||
let sort = if let Sort::Latest = options.sort.as_ref().unwrap_or_else(|| &Sort::Latest) {
|
||||
-1
|
||||
} else {
|
||||
1
|
||||
};
|
||||
let mut cursor = get_collection("messages")
|
||||
.find(
|
||||
query,
|
||||
|
||||
@@ -3,6 +3,7 @@ use rocket::Route;
|
||||
mod delete_channel;
|
||||
mod edit_channel;
|
||||
mod fetch_channel;
|
||||
mod fetch_members;
|
||||
mod group_add_member;
|
||||
mod group_create;
|
||||
mod group_remove_member;
|
||||
@@ -17,6 +18,7 @@ mod message_send;
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![
|
||||
fetch_channel::req,
|
||||
fetch_members::req,
|
||||
delete_channel::req,
|
||||
edit_channel::req,
|
||||
message_send::req,
|
||||
|
||||
@@ -77,14 +77,14 @@ pub async fn req(user: User, username: String) -> Result<JsonValue> {
|
||||
ClientboundNotification::UserRelationship {
|
||||
id: user.id.clone(),
|
||||
user: target_user,
|
||||
status: RelationshipStatus::Friend
|
||||
status: RelationshipStatus::Friend,
|
||||
}
|
||||
.publish(user.id.clone());
|
||||
|
||||
ClientboundNotification::UserRelationship {
|
||||
id: target_id.to_string(),
|
||||
user,
|
||||
status: RelationshipStatus::Friend
|
||||
status: RelationshipStatus::Friend,
|
||||
}
|
||||
.publish(target_id.to_string());
|
||||
|
||||
@@ -134,18 +134,18 @@ pub async fn req(user: User, username: String) -> Result<JsonValue> {
|
||||
let user = user
|
||||
.from_override(&target_user, RelationshipStatus::Incoming)
|
||||
.await?;
|
||||
|
||||
|
||||
ClientboundNotification::UserRelationship {
|
||||
id: user.id.clone(),
|
||||
user: target_user,
|
||||
status: RelationshipStatus::Outgoing
|
||||
status: RelationshipStatus::Outgoing,
|
||||
}
|
||||
.publish(user.id.clone());
|
||||
|
||||
|
||||
ClientboundNotification::UserRelationship {
|
||||
id: target_id.to_string(),
|
||||
user,
|
||||
status: RelationshipStatus::Incoming
|
||||
status: RelationshipStatus::Incoming,
|
||||
}
|
||||
.publish(target_id.to_string());
|
||||
|
||||
|
||||
@@ -75,24 +75,24 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
) {
|
||||
Ok(_) => {
|
||||
let target = target
|
||||
.from_override(&user, RelationshipStatus::Friend)
|
||||
.from_override(&user, RelationshipStatus::Blocked)
|
||||
.await?;
|
||||
let user = user
|
||||
.from_override(&target, RelationshipStatus::Friend)
|
||||
.from_override(&target, RelationshipStatus::BlockedOther)
|
||||
.await?;
|
||||
let target_id = target.id.clone();
|
||||
|
||||
ClientboundNotification::UserRelationship {
|
||||
id: user.id.clone(),
|
||||
user: target,
|
||||
status: RelationshipStatus::Blocked
|
||||
status: RelationshipStatus::Blocked,
|
||||
}
|
||||
.publish(user.id.clone());
|
||||
|
||||
ClientboundNotification::UserRelationship {
|
||||
id: target_id.clone(),
|
||||
user,
|
||||
status: RelationshipStatus::BlockedOther
|
||||
status: RelationshipStatus::BlockedOther,
|
||||
}
|
||||
.publish(target_id);
|
||||
|
||||
@@ -145,14 +145,14 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
ClientboundNotification::UserRelationship {
|
||||
id: user.id.clone(),
|
||||
user: target,
|
||||
status: RelationshipStatus::Blocked
|
||||
status: RelationshipStatus::Blocked,
|
||||
}
|
||||
.publish(user.id.clone());
|
||||
|
||||
|
||||
ClientboundNotification::UserRelationship {
|
||||
id: target_id.clone(),
|
||||
user,
|
||||
status: RelationshipStatus::BlockedOther
|
||||
status: RelationshipStatus::BlockedOther,
|
||||
}
|
||||
.publish(target_id);
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ pub async fn req(
|
||||
ClientboundNotification::UserUpdate {
|
||||
id: user.id.clone(),
|
||||
data: json!(data.0),
|
||||
clear: None
|
||||
clear: None,
|
||||
}
|
||||
.publish(user.id.clone());
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{database::*, notifications::events::RemoveUserField};
|
||||
use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::{database::*, notifications::events::RemoveUserField};
|
||||
|
||||
use mongodb::bson::{doc, to_document};
|
||||
use rocket_contrib::json::Json;
|
||||
@@ -25,7 +25,7 @@ pub struct Data {
|
||||
profile: Option<UserProfileData>,
|
||||
#[validate(length(min = 1, max = 128))]
|
||||
avatar: Option<String>,
|
||||
remove: Option<RemoveUserField>
|
||||
remove: Option<RemoveUserField>,
|
||||
}
|
||||
|
||||
#[patch("/<_ignore_id>", data = "<data>")]
|
||||
@@ -35,8 +35,12 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
|
||||
data.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
if data.status.is_none() && data.profile.is_none() && data.avatar.is_none() && data.remove.is_none() {
|
||||
return Ok(())
|
||||
if data.status.is_none()
|
||||
&& data.profile.is_none()
|
||||
&& data.avatar.is_none()
|
||||
&& data.remove.is_none()
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut unset = doc! {};
|
||||
@@ -49,14 +53,14 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
|
||||
match remove {
|
||||
RemoveUserField::ProfileContent => {
|
||||
unset.insert("profile.content", 1);
|
||||
},
|
||||
}
|
||||
RemoveUserField::ProfileBackground => {
|
||||
unset.insert("profile.background", 1);
|
||||
remove_background = true;
|
||||
}
|
||||
RemoveUserField::StatusText => {
|
||||
unset.insert("status.text", 1);
|
||||
},
|
||||
}
|
||||
RemoveUserField::Avatar => {
|
||||
unset.insert("avatar", 1);
|
||||
remove_avatar = true;
|
||||
@@ -80,7 +84,8 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
|
||||
}
|
||||
|
||||
if let Some(attachment_id) = profile.background {
|
||||
let attachment = File::find_and_use(&attachment_id, "backgrounds", "user", &user.id).await?;
|
||||
let attachment =
|
||||
File::find_and_use(&attachment_id, "backgrounds", "user", &user.id).await?;
|
||||
set.insert(
|
||||
"profile.background",
|
||||
to_document(&attachment).map_err(|_| Error::DatabaseError {
|
||||
@@ -133,7 +138,7 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
|
||||
ClientboundNotification::UserUpdate {
|
||||
id: user.id.clone(),
|
||||
data: json!({ "status": status }),
|
||||
clear: None
|
||||
clear: None,
|
||||
}
|
||||
.publish(user.id.clone());
|
||||
}
|
||||
@@ -142,7 +147,7 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
|
||||
ClientboundNotification::UserUpdate {
|
||||
id: user.id.clone(),
|
||||
data: json!({ "avatar": avatar }),
|
||||
clear: None
|
||||
clear: None,
|
||||
}
|
||||
.publish(user.id.clone());
|
||||
}
|
||||
@@ -151,7 +156,7 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
|
||||
ClientboundNotification::UserUpdate {
|
||||
id: user.id.clone(),
|
||||
data: json!({}),
|
||||
clear: Some(clear)
|
||||
clear: Some(clear),
|
||||
}
|
||||
.publish(user.id.clone());
|
||||
}
|
||||
|
||||
@@ -56,14 +56,14 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
ClientboundNotification::UserRelationship {
|
||||
id: user.id.clone(),
|
||||
user: target,
|
||||
status: RelationshipStatus::None
|
||||
status: RelationshipStatus::None,
|
||||
}
|
||||
.publish(user.id.clone());
|
||||
|
||||
ClientboundNotification::UserRelationship {
|
||||
id: target_id.clone(),
|
||||
user,
|
||||
status: RelationshipStatus::None
|
||||
status: RelationshipStatus::None,
|
||||
}
|
||||
.publish(target_id);
|
||||
|
||||
|
||||
@@ -85,14 +85,14 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
ClientboundNotification::UserRelationship {
|
||||
id: user.id.clone(),
|
||||
user: target,
|
||||
status: RelationshipStatus::None
|
||||
status: RelationshipStatus::None,
|
||||
}
|
||||
.publish(user.id.clone());
|
||||
|
||||
|
||||
ClientboundNotification::UserRelationship {
|
||||
id: target_id.clone(),
|
||||
user: user,
|
||||
status: RelationshipStatus::None
|
||||
status: RelationshipStatus::None,
|
||||
}
|
||||
.publish(target_id);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user