confirm subscription with a tick reaction

This commit is contained in:
Pierre de Lacroix 2026-06-30 22:49:03 +02:00
parent a208f352bc
commit db39469cad
Signed by: lateralus23
GPG key ID: 53E0CEC29C24EF39
2 changed files with 38 additions and 6 deletions

View file

@ -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<Shift>) -> String {
output
}
pub async fn handle(command: impl AsRef<str>, 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<EventId>,
reaction: impl Into<String>,
) -> 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<str>,
event: OriginalSyncMessageLikeEvent<RoomMessageEventContent>,
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<str>, 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?;
}
}
}

View file

@ -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(())