From 6a939133c8840874824ae7ad1a7b8fcc0f59ecdf Mon Sep 17 00:00:00 2001 From: Leonid Kozarin Date: Thu, 2 Jan 2025 01:40:11 +0300 Subject: [PATCH] =?UTF-8?q?Fix=20#62=20=E2=80=94=20The=20bot=20is=20broken?= =?UTF-8?q?=20when=20a=20user's=20name=20contains=20angel=20brackets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/domain/username.rs | 9 +++++++++ src/handlers/pvp.rs | 6 +++--- src/handlers/utils/mod.rs | 8 +++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/domain/username.rs b/src/domain/username.rs index 45dd7f5..0a75fb7 100644 --- a/src/domain/username.rs +++ b/src/domain/username.rs @@ -1,3 +1,4 @@ +use std::ops::Deref; use derive_more::{Constructor, From}; #[derive(Debug, Clone, Constructor, From, Eq, PartialEq)] @@ -22,3 +23,11 @@ impl AsRef for Username { &self.0 } } + +impl Deref for Username { + type Target = str; + + fn deref(&self) -> &Self::Target { + self.0.as_str() + } +} diff --git a/src/handlers/pvp.rs b/src/handlers/pvp.rs index 0df2757..2afbc47 100644 --- a/src/handlers/pvp.rs +++ b/src/handlers/pvp.rs @@ -143,9 +143,9 @@ pub async fn inline_handler(bot: Bot, query: InlineQuery) -> HandlerResult { Ok(()) } -pub(super) fn build_inline_keyboard_article_result(uid: UserId, lang_code: &LanguageCode, name: String, bet: u16) -> InlineQueryResult { +pub(super) fn build_inline_keyboard_article_result(uid: UserId, lang_code: &LanguageCode, name: Username, bet: u16) -> InlineQueryResult { let title = t!("inline.results.titles.pvp", locale = lang_code, bet = bet); - let text = t!("commands.pvp.results.start", locale = lang_code, name = name, bet = bet); + let text = t!("commands.pvp.results.start", locale = lang_code, name = name.escaped(), bet = bet); let content = InputMessageContent::Text(InputMessageContentText::new(text).parse_mode(ParseMode::Html)); let btn_label = t!("commands.pvp.button", locale = lang_code); let btn_data = BattleCallbackData::new(uid, bet).to_data_string(); @@ -221,7 +221,7 @@ impl From<&User> for UserInfo { fn from(value: &User) -> Self { Self { uid: value.id, - name: Username::new(utils::get_full_name(value)) + name: utils::get_full_name(value) } } } diff --git a/src/handlers/utils/mod.rs b/src/handlers/utils/mod.rs index 64497a6..99d28e0 100644 --- a/src/handlers/utils/mod.rs +++ b/src/handlers/utils/mod.rs @@ -8,11 +8,13 @@ pub use tghack::*; pub use incrementor::*; use teloxide::types::User; +use crate::domain::Username; -pub fn get_full_name(user: &User) -> String { - user.last_name.as_ref() +pub fn get_full_name(user: &User) -> Username { + let name = user.last_name.as_ref() .map(|last_name| format!("{} {}", user.first_name, last_name)) - .unwrap_or(user.first_name.clone()) + .unwrap_or(user.first_name.clone()); + Username::new(name) } pub mod date {