Strict typing for system messages; add a way to rename group.
This commit is contained in:
@@ -102,8 +102,7 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
|
||||
Message::create(
|
||||
"00000000000000000000000000".to_string(),
|
||||
id.clone(),
|
||||
// ! FIXME: make a schema for this
|
||||
format!("{{\"type\":\"user_left\",\"id\":\"{}\"}}", user.id),
|
||||
Content::SystemMessage(SystemMessage::UserLeft { id: user.id })
|
||||
)
|
||||
.publish(&target)
|
||||
.await
|
||||
|
||||
71
src/routes/channels/edit_channel.rs
Normal file
71
src/routes/channels/edit_channel.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::notifications::events::ClientboundNotification;
|
||||
|
||||
use validator::Validate;
|
||||
use rocket_contrib::json::Json;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use mongodb::bson::{doc, to_document};
|
||||
|
||||
#[derive(Validate, Serialize, Deserialize)]
|
||||
pub struct Data {
|
||||
#[validate(length(min = 1, max = 32))]
|
||||
name: Option<String>,
|
||||
#[validate(length(min = 0, max = 1024))]
|
||||
description: Option<String>,
|
||||
}
|
||||
|
||||
#[patch("/<target>", data = "<info>")]
|
||||
pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<()> {
|
||||
info.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
if info.name.is_none() && info.description.is_none() {
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
let target = target.fetch_channel().await?;
|
||||
let perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_channel(&target)
|
||||
.for_channel()
|
||||
.await?;
|
||||
|
||||
if !perm.get_manage_channel() {
|
||||
Err(Error::MissingPermission)?
|
||||
}
|
||||
|
||||
match &target {
|
||||
Channel::Group { id, .. } => {
|
||||
let col = get_collection("channels");
|
||||
col.update_one(
|
||||
doc! { "_id": &id },
|
||||
doc! { "$set": to_document(&info.0).map_err(|_| Error::DatabaseError { operation: "to_document", with: "info" })? },
|
||||
None
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "channel" })?;
|
||||
|
||||
ClientboundNotification::ChannelUpdate {
|
||||
id: id.clone(),
|
||||
data: json!(info.0)
|
||||
}
|
||||
.publish(id.clone())
|
||||
.await
|
||||
.ok();
|
||||
|
||||
if let Some(name) = &info.name {
|
||||
Message::create(
|
||||
"00000000000000000000000000".to_string(),
|
||||
id.clone(),
|
||||
Content::SystemMessage(SystemMessage::ChannelRenamed { name: name.clone(), by: user.id })
|
||||
)
|
||||
.publish(&target)
|
||||
.await
|
||||
.ok();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(Error::InvalidOperation)
|
||||
}
|
||||
}
|
||||
@@ -59,8 +59,7 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
|
||||
Message::create(
|
||||
"00000000000000000000000000".to_string(),
|
||||
id.clone(),
|
||||
// ! FIXME: make a schema for this
|
||||
format!("{{\"type\":\"user_added\",\"id\":\"{}\",\"by\":\"{}\"}}", member.id, user.id),
|
||||
Content::SystemMessage(SystemMessage::UserAdded { id: member.id, by: user.id })
|
||||
)
|
||||
.publish(&channel)
|
||||
.await
|
||||
|
||||
@@ -56,8 +56,7 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
|
||||
Message::create(
|
||||
"00000000000000000000000000".to_string(),
|
||||
id.clone(),
|
||||
// ! FIXME: make a schema for this
|
||||
format!("{{\"type\":\"user_remove\",\"id\":\"{}\",\"by\":\"{}\"}}", member.id, user.id),
|
||||
Content::SystemMessage(SystemMessage::UserRemove { id: member.id, by: user.id })
|
||||
)
|
||||
.publish(&channel)
|
||||
.await
|
||||
|
||||
@@ -115,10 +115,11 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
|
||||
channel: target.id().to_string(),
|
||||
author: user.id,
|
||||
|
||||
content: message.content.clone(),
|
||||
content: Content::Text(message.content.clone()),
|
||||
attachment,
|
||||
nonce: Some(message.nonce.clone()),
|
||||
edited: None,
|
||||
embeds: None
|
||||
};
|
||||
|
||||
msg.clone().publish(&target).await?;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use rocket::Route;
|
||||
|
||||
mod delete_channel;
|
||||
mod edit_channel;
|
||||
mod fetch_channel;
|
||||
mod group_add_member;
|
||||
mod group_create;
|
||||
@@ -17,6 +18,7 @@ pub fn routes() -> Vec<Route> {
|
||||
routes![
|
||||
fetch_channel::req,
|
||||
delete_channel::req,
|
||||
edit_channel::req,
|
||||
message_send::req,
|
||||
message_query::req,
|
||||
message_query_stale::req,
|
||||
|
||||
@@ -9,7 +9,7 @@ use rocket_contrib::json::JsonValue;
|
||||
#[get("/")]
|
||||
pub async fn root() -> JsonValue {
|
||||
json!({
|
||||
"revolt": "0.4.0-alpha.0",
|
||||
"revolt": "0.4.0-alpha.1",
|
||||
"features": {
|
||||
"registration": !*DISABLE_REGISTRATION,
|
||||
"captcha": {
|
||||
|
||||
Reference in New Issue
Block a user