Skip to content

Commit 78d79b5

Browse files
committed
Add ability to mention users without usernames
1 parent a402a26 commit 78d79b5

File tree

6 files changed

+54
-24
lines changed

6 files changed

+54
-24
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "commit-notifier"
33
authors = [ "Lin Yinfeng <[email protected]>" ]
4-
version = "0.2.0"
4+
version = "0.2.1"
55
edition = "2024"
66
description = """
77
A simple bot tracking git commits/PRs/issues/branches

src/chat/settings.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use std::collections::{BTreeMap, BTreeSet};
22

33
use serde::{Deserialize, Serialize};
4-
use teloxide::utils::markdown;
4+
use teloxide::{types::User, utils::markdown};
55
use url::Url;
66

7+
use crate::error::Error;
8+
79
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
810
pub struct ChatRepoSettings {
911
#[serde(default, alias = "pull_requests")]
@@ -73,14 +75,50 @@ impl NotifySettings {
7375
}
7476

7577
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Serialize, Deserialize)]
78+
#[serde(try_from = "SubscriberCompat")]
7679
pub enum Subscriber {
77-
Telegram { username: String },
80+
Telegram { markdown_mention: String },
81+
}
82+
83+
impl TryFrom<SubscriberCompat> for Subscriber {
84+
type Error = Error;
85+
86+
fn try_from(compat: SubscriberCompat) -> Result<Self, Self::Error> {
87+
match &compat {
88+
SubscriberCompat::Telegram {
89+
markdown_mention,
90+
username,
91+
} => match (markdown_mention, username) {
92+
(Some(mention), _) => Ok(Subscriber::Telegram {
93+
markdown_mention: mention.clone(),
94+
}),
95+
(_, Some(username)) => Ok(Subscriber::Telegram {
96+
markdown_mention: format!("@{username}"),
97+
}),
98+
(_, _) => Err(Error::InvalidSubscriber(compat)),
99+
},
100+
}
101+
}
102+
}
103+
104+
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Deserialize)]
105+
pub enum SubscriberCompat {
106+
Telegram {
107+
markdown_mention: Option<String>,
108+
username: Option<String>, // field for compatibility
109+
},
78110
}
79111

80112
impl Subscriber {
81-
fn markdown(&self) -> String {
113+
pub fn from_tg_user(u: &User) -> Self {
114+
Self::Telegram {
115+
markdown_mention: markdown::user_mention_or_link(u),
116+
}
117+
}
118+
119+
pub fn markdown(&self) -> &str {
82120
match self {
83-
Subscriber::Telegram { username } => format!("@{}", markdown::escape(username)),
121+
Subscriber::Telegram { markdown_mention } => markdown_mention,
84122
}
85123
}
86124
}

src/error.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use teloxide::types::ReplyParameters;
55
use thiserror::Error;
66
use tokio::sync::Mutex;
77

8+
use crate::chat::settings::SubscriberCompat;
89
use crate::github::GitHubInfo;
910

1011
#[derive(Error, Debug)]
@@ -104,8 +105,6 @@ pub enum Error {
104105
SubscribeCallbackNoChatId,
105106
#[error("can not determine message id from subscribe callback query")]
106107
SubscribeCallbackNoMsgId,
107-
#[error("can not determine username from subscribe callback query")]
108-
SubscribeCallbackNoUsername,
109108
#[error("can not get data from subscribe callback query")]
110109
SubscribeCallbackNoData,
111110
#[error("invalid kind '{0}' in subscribe callback data")]
@@ -118,6 +117,8 @@ pub enum Error {
118117
UnsupportedPRIssueUrl(String),
119118
#[error("not in an admin chat")]
120119
NotAdminChat,
120+
#[error("invalid subscriber: {0:?}")]
121+
InvalidSubscriber(SubscriberCompat),
121122
}
122123

123124
impl Error {

src/main.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use teloxide::types::InlineKeyboardButton;
3434
use teloxide::types::InlineKeyboardButtonKind;
3535
use teloxide::types::InlineKeyboardMarkup;
3636
use teloxide::types::ParseMode;
37+
use teloxide::types::User;
3738
use teloxide::update_listeners;
3839
use teloxide::utils::command::BotCommands;
3940
use teloxide::utils::markdown;
@@ -325,8 +326,8 @@ async fn handle_callback_query_command_result(
325326
query: &CallbackQuery,
326327
) -> Result<String, CommandError> {
327328
log::debug!("query = {query:?}");
328-
let (chat_id, username) = get_chat_id_and_username_from_query(query)?;
329-
let subscriber = Subscriber::Telegram { username };
329+
let (chat_id, user) = get_chat_id_and_user_from_query(query)?;
330+
let subscriber = Subscriber::from_tg_user(&user);
330331
let _msg = query
331332
.message
332333
.as_ref()
@@ -388,15 +389,10 @@ async fn handle_callback_query_command_result(
388389
}
389390
}
390391

391-
fn get_chat_id_and_username_from_query(query: &CallbackQuery) -> Result<(ChatId, String), Error> {
392+
fn get_chat_id_and_user_from_query(query: &CallbackQuery) -> Result<(ChatId, User), Error> {
392393
let chat_id = query.chat_id().ok_or(Error::SubscribeCallbackNoChatId)?;
393-
let username = query
394-
.from
395-
.username
396-
.as_ref()
397-
.ok_or(Error::SubscribeCallbackNoUsername)?
398-
.clone();
399-
Ok((chat_id, username))
394+
let user = query.from.clone();
395+
Ok((chat_id, user))
400396
}
401397

402398
enum CommandError {

src/message.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,5 @@ where
190190
}
191191

192192
pub fn subscriber_from_msg(msg: &Message) -> Option<Subscriber> {
193-
match &msg.from {
194-
None => None,
195-
Some(u) => u.username.as_ref().map(|name| Subscriber::Telegram {
196-
username: name.to_string(),
197-
}),
198-
}
193+
msg.from.as_ref().map(Subscriber::from_tg_user)
199194
}

0 commit comments

Comments
 (0)