mod action;
pub(crate) mod chats;
mod dialog;
mod participant;
pub use action::ActionSender;
mod channel;
mod group;
mod peer_map;
mod permissions;
mod user;
pub use channel::Channel;
pub use chats::{AdminRightsBuilder, BannedRightsBuilder};
pub use dialog::Dialog;
use grammers_session::types::{PeerAuth, PeerId, PeerInfo, PeerRef};
use grammers_tl_types as tl;
pub use group::Group;
pub use participant::{Participant, Role};
pub use peer_map::PeerMap;
pub use permissions::{Permissions, Restrictions};
pub use user::{Platform, RestrictionReason, User};
use crate::{Client, media::ChatPhoto};
#[derive(Clone, Debug)]
pub enum Peer {
User(User),
Group(Group),
Channel(Channel),
}
impl Peer {
pub(crate) fn from_user(client: &Client, user: tl::enums::User) -> Self {
Self::User(User::from_raw(client, user))
}
pub fn from_raw(client: &Client, chat: tl::enums::Chat) -> Self {
use tl::enums::Chat as C;
match chat {
C::Empty(_) | C::Chat(_) | C::Forbidden(_) => {
Self::Group(Group::from_raw(client, chat))
}
C::Channel(ref channel) => {
if channel.broadcast {
Self::Channel(Channel::from_raw(client, chat))
} else {
Self::Group(Group::from_raw(client, chat))
}
}
C::ChannelForbidden(ref channel) => {
if channel.broadcast {
Self::Channel(Channel::from_raw(client, chat))
} else {
Self::Group(Group::from_raw(client, chat))
}
}
}
}
pub fn id(&self) -> PeerId {
match self {
Self::User(user) => user.id(),
Self::Group(group) => group.id(),
Self::Channel(channel) => channel.id(),
}
}
pub(crate) fn auth(&self) -> Option<PeerAuth> {
match self {
Self::User(user) => user.auth(),
Self::Group(group) => group.auth(),
Self::Channel(channel) => channel.auth(),
}
}
pub async fn to_ref(&self) -> Option<PeerRef> {
match self {
Self::User(user) => user.to_ref().await,
Self::Group(group) => group.to_ref().await,
Self::Channel(channel) => channel.to_ref().await,
}
}
pub fn name(&self) -> Option<&str> {
match self {
Self::User(user) => user.first_name(),
Self::Group(group) => group.title(),
Self::Channel(channel) => Some(channel.title()),
}
}
pub fn username(&self) -> Option<&str> {
match self {
Self::User(user) => user.username(),
Self::Group(group) => group.username(),
Self::Channel(channel) => channel.username(),
}
}
pub fn usernames(&self) -> Vec<&str> {
match self {
Self::User(user) => user.usernames(),
Self::Group(group) => group.usernames(),
Self::Channel(channel) => channel.usernames(),
}
}
pub async fn photo(&self, big: bool) -> Option<ChatPhoto> {
let peer = self.to_ref().await?.into();
match self {
Self::User(user) => user.photo().map(|x| ChatPhoto {
raw: tl::enums::InputFileLocation::InputPeerPhotoFileLocation(
tl::types::InputPeerPhotoFileLocation {
big,
peer,
photo_id: x.photo_id,
},
),
}),
Self::Group(group) => group.photo().map(|x| ChatPhoto {
raw: tl::enums::InputFileLocation::InputPeerPhotoFileLocation(
tl::types::InputPeerPhotoFileLocation {
big,
peer,
photo_id: x.photo_id,
},
),
}),
Self::Channel(channel) => channel.photo().map(|x| ChatPhoto {
raw: tl::enums::InputFileLocation::InputPeerPhotoFileLocation(
tl::types::InputPeerPhotoFileLocation {
big,
peer,
photo_id: x.photo_id,
},
),
}),
}
}
}
impl From<Peer> for PeerInfo {
#[inline]
fn from(peer: Peer) -> Self {
<Self as From<&Peer>>::from(&peer)
}
}
impl<'a> From<&'a Peer> for PeerInfo {
fn from(peer: &'a Peer) -> Self {
match peer {
Peer::User(user) => <PeerInfo as From<&'a User>>::from(user),
Peer::Group(group) => <PeerInfo as From<&'a Group>>::from(group),
Peer::Channel(channel) => <PeerInfo as From<&'a Channel>>::from(channel),
}
}
}