Avoid repetition in DialogueDispatcherHandlerCx

This commit is contained in:
Temirkhan Myrzamadi 2020-05-24 01:02:17 +06:00
parent 267a1dd053
commit b0940df9bd
2 changed files with 8 additions and 160 deletions

View file

@ -99,11 +99,7 @@ where
.map(Option::unwrap_or_default);
match handler
.handle(DialogueDispatcherHandlerCx {
bot: cx.bot,
update: cx.update,
dialogue,
})
.handle(DialogueDispatcherHandlerCx { cx, dialogue })
.await
{
DialogueStage::Next(new_dialogue) => {
@ -224,7 +220,7 @@ mod tests {
|cx: DialogueDispatcherHandlerCx<MyUpdate, (), Infallible>| async move {
delay_for(Duration::from_millis(300)).await;
match cx.update {
match cx.cx.update {
MyUpdate { chat_id: 1, unique_number } => {
SEQ1.lock().await.push(unique_number);
}

View file

@ -1,15 +1,4 @@
use crate::{
dispatching::dialogue::GetChatId,
requests::{
DeleteMessage, EditMessageCaption, EditMessageText, ForwardMessage,
PinChatMessage, SendAnimation, SendAudio, SendContact, SendDocument,
SendLocation, SendMediaGroup, SendMessage, SendPhoto, SendSticker,
SendVenue, SendVideo, SendVideoNote, SendVoice,
},
types::{ChatId, ChatOrInlineMessage, InputFile, InputMedia, Message},
Bot,
};
use std::sync::Arc;
use crate::dispatching::{dialogue::GetChatId, DispatcherHandlerCx};
/// A context of a [`DialogueDispatcher`]'s message handler.
///
@ -19,15 +8,14 @@ use std::sync::Arc;
/// [`DialogueDispatcher`]: crate::dispatching::dialogue::DialogueDispatcher
#[derive(Debug)]
pub struct DialogueDispatcherHandlerCx<Upd, D, E> {
pub bot: Arc<Bot>,
pub update: Upd,
pub cx: DispatcherHandlerCx<Upd>,
pub dialogue: Result<D, E>,
}
impl<Upd, D, E> DialogueDispatcherHandlerCx<Upd, D, E> {
/// Creates a new instance with the provided fields.
pub fn new(bot: Arc<Bot>, update: Upd, dialogue: D) -> Self {
Self { bot, update, dialogue: Ok(dialogue) }
pub fn new(cx: DispatcherHandlerCx<Upd>, dialogue: D) -> Self {
Self { cx, dialogue: Ok(dialogue) }
}
/// Creates a new instance by substituting a dialogue and preserving
@ -36,11 +24,7 @@ impl<Upd, D, E> DialogueDispatcherHandlerCx<Upd, D, E> {
self,
new_dialogue: Result<Nd, Ne>,
) -> DialogueDispatcherHandlerCx<Upd, Nd, Ne> {
DialogueDispatcherHandlerCx {
bot: self.bot,
update: self.update,
dialogue: new_dialogue,
}
DialogueDispatcherHandlerCx { cx: self.cx, dialogue: new_dialogue }
}
}
@ -49,138 +33,6 @@ where
Upd: GetChatId,
{
fn chat_id(&self) -> i64 {
self.update.chat_id()
}
}
impl<D, E> DialogueDispatcherHandlerCx<Message, D, E> {
pub fn answer<T>(&self, text: T) -> SendMessage
where
T: Into<String>,
{
self.bot.send_message(self.chat_id(), text)
}
pub fn reply_to<T>(&self, text: T) -> SendMessage
where
T: Into<String>,
{
self.bot
.send_message(self.chat_id(), text)
.reply_to_message_id(self.update.id)
}
pub fn answer_photo(&self, photo: InputFile) -> SendPhoto {
self.bot.send_photo(self.update.chat.id, photo)
}
pub fn answer_audio(&self, audio: InputFile) -> SendAudio {
self.bot.send_audio(self.update.chat.id, audio)
}
pub fn answer_animation(&self, animation: InputFile) -> SendAnimation {
self.bot.send_animation(self.update.chat.id, animation)
}
pub fn answer_document(&self, document: InputFile) -> SendDocument {
self.bot.send_document(self.update.chat.id, document)
}
pub fn answer_video(&self, video: InputFile) -> SendVideo {
self.bot.send_video(self.update.chat.id, video)
}
pub fn answer_voice(&self, voice: InputFile) -> SendVoice {
self.bot.send_voice(self.update.chat.id, voice)
}
pub fn answer_media_group<T>(&self, media_group: T) -> SendMediaGroup
where
T: Into<Vec<InputMedia>>,
{
self.bot.send_media_group(self.update.chat.id, media_group)
}
pub fn answer_location(
&self,
latitude: f32,
longitude: f32,
) -> SendLocation {
self.bot.send_location(self.update.chat.id, latitude, longitude)
}
pub fn answer_venue<T, U>(
&self,
latitude: f32,
longitude: f32,
title: T,
address: U,
) -> SendVenue
where
T: Into<String>,
U: Into<String>,
{
self.bot.send_venue(
self.update.chat.id,
latitude,
longitude,
title,
address,
)
}
pub fn answer_video_note(&self, video_note: InputFile) -> SendVideoNote {
self.bot.send_video_note(self.update.chat.id, video_note)
}
pub fn answer_contact<T, U>(
&self,
phone_number: T,
first_name: U,
) -> SendContact
where
T: Into<String>,
U: Into<String>,
{
self.bot.send_contact(self.chat_id(), phone_number, first_name)
}
pub fn answer_sticker<T>(&self, sticker: InputFile) -> SendSticker {
self.bot.send_sticker(self.update.chat.id, sticker)
}
pub fn forward_to<T>(&self, chat_id: T) -> ForwardMessage
where
T: Into<ChatId>,
{
self.bot.forward_message(chat_id, self.update.chat.id, self.update.id)
}
pub fn edit_message_text<T>(&self, text: T) -> EditMessageText
where
T: Into<String>,
{
self.bot.edit_message_text(
ChatOrInlineMessage::Chat {
chat_id: self.update.chat.id.into(),
message_id: self.update.id,
},
text,
)
}
pub fn edit_message_caption(&self) -> EditMessageCaption {
self.bot.edit_message_caption(ChatOrInlineMessage::Chat {
chat_id: self.update.chat.id.into(),
message_id: self.update.id,
})
}
pub fn delete_message(&self) -> DeleteMessage {
self.bot.delete_message(self.update.chat.id, self.update.id)
}
pub fn pin_message(&self) -> PinChatMessage {
self.bot.pin_chat_message(self.update.chat.id, self.update.id)
self.cx.update.chat_id()
}
}