diff --git a/src/command.rs b/src/command.rs index bf69c05..d8d3562 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,7 +1,10 @@ use crate::grist::{Priority, Shift}; use chrono::{DateTime, FixedOffset, TimeZone, Utc}; use clap::{CommandFactory, Parser, Subcommand}; -use matrix_sdk::{Client, Room}; +use matrix_sdk::{ + Client, Room, + ruma::events::{OriginalSyncMessageLikeEvent, room::message::RoomMessageEventContent}, +}; prelude! {} @@ -55,7 +58,6 @@ enum Action { }, // should we add an option to (un)subscribe someone else? - /// Se désinscrire d'un shift #[command(name = "désinscrire")] Unsubscribe { @@ -107,9 +109,39 @@ pub fn format_shifts(shifts: Vec) -> String { output } -pub async fn handle(command: impl AsRef, sender: OwnedUserId, room: Room, client: Client) -> Res<()> { +/// Attaches a reaction to the message corresponding to `event`. +/// +/// If sending the reaction fails, a warning is issued but not error is thrown. +pub async fn send_reaction( + room: Room, + event: impl AsRef, + reaction: impl Into, +) -> Res<()> { + use matrix_sdk::ruma::events::{reaction::ReactionEventContent, relation::Annotation}; + let event = event.as_ref(); + let reaction = reaction.into(); + let reaction_res = room + .send(ReactionEventContent::new(Annotation::new( + event.into(), + reaction, + ))) + .await; + if let Err(e) = reaction_res { + warn!("failed to send reaction, moving on...\n{}", e) + } + Ok(()) +} + +pub async fn handle( + command: impl AsRef, + event: OriginalSyncMessageLikeEvent, + room: Room, + client: Client, +) -> Res<()> { debug!("handling command: {}", command.as_ref()); + let sender = event.sender; + let args = shlex::split(command.as_ref()).ok_or_else(lazyhow!("error: Invalid quoting"))?; let mut command = Clap::command(); @@ -153,13 +185,13 @@ pub async fn handle(command: impl AsRef, sender: OwnedUserId, room: Room, c debug!("user asked for subscribe"); let _ = crate::grist::subscribe(id, sender.to_string()).await?; debug!("subscribe ok"); - // confirm with tick reaction + send_reaction(room, event.event_id, "✔️").await?; } Action::Unsubscribe { id } => { debug!("user asked for unsubscribe"); let _ = crate::grist::unsubscribe(id, sender.to_string()).await?; debug!("unsubscribe ok"); - // confirm with tick reaction + send_reaction(room, event.event_id, "✔️").await?; } } } diff --git a/src/serve/message.rs b/src/serve/message.rs index c80af65..d98ac98 100644 --- a/src/serve/message.rs +++ b/src/serve/message.rs @@ -84,7 +84,7 @@ pub async fn on_room_message( event.sender, text_content.body, body ); - command::handle(body, event.sender, room, client).await?; + command::handle(body, event.clone(), room, client).await?; debug!("message successfully sent"); Ok(())