2024-10-03 11:48:14 +03:00
|
|
|
use chrono::{DateTime, Utc};
|
2024-09-12 13:44:06 +03:00
|
|
|
use regex::Regex;
|
|
|
|
|
|
2024-10-01 21:06:54 +03:00
|
|
|
use mxlink::matrix_sdk::ruma::OwnedUserId;
|
2026-03-20 12:07:15 +02:00
|
|
|
use mxlink::matrix_sdk::ruma::events::room::message::{
|
|
|
|
|
FileMessageEventContent, ImageMessageEventContent,
|
|
|
|
|
};
|
2025-05-10 09:18:01 +03:00
|
|
|
use mxlink::mime::Mime;
|
2024-10-01 21:06:54 +03:00
|
|
|
|
2024-09-12 13:44:06 +03:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct MatrixMessage {
|
2024-10-01 21:06:54 +03:00
|
|
|
pub sender_id: OwnedUserId,
|
2025-05-10 09:18:01 +03:00
|
|
|
pub content: MatrixMessageContent,
|
2024-10-01 21:06:54 +03:00
|
|
|
pub mentioned_users: Vec<OwnedUserId>,
|
2024-10-03 11:48:14 +03:00
|
|
|
pub timestamp: DateTime<Utc>,
|
2024-09-12 13:44:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2025-05-10 09:18:01 +03:00
|
|
|
pub enum MatrixMessageContent {
|
|
|
|
|
Text(String),
|
|
|
|
|
Notice(String),
|
|
|
|
|
Image(ImageMessageEventContent, Mime, Vec<u8>),
|
2026-03-20 12:07:15 +02:00
|
|
|
File(FileMessageEventContent, Mime, Vec<u8>),
|
2024-09-12 13:44:06 +03:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 21:06:54 +03:00
|
|
|
#[derive(Clone)]
|
2024-09-12 13:44:06 +03:00
|
|
|
pub struct MatrixMessageProcessingParams {
|
2024-10-01 21:06:54 +03:00
|
|
|
pub(crate) bot_user_id: OwnedUserId,
|
|
|
|
|
|
|
|
|
|
/// The prefixes that will be stripped when processing the messages in the context (thread or reply chain),
|
|
|
|
|
/// which are found to be mentioning the bot user (`bot_user_id`).
|
|
|
|
|
pub(crate) bot_user_prefixes_to_strip: Vec<String>,
|
2024-09-12 13:44:06 +03:00
|
|
|
|
2024-10-01 21:06:54 +03:00
|
|
|
/// The prefixes that will be stripped when processing the 1st message in the context (thread or reply chain).
|
|
|
|
|
pub(crate) first_message_prefixes_to_strip: Vec<String>,
|
|
|
|
|
|
|
|
|
|
/// A list of users whose messages are allowed.
|
|
|
|
|
/// If None, all messages are allowed.
|
|
|
|
|
/// If Some, only messages from the allowed users (and the bot itself, `bot_user_id`) are allowed.
|
|
|
|
|
pub(crate) allowed_users: Option<Vec<Regex>>,
|
2024-09-12 13:44:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MatrixMessageProcessingParams {
|
2024-10-01 21:06:54 +03:00
|
|
|
pub fn new(bot_user_id: OwnedUserId, allowed_users: Option<Vec<Regex>>) -> Self {
|
2024-09-12 13:44:06 +03:00
|
|
|
Self {
|
|
|
|
|
bot_user_id,
|
2024-10-01 21:06:54 +03:00
|
|
|
bot_user_prefixes_to_strip: vec![],
|
|
|
|
|
|
|
|
|
|
first_message_prefixes_to_strip: vec![],
|
|
|
|
|
|
2024-09-12 13:44:06 +03:00
|
|
|
allowed_users,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 21:06:54 +03:00
|
|
|
pub fn with_bot_user_prefixes_to_strip(mut self, value: Vec<String>) -> Self {
|
|
|
|
|
self.bot_user_prefixes_to_strip = value;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn with_first_message_prefixes_to_strip(mut self, value: Vec<String>) -> Self {
|
|
|
|
|
self.first_message_prefixes_to_strip = value;
|
2024-09-12 13:44:06 +03:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|