Skip to content

Commit

Permalink
Support getting multiple usernames for Chat (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
hlf20010508 authored Sep 2, 2024
1 parent ade3284 commit b20b7e0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/grammers-client/src/types/chat/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ impl Channel {
self.raw.username.as_deref()
}

/// Return collectible usernames of this chat, if any.
///
/// The returned usernames do not contain the "@" prefix.
///
/// Outside of the application, people may link to this user with one of its username, such
/// as https://t.me/username.
pub fn usernames(&self) -> Vec<&str> {
self.raw
.usernames
.as_deref()
.map_or(Vec::new(), |usernames| {
usernames
.iter()
.map(|username| match username {
tl::enums::Username::Username(username) => username.username.as_ref(),
})
.collect()
})
}

/// Return the photo of this channel, if any.
pub fn photo(&self) -> Option<&tl::types::ChatPhoto> {
match &self.raw.photo {
Expand Down
31 changes: 31 additions & 0 deletions lib/grammers-client/src/types/chat/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,37 @@ impl Group {
}
}

/// Return collectible usernames of this chat, if any.
///
/// The returned usernames do not contain the "@" prefix.
///
/// Outside of the application, people may link to this user with one of its username, such
/// as https://t.me/username.
pub fn usernames(&self) -> Vec<&str> {
use tl::enums::Chat;

match &self.raw {
Chat::Empty(_) | Chat::Chat(_) | Chat::Forbidden(_) | Chat::ChannelForbidden(_) => {
Vec::new()
}
Chat::Channel(channel) => {
channel
.usernames
.as_deref()
.map_or(Vec::new(), |usernames| {
usernames
.iter()
.map(|username| match username {
tl::enums::Username::Username(username) => {
username.username.as_ref()
}
})
.collect()
})
}
}
}

// Return photo of this group, if any.
pub fn photo(&self) -> Option<&tl::types::ChatPhoto> {
match &self.raw {
Expand Down
14 changes: 14 additions & 0 deletions lib/grammers-client/src/types/chat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ impl Chat {
}
}

/// Return collectible usernames of this chat, if any.
///
/// The returned usernames do not contain the "@" prefix.
///
/// Outside of the application, people may link to this user with one of its username, such
/// as https://t.me/username.
pub fn usernames(&self) -> Vec<&str> {
match self {
Self::User(user) => user.usernames(),
Self::Group(group) => group.usernames(),
Self::Channel(channel) => channel.usernames(),
}
}

// If `Self` has `min` `access_hash`, returns a mutable reference to both `min` and `access_hash`.
//
// This serves as a way of checking "is it min?" and "update the access hash" both in one.
Expand Down
20 changes: 20 additions & 0 deletions lib/grammers-client/src/types/chat/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,26 @@ impl User {
self.raw.username.as_deref()
}

/// Return collectible usernames of this chat, if any.
///
/// The returned usernames do not contain the "@" prefix.
///
/// Outside of the application, people may link to this user with one of its username, such
/// as https://t.me/username.
pub fn usernames(&self) -> Vec<&str> {
self.raw
.usernames
.as_deref()
.map_or(Vec::new(), |usernames| {
usernames
.iter()
.map(|username| match username {
tl::enums::Username::Username(username) => username.username.as_ref(),
})
.collect()
})
}

/// Return the phone number of this user, if they are not a bot and their privacy settings
/// allow you to see it.
pub fn phone(&self) -> Option<&str> {
Expand Down

0 comments on commit b20b7e0

Please sign in to comment.