mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
Messaging: Parse mentions.
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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?;
|
||||
|
||||
Reference in New Issue
Block a user