Move schmfication to crate

This commit is contained in:
Jonathan Flueren 2023-07-07 11:29:57 +02:00
parent c0472b53e4
commit 37305a5465
4 changed files with 42 additions and 79 deletions

7
Cargo.lock generated
View file

@ -1374,6 +1374,7 @@ dependencies = [
"anyhow", "anyhow",
"matrix-sdk", "matrix-sdk",
"rand 0.8.5", "rand 0.8.5",
"schmfy",
"tokio", "tokio",
"tracing-subscriber", "tracing-subscriber",
] ]
@ -2238,6 +2239,12 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "schmfy"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9250f38131044f00dc302af28c08a2e121b47c6cf2ad614859604128f002c428"
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.1.0" version = "1.1.0"

View file

@ -9,5 +9,6 @@ edition = "2021"
anyhow = "1.0.71" anyhow = "1.0.71"
matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk.git", package = "matrix-sdk" } matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk.git", package = "matrix-sdk" }
rand = "0.8.5" rand = "0.8.5"
schmfy = "0.1.0"
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
tracing-subscriber = "0.3.17" tracing-subscriber = "0.3.17"

View file

@ -1,7 +1,6 @@
use std::{env, process::exit}; use std::{env, process::exit};
mod utils; use schmfy::schmfy;
use crate::utils::schmfy;
use rand::Rng; use rand::Rng;
use matrix_sdk::{ use matrix_sdk::{
@ -9,8 +8,8 @@ use matrix_sdk::{
room::Room, room::Room,
ruma::events::{room::{ ruma::events::{room::{
member::StrippedRoomMemberEvent, member::StrippedRoomMemberEvent,
message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent, OriginalRoomMessageEvent}, message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent},
}, relation::Annotation, reaction::ReactionEventContent, OriginalMessageLikeEvent}, }, relation::Annotation, reaction::ReactionEventContent},
Client, Client,
}; };
use tokio::time::{sleep, Duration}; use tokio::time::{sleep, Duration};
@ -155,7 +154,7 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
} }
if is_allowed_room(room.name()) { if is_allowed_room(room.name()) {
if text_content.body.contains("!timo") { if text_content.body.to_lowercase().contains("timo") {
let reaction = ReactionEventContent::new( let reaction = ReactionEventContent::new(
Annotation::new( Annotation::new(
event.event_id.to_owned(), event.event_id.to_owned(),
@ -165,6 +164,36 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
room.send(reaction, None).await.unwrap(); room.send(reaction, None).await.unwrap();
} }
if text_content.body.to_lowercase().contains("jan") {
let reaction = ReactionEventContent::new(
Annotation::new(
event.event_id.to_owned(),
"JAN".to_owned()
)
);
room.send(reaction, None).await.unwrap();
}
if text_content.body.to_lowercase().contains("fabian") {
let reaction = ReactionEventContent::new(
Annotation::new(
event.event_id.to_owned(),
"FABIAN".to_owned()
)
);
room.send(reaction, None).await.unwrap();
}
if text_content.body.to_lowercase().contains("second") || text_content.body.to_lowercase().contains("dennis") {
let reaction = ReactionEventContent::new(
Annotation::new(
event.event_id.to_owned(),
"SECOND".to_owned()
)
);
room.send(reaction, None).await.unwrap();
}
if event.sender.as_str().contains("conduit.rs") { if event.sender.as_str().contains("conduit.rs") {
let reaction = ReactionEventContent::new( let reaction = ReactionEventContent::new(

View file

@ -1,74 +0,0 @@
// Schmfies any String
pub fn schmfy(source: String) -> String {
if source.starts_with("schm") {
return source;
}
// if source is subsite (e.g. news/fsr), schmfy all parts separately
if source.contains('/') {
return source
.split('/')
.map(|s| schmfy(String::from(s)))
.collect::<Vec<String>>()
.join("/");
}
if source.is_empty() {
return source;
}
// schmfy first char if word is no longer than 3
if source.len() <= 3 {
let (prefix, suffix) = source.split_at(1);
let c = prefix.chars().next().unwrap_or('-');
return schmfy_char(c) + suffix;
}
// Normal words - replace prefix before first vocal
// with "schm"
let vok_pos = source
.chars()
.position(|c| "aeiouäöü".contains(c))
.unwrap_or(0);
let (_, suffix) = source.split_at(vok_pos);
String::from("schm") + suffix
}
// Schmfies single char
fn schmfy_char(c: char) -> String {
let mut ret = String::from("schm");
match c {
'a' | 'e' | 'i' | 'o' | 'u' | 'ä' | 'ö' | 'ü' => {
ret.push(c);
}
'b' | 'c' | 'd' | 'g' | 'p' | 't' | 'w' => ret.push('e'),
'f' | 'l' | 'm' | 'n' | 'r' | 's' => {
ret.push('e');
ret.push(c)
}
'h' | 'k' => ret.push('a'),
'j' => {
ret.push('o');
ret.push('t')
}
'q' => ret.push('u'),
'v' => {
ret.push('a');
ret.push('u')
}
'x' => {
ret.push('i');
ret.push('x')
}
'y' => ret.push(c),
'z' => {
ret.push('e');
ret.push('t')
}
_ => ret.push(c),
}
ret
}