forked from jmug/stoatchat
Messaging: Add message replies.
Servers: Add VoiceChannel.
This commit is contained in:
@@ -105,7 +105,8 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Channel::TextChannel { .. } => {
|
||||
Channel::TextChannel { .. } |
|
||||
Channel::VoiceChannel { .. } => {
|
||||
if perm.get_manage_channel() {
|
||||
target.delete().await
|
||||
} else {
|
||||
|
||||
@@ -30,7 +30,8 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
Channel::Group { .. } => {
|
||||
unimplemented!()
|
||||
}
|
||||
Channel::TextChannel { id, server, .. } => {
|
||||
Channel::TextChannel { id, server, .. }
|
||||
| Channel::VoiceChannel { id, server, .. } => {
|
||||
Invite::Server {
|
||||
code: code.clone(),
|
||||
creator: user.id,
|
||||
|
||||
@@ -6,6 +6,7 @@ use mongodb::bson::doc;
|
||||
#[delete("/<target>/messages/<msg>")]
|
||||
pub async fn req(user: User, target: Ref, msg: Ref) -> Result<()> {
|
||||
let channel = target.fetch_channel().await?;
|
||||
channel.has_messaging()?;
|
||||
|
||||
let perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_channel(&channel)
|
||||
|
||||
@@ -19,6 +19,8 @@ pub async fn req(user: User, target: Ref, msg: Ref, edit: Json<Data>) -> Result<
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
let channel = target.fetch_channel().await?;
|
||||
channel.has_messaging()?;
|
||||
|
||||
let perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_channel(&channel)
|
||||
.for_channel()
|
||||
|
||||
@@ -6,6 +6,7 @@ use rocket_contrib::json::JsonValue;
|
||||
#[get("/<target>/messages/<msg>")]
|
||||
pub async fn req(user: User, target: Ref, msg: Ref) -> Result<JsonValue> {
|
||||
let channel = target.fetch_channel().await?;
|
||||
channel.has_messaging()?;
|
||||
|
||||
let perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_channel(&channel)
|
||||
|
||||
@@ -38,6 +38,7 @@ pub async fn req(user: User, target: Ref, options: Form<Options>) -> Result<Json
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
let target = target.fetch_channel().await?;
|
||||
target.has_messaging()?;
|
||||
|
||||
let perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_channel(&target)
|
||||
|
||||
@@ -18,6 +18,7 @@ pub async fn req(user: User, target: Ref, data: Json<Options>) -> Result<JsonVal
|
||||
}
|
||||
|
||||
let target = target.fetch_channel().await?;
|
||||
target.has_messaging()?;
|
||||
|
||||
let perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_channel(&target)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
@@ -8,6 +10,12 @@ use serde::{Deserialize, Serialize};
|
||||
use ulid::Ulid;
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Reply {
|
||||
id: String,
|
||||
mention: bool
|
||||
}
|
||||
|
||||
#[derive(Validate, Serialize, Deserialize)]
|
||||
pub struct Data {
|
||||
#[validate(length(min = 0, max = 2000))]
|
||||
@@ -17,6 +25,7 @@ pub struct Data {
|
||||
nonce: String,
|
||||
#[validate(length(min = 1, max = 128))]
|
||||
attachments: Option<Vec<String>>,
|
||||
replies: Option<Vec<Reply>>,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
@@ -25,6 +34,7 @@ lazy_static! {
|
||||
|
||||
#[post("/<target>/messages", data = "<message>")]
|
||||
pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonValue> {
|
||||
let message = message.into_inner();
|
||||
message
|
||||
.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
@@ -36,6 +46,8 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
|
||||
}
|
||||
|
||||
let target = target.fetch_channel().await?;
|
||||
target.has_messaging()?;
|
||||
|
||||
let perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_channel(&target)
|
||||
.for_channel()
|
||||
@@ -65,42 +77,64 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
|
||||
}
|
||||
|
||||
let id = Ulid::new().to_string();
|
||||
let attachments = if let Some(ids) = &message.attachments {
|
||||
|
||||
let mut mentions = HashSet::new();
|
||||
if let Some(captures) = RE_ULID.captures_iter(&message.content).next() {
|
||||
// ! FIXME: in the future, verify in group so we can send out push
|
||||
mentions.insert(captures[1].to_string());
|
||||
}
|
||||
|
||||
let mut replies = HashSet::new();
|
||||
if let Some(entries) = message.replies {
|
||||
// ! FIXME: move this to app config
|
||||
if entries.len() >= 5 {
|
||||
return Err(Error::TooManyReplies)
|
||||
}
|
||||
|
||||
for Reply { id, mention } in entries {
|
||||
let message = Ref::from_unchecked(id)
|
||||
.fetch_message(&target)
|
||||
.await?;
|
||||
|
||||
replies.insert(message.id);
|
||||
|
||||
if mention {
|
||||
mentions.insert(message.author);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut attachments = vec![];
|
||||
if let Some(ids) = &message.attachments {
|
||||
// ! FIXME: move this to app config
|
||||
if ids.len() >= 5 {
|
||||
return Err(Error::TooManyAttachments)
|
||||
}
|
||||
|
||||
let mut attachments = vec![];
|
||||
for attachment_id in ids {
|
||||
attachments
|
||||
.push(File::find_and_use(attachment_id, "attachments", "message", &id).await?);
|
||||
}
|
||||
|
||||
Some(attachments)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut mentions = vec![];
|
||||
if let Some(captures) = RE_ULID.captures_iter(&message.content).next() {
|
||||
// ! FIXME: in the future, verify in group so we can send out push
|
||||
mentions.push(captures[1].to_string());
|
||||
}
|
||||
|
||||
|
||||
let msg = Message {
|
||||
id,
|
||||
channel: target.id().to_string(),
|
||||
author: user.id,
|
||||
|
||||
content: Content::Text(message.content.clone()),
|
||||
attachments,
|
||||
nonce: Some(message.nonce.clone()),
|
||||
edited: None,
|
||||
embeds: None,
|
||||
|
||||
attachments: if attachments.len() > 0 { Some(attachments) } else { None },
|
||||
mentions: if mentions.len() > 0 {
|
||||
Some(mentions)
|
||||
Some(mentions.into_iter().collect::<Vec<String>>())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
replies: if replies.len() > 0 {
|
||||
Some(replies.into_iter().collect::<Vec<String>>())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
|
||||
@@ -7,8 +7,22 @@ use serde::{Deserialize, Serialize};
|
||||
use ulid::Ulid;
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
enum ChannelType {
|
||||
Text,
|
||||
Voice
|
||||
}
|
||||
|
||||
impl Default for ChannelType {
|
||||
fn default() -> Self {
|
||||
ChannelType::Text
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Validate, Serialize, Deserialize)]
|
||||
pub struct Data {
|
||||
#[serde(rename = "type", default = "ChannelType::default")]
|
||||
channel_type: ChannelType,
|
||||
#[validate(length(min = 1, max = 32))]
|
||||
name: String,
|
||||
#[validate(length(min = 0, max = 1024))]
|
||||
@@ -30,7 +44,7 @@ pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<JsonValue>
|
||||
.for_server()
|
||||
.await?;
|
||||
|
||||
if !perm.get_manage_server() {
|
||||
if !perm.get_manage_channels() {
|
||||
Err(Error::MissingPermission)?
|
||||
}
|
||||
|
||||
@@ -52,15 +66,26 @@ pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<JsonValue>
|
||||
}
|
||||
|
||||
let id = Ulid::new().to_string();
|
||||
let channel = Channel::TextChannel {
|
||||
id: id.clone(),
|
||||
server: target.id.clone(),
|
||||
nonce: Some(info.nonce),
|
||||
let channel = match info.channel_type {
|
||||
ChannelType::Text => Channel::TextChannel {
|
||||
id: id.clone(),
|
||||
server: target.id.clone(),
|
||||
nonce: Some(info.nonce),
|
||||
|
||||
name: info.name,
|
||||
description: info.description,
|
||||
icon: None,
|
||||
last_message: None,
|
||||
name: info.name,
|
||||
description: info.description,
|
||||
icon: None,
|
||||
last_message: None,
|
||||
},
|
||||
ChannelType::Voice => Channel::VoiceChannel {
|
||||
id: id.clone(),
|
||||
server: target.id.clone(),
|
||||
nonce: Some(info.nonce),
|
||||
|
||||
name: info.name,
|
||||
description: info.description,
|
||||
icon: None
|
||||
}
|
||||
};
|
||||
|
||||
channel.clone().publish().await?;
|
||||
|
||||
Reference in New Issue
Block a user