Added logic to detect and remove spam
parent
5f7c263513
commit
eb710a0efe
47
src/bot.rs
47
src/bot.rs
|
@ -1,6 +1,8 @@
|
|||
#[allow(clippy::module_inception, dead_code)]
|
||||
pub mod bot {
|
||||
|
||||
use std::result;
|
||||
|
||||
use log::{info, debug};
|
||||
use matrix_sdk::{
|
||||
Client,
|
||||
|
@ -33,10 +35,7 @@ pub mod bot {
|
|||
pub async fn bot_login(creds: BotUserInfo) -> Self {
|
||||
let user_id = creds.user_id.as_str();
|
||||
let password = creds.password.as_str();
|
||||
// let homeserver = creds.homeserver.as_str();
|
||||
let room = RoomId::parse(creds.room.as_str()).unwrap();
|
||||
// let client = Client::builder().homeserver_url(homeserver).build().await.unwrap();
|
||||
|
||||
let user = UserId::parse(user_id).expect("Unable to parse bot user id");
|
||||
let server_name = ServerName::parse(user.server_name()).unwrap();
|
||||
let request_config = RequestConfig::new().force_auth();
|
||||
|
@ -147,13 +146,11 @@ pub mod bot {
|
|||
// Message edit
|
||||
if let Some((edited_msg_event_id, text)) = get_edited_message_event_text(&event){
|
||||
if bot.info.allow_swear && detect_swear_from_message(&bot.swear_list, &text) {
|
||||
let member = room.get_member(&event.sender).await.unwrap().unwrap();
|
||||
bot.delete_message_from_room(&edited_msg_event_id, "Swearing").await;
|
||||
bot.update_reputation_for_member(&member, -1).await.expect("Failed to subtract reputation from member");
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
let member = room.get_member(&event.sender).await.unwrap().unwrap();
|
||||
bot.delete_message_from_room(&edited_msg_event_id, "Swearing").await;
|
||||
bot.update_reputation_for_member(&member, -1).await.expect("Failed to subtract reputation from member");
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async fn delete_message_from_room(&self, event_id: &OwnedEventId, reason: &str) {
|
||||
|
@ -201,8 +198,34 @@ pub mod bot {
|
|||
let _ = &self.joined_room.send(content, None).await.unwrap();
|
||||
}
|
||||
|
||||
async fn detect_spam(&self, event_id: &OwnedEventId) -> bool {
|
||||
todo!()
|
||||
async fn detect_spam(&mut self, event: &OriginalSyncRoomMessageEvent) {
|
||||
let author = self.joined_room.get_member(&event.sender).await.unwrap().unwrap();
|
||||
let author_name = author.user_id().as_str().to_string();
|
||||
let curr_utc = Utc::now().timestamp();
|
||||
if self.spam_info.author_msg_times.contains_key(&author_name) {
|
||||
self.spam_info.author_msg_times.insert(author_name.clone(), vec![]);
|
||||
}
|
||||
else {
|
||||
self.spam_info.author_msg_times.get_mut(&author_name).unwrap().push(curr_utc);
|
||||
}
|
||||
|
||||
let expire_time: i64 = curr_utc - self.spam_info.detection_window;
|
||||
|
||||
let mut expired_msgs: Vec<i64> = vec![];
|
||||
for msg_time in self.spam_info.author_msg_times.get(&author_name).unwrap() {
|
||||
if msg_time < &expire_time {
|
||||
expired_msgs.push(*msg_time)
|
||||
}
|
||||
}
|
||||
|
||||
for msg in expired_msgs {
|
||||
self.spam_info.author_msg_times.get_mut(&author_name).unwrap().retain(|value| *value != msg);
|
||||
}
|
||||
|
||||
if i64::try_from(self.spam_info.author_msg_times.get(&author_name).unwrap().len()).unwrap() > self.spam_info.max_msg_per_window {
|
||||
self.delete_message_from_room(&event.event_id, "Spamming").await;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async fn detect_command(&self, event_id: &OwnedEventId) {
|
||||
|
|
|
@ -19,5 +19,5 @@ async fn main() {
|
|||
}
|
||||
let config_path = &args[0];
|
||||
let creds = BotUserInfo::get_info(config_path).unwrap();
|
||||
let bot = Bot::bot_login(creds).await;
|
||||
let mut bot = Bot::bot_login(creds).await;
|
||||
}
|
||||
|
|
|
@ -14,14 +14,14 @@ pub mod utils {
|
|||
|
||||
#[derive(Clone)]
|
||||
pub struct SpamInfo {
|
||||
pub detection_window: i32,
|
||||
pub max_msg_per_window: i32,
|
||||
pub author_msg_times: HashMap<String, Vec<i32>>,
|
||||
pub detection_window: i64,
|
||||
pub max_msg_per_window: i64,
|
||||
pub author_msg_times: HashMap<String, Vec<i64>>,
|
||||
}
|
||||
|
||||
impl SpamInfo {
|
||||
pub fn new() -> Self {
|
||||
SpamInfo { detection_window: 7, max_msg_per_window: 5, author_msg_times: HashMap::new() }
|
||||
SpamInfo { detection_window: 5, max_msg_per_window: 5, author_msg_times: HashMap::new() }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue