Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skeleton of card cmd, without db interaction #6

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions src/cmd_carte.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use teloxide::{
dispatching::dialogue::{GetChatId, InMemStorage},
payloads::SendMessageSetters,
prelude::Dialogue,
requests::Requester,
types::{
CallbackQuery, ChatId, InlineKeyboardButton, InlineKeyboardMarkup, Message, MessageId, ReplyMarkup
},
Bot,
};

use crate::HandlerResult;

#[derive(Default, Clone, Debug)]
pub enum CardState {
#[default]
Start,
ChooseCardOption,
GiveCard {
/// ID of the message querying the target of the /carte.
/// Used to delete the message after the selection.
message_id: MessageId,
},
// ReturnCard {
// /// ID of the message.
// /// Used to delete the message after the selection.
// message_id: MessageId,
// },
}
pub type CarteDialogue = Dialogue<CardState, InMemStorage<CardState>>;


// when /carte: message with where the card is (either in office, either in CLIC or the name of the holder)
// if in CLIC, propose to give the card, or to do nothing
// if a holder have the card, propose to give back the card to the office or do nothing


/// Starts the /carte dialogue.
pub async fn start_card_dialogue(
bot: Bot,
msg: Message,
dialogue: CarteDialogue,
) -> HandlerResult {
log::info!("Starting /carte dialogue");

// log::debug!("Removing /carte message");
// bot.delete_message(msg.chat.id, msg.id).await?;

// if there is a holder:
let cartd_status = "CLIC"; // TODO get DB info from who hold the card

let is_at_office = cartd_status == "CLIC";
let (text, token) = if is_at_office {("Donner la carte", "give_card")} else {("Rendre la carte", "return_card")};

let row = vec![
InlineKeyboardButton::callback(text, token),
InlineKeyboardButton::callback("Cancel", "nothing")
];
Comment on lines +52 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to also have the possibility to "forward" the card, e.g. when a commission holds it, they can give it to another one.


log::debug!("Sending message with inline keyboard for callback");

bot
.send_message(msg.chat.id, cartd_status)
.reply_markup(ReplyMarkup::InlineKeyboard(InlineKeyboardMarkup::new(vec![row])))
.await?;

log::debug!("Updating dialogue to ChooseTarget");
dialogue
.update(CardState::ChooseCardOption)
.await?;

Ok(())
}


/// Handles the callback from the inline keyboard, and sends a message to confirm selection of option.
/// The CallbackQuery data contains the action to perform.
pub async fn choose_option(
bot: Bot,
callback_query: CallbackQuery,
dialogue: CarteDialogue,
message_id: MessageId,
) -> HandlerResult {
if let Some(id) = callback_query.chat_id() {

log::debug!("Removing option query message");
bot.delete_message(dialogue.chat_id(), message_id).await?;

log::debug!("Sending target selection message");

return match callback_query.data.unwrap_or_default().as_str() {
"give_card" => {

let msg = bot.send_message(id, "Qui prend la carte ?").await?;
dialogue.update(CardState::GiveCard { message_id: msg.id }).await?;
Ok(())
},
"return_card" => return_card(bot, id).await,
_ => Ok(()), // TODO close the dialogue
}
}

Ok(())
}


pub async fn give_card(
bot: Bot,
msg: Message,
dialogue: CarteDialogue
) -> HandlerResult {

let card_holder = msg.text();

// TODO set holder in the db
bot.send_message(dialogue.chat_id(), "{} est désormais en posséssion de la carte invité").await?;


Ok(())
}

async fn return_card(bot: Bot, chat_id: ChatId) -> HandlerResult {
bot.send_message(chat_id, "{} a rendu la carte au bureau !").await?;
Ok(()) // TODO change state in the db to bureau
}
NoeTerrier marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 12 additions & 7 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ use teloxide::{
use crate::{
cmd_authentication::{
admin_list, admin_remove, authenticate, authorizations, authorize, unauthorize
},
cmd_bureau::bureau,
cmd_poll::{
}, cmd_bureau::bureau, cmd_carte::{choose_option, give_card, start_card_dialogue, CardState}, cmd_poll::{
choose_target,
set_quote,
start_poll_dialogue,
stats, PollState
},
HandlerResult
}, HandlerResult
};

pub fn command_message_handler(
Expand All @@ -35,7 +32,8 @@ pub fn command_message_handler(
require_authorization()
.branch(dptree::case![Command::Bureau].endpoint(bureau))
.branch(dptree::case![Command::Poll].endpoint(start_poll_dialogue))
.branch(dptree::case![Command::Stats].endpoint(stats)),
.branch(dptree::case![Command::Stats].endpoint(stats))
.branch(dptree::case![Command::Carte].endpoint(start_card_dialogue)),
)
.branch(
require_admin().chain(
Expand All @@ -55,11 +53,15 @@ pub fn command_message_handler(
),
)
.branch(dptree::case![PollState::SetQuote { message_id, target }].endpoint(set_quote))
.branch(dptree::case![CardState::GiveCard { message_id }].endpoint(give_card))

}

pub fn command_callback_query_handler(
) -> Endpoint<'static, DependencyMap, HandlerResult, DpHandlerDescription> {
dptree::case![PollState::ChooseTarget { message_id }].endpoint(choose_target)
dptree::entry()
.branch(dptree::case![PollState::ChooseTarget { message_id }].endpoint(choose_target))
.branch(dptree::case![CardState::ChooseCardOption].endpoint(choose_option))
}

// ----------------------------- ACCESS CONTROL -------------------------------
Expand Down Expand Up @@ -147,6 +149,8 @@ pub enum Command {
Authorizations,
#[command(description = "(Admin) Affiche les stats des membres du comité")]
Stats,
#[command(description = "Traque la carte invité CLIC")]
Carte
}

impl Command {
Expand All @@ -163,6 +167,7 @@ impl Command {
Self::Unauthorize(..) => "unauthorize",
Self::Authorizations => "authorizations",
Self::Stats => "stats",
Self::Carte => "carte",
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod directus;
mod cmd_poll;
mod cmd_bureau;
mod cmd_authentication;
mod cmd_carte;

pub type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;

Expand Down
Loading