Add User::is_anonymous and User::is_channel functions

Also add documentation for `User::{full_name, mention, url}`
This commit is contained in:
Maybe Waffle 2021-12-25 04:12:59 +03:00
parent a07c9b0431
commit 2bd19a598e
2 changed files with 45 additions and 1 deletions

View file

@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Limits::messages_per_min_channel` ([#121][pr121])
- `media_group_id` field to `MediaDocument` and `MediaAudio` ([#139][pr139])
- `caption_entities` method to `InputMediaPhoto` ([#140][pr140])
- `User::is_anonymous` and `User::is_channel` functions ([#151][pr151])
[pr109]: https://github.com/teloxide/teloxide-core/pull/109
[pr116]: https://github.com/teloxide/teloxide-core/pull/116
@ -25,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[pr139]: https://github.com/teloxide/teloxide-core/pull/139
[pr140]: https://github.com/teloxide/teloxide-core/pull/140
[pr143]: https://github.com/teloxide/teloxide-core/pull/143
[pr151]: https://github.com/teloxide/teloxide-core/pull/151
### Changed

View file

@ -28,6 +28,8 @@ pub struct User {
}
impl User {
/// Returns full name of this user, ie first and last names joined with a
/// space.
pub fn full_name(&self) -> String {
match &self.last_name {
Some(last_name) => (format!("{0} {1}", self.first_name, last_name)),
@ -35,12 +37,52 @@ impl User {
}
}
/// Returns a username mention of this user. Returns `None` if
/// `self.username.is_none()`.
pub fn mention(&self) -> Option<String> {
Some(format!("@{}", self.username.as_ref()?))
}
/// Returns an URL that links to this user in the form of
/// `tg://user/?id=<...>`
pub fn url(&self) -> reqwest::Url {
reqwest::Url::parse(format!("tg://user/?id={}", self.id).as_str()).unwrap()
reqwest::Url::parse(&format!("tg://user/?id={}", self.id)).unwrap()
}
/// Returns `true` if this is special user used by telegram bot API to
/// denote an annonymous user that sends messages on behalf of a group.
pub fn is_anonymous(&self) -> bool {
// https://github.com/tdlib/td/blob/4791fb6a2af0257f6cad8396e10424a79ee5f768/td/telegram/ContactsManager.cpp#L4941-L4943
const ANON_ID: i64 = 1087968824;
// Sanity check
debug_assert!(
(self.id != ANON_ID)
|| (self.is_bot
&& self.first_name == "Group"
&& self.last_name.is_none()
&& self.username.as_deref() == Some("GroupAnonymousBot"))
);
self.id == ANON_ID
}
/// Returns `true` if this is special user used by telegram bot API to
/// denote an annonymous user that sends messages on behalf of a channel.
pub fn is_channel(&self) -> bool {
// https://github.com/tdlib/td/blob/4791fb6a2af0257f6cad8396e10424a79ee5f768/td/telegram/ContactsManager.cpp#L4945-L4947
const ANON_CHANNEL_ID: i64 = 136817688;
// Sanity check
debug_assert!(
(self.id != ANON_CHANNEL_ID)
|| (self.is_bot
&& self.first_name == "Group"
&& self.last_name.is_none()
&& self.username.as_deref() == Some("GroupAnonymousBot"))
);
self.id == ANON_CHANNEL_ID
}
}