Skip to content

Commit

Permalink
Fix #62 — The bot is broken when a user's name contains angel brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
kozalosev committed Jan 1, 2025
1 parent 1b3a901 commit 6a93913
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/domain/username.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ops::Deref;
use derive_more::{Constructor, From};

#[derive(Debug, Clone, Constructor, From, Eq, PartialEq)]
Expand All @@ -22,3 +23,11 @@ impl AsRef<String> for Username {
&self.0
}
}

impl Deref for Username {
type Target = str;

fn deref(&self) -> &Self::Target {
self.0.as_str()
}
}
6 changes: 3 additions & 3 deletions src/handlers/pvp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/handlers/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6a93913

Please sign in to comment.