Implement suggest command

This commit is contained in:
2025-12-17 14:42:14 -06:00
parent 51021e542a
commit a14c3eba0c
3 changed files with 48 additions and 6 deletions
+31 -5
View File
@@ -1,10 +1,14 @@
use matrix_sdk::{
Room,
event_handler::Ctx,
ruma::events::room::message::{OriginalSyncRoomMessageEvent, RoomMessageEventContent},
ruma::events::{
reaction::ReactionEventContent,
relation::Annotation,
room::message::{OriginalSyncRoomMessageEvent, RoomMessageEventContent},
},
};
use crate::{MagbotContext, config::MagbotStatus};
use crate::{MagbotContext, TitleSuggestion, config::MagbotStatus};
const HELP_MSG: &str = "!help - displays this help message
!showstart - starts listening for title suggestions
@@ -83,12 +87,34 @@ pub async fn help(
pub async fn suggest(
event: &OriginalSyncRoomMessageEvent,
room: &Room,
text: &str,
ctx: Ctx<MagbotContext>,
mut ctx: Ctx<MagbotContext>,
) -> anyhow::Result<()> {
// save the title into memory with author
match ctx.0.status {
MagbotStatus::Running => {
// grab the index of the first <space> character
let start_index = event
.content
.body()
.chars()
.position(|c| c == ' ')
.unwrap_or(8); // start at 8 if no space since '!suggest' is 8 chars
let suggestion_str = &event.content.body()[start_index + 1..];
let suggestion = TitleSuggestion::new(&event.sender.to_string(), suggestion_str);
ctx.0.titles.push(suggestion);
let reaction = ReactionEventContent::new(Annotation::new(
event.event_id.clone(),
"👍".to_string(),
));
room.send(reaction).await?;
}
MagbotStatus::Sleeping => {
let msg = RoomMessageEventContent::text_plain("{user} Magbot is not running");
room.send(msg).await?;
}
}
// react to the original message to show it was saved
todo!()
Ok(())
}
/// Outputs all the recorded title suggestsions
+8
View File
@@ -16,6 +16,14 @@ impl Default for MagbotStatus {
MagbotStatus::Sleeping
}
}
impl std::fmt::Display for MagbotStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self {
MagbotStatus::Running => write!(f, "Running"),
MagbotStatus::Sleeping => write!(f, "Sleeping"),
}
}
}
#[derive(Clone, Debug, Deserialize)]
pub struct MagbotConfig {
+9 -1
View File
@@ -30,6 +30,14 @@ struct TitleSuggestion {
pub(crate) user: String,
pub(crate) title: String,
}
impl TitleSuggestion {
pub(crate) fn new(author: &str, title: &str) -> Self {
Self {
user: author.to_string(),
title: title.to_string(),
}
}
}
async fn on_room_message(
event: OriginalSyncRoomMessageEvent,
@@ -62,7 +70,7 @@ async fn on_room_message(
// submit a title suggestion
s if s.to_lowercase().starts_with("!suggest") => {
commands::suggest(&event, &room, s, ctx).await?;
commands::suggest(&event, &room, ctx).await?;
}
// retrieve list of titles