Messaging: Parse mentions.

This commit is contained in:
Paul
2021-06-11 18:07:37 +01:00
parent 0f18a6781d
commit 78c890aa59
6 changed files with 23 additions and 2 deletions

View File

@@ -59,10 +59,12 @@ pub struct Message {
pub edited: Option<DateTime>,
#[serde(skip_serializing_if = "Option::is_none")]
pub embeds: Option<Vec<Embed>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mentions: Option<Vec<String>>
}
impl Message {
pub fn create(author: String, channel: String, content: Content) -> Message {
pub fn create(author: String, channel: String, content: Content, mentions: Option<Vec<String>>) -> Message {
Message {
id: Ulid::new().to_string(),
nonce: None,
@@ -73,6 +75,7 @@ impl Message {
attachments: None,
edited: None,
embeds: None,
mentions
}
}

View File

@@ -102,6 +102,7 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
"00000000000000000000000000".to_string(),
id.clone(),
Content::SystemMessage(SystemMessage::UserLeft { id: user.id }),
None
)
.publish(&target)
.await

View File

@@ -119,6 +119,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
name,
by: user.id.clone(),
}),
None
)
.publish(&target)
.await
@@ -132,6 +133,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
Content::SystemMessage(SystemMessage::ChannelDescriptionChanged {
by: user.id.clone(),
}),
None
)
.publish(&target)
.await
@@ -143,6 +145,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
"00000000000000000000000000".to_string(),
id.clone(),
Content::SystemMessage(SystemMessage::ChannelIconChanged { by: user.id }),
None
)
.publish(&target)
.await

View File

@@ -62,6 +62,7 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
id: member.id,
by: user.id,
}),
None
)
.publish(&channel)
.await

View File

@@ -58,6 +58,7 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
id: member.id,
by: user.id,
}),
None
)
.publish(&channel)
.await

View File

@@ -4,8 +4,9 @@ use crate::util::result::{Error, Result};
use mongodb::{bson::doc, options::FindOneOptions};
use rocket_contrib::json::{Json, JsonValue};
use serde::{Deserialize, Serialize};
use ulid::Ulid;
use validator::Validate;
use regex::Regex;
use ulid::Ulid;
#[derive(Validate, Serialize, Deserialize)]
pub struct Data {
@@ -18,6 +19,10 @@ pub struct Data {
attachment: Option<String>,
}
lazy_static! {
static ref RE_ULID: Regex = Regex::new(r"<@([0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26})>").unwrap();
}
#[post("/<target>/messages", data = "<message>")]
pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonValue> {
message
@@ -66,6 +71,12 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
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(),
@@ -76,6 +87,7 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
nonce: Some(message.nonce.clone()),
edited: None,
embeds: None,
mentions: if mentions.len() > 0 { Some(mentions) } else { None }
};
msg.clone().publish(&target).await?;