use serde::{Deserialize, Serialize};
pub use self::{
action::*,
boost::*,
full_info::*,
id::*,
invite_link::*,
join_request::*,
location::*,
member::*,
message::*,
permissions::*,
photo::*,
sender_chat::*,
sticker_set::*,
};
use crate::{
api::{Method, Payload, PayloadError},
types::BackgroundType,
};
mod action;
mod boost;
mod full_info;
mod id;
mod invite_link;
mod join_request;
mod location;
mod member;
mod message;
mod permissions;
mod photo;
mod sender_chat;
mod sticker_set;
#[derive(Clone, Debug, derive_more::From, Deserialize, PartialEq, Serialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum Chat {
Channel(ChannelChat),
Group(GroupChat),
Private(PrivateChat),
Supergroup(SupergroupChat),
}
impl Chat {
pub fn get_id(&self) -> ChatPeerId {
match self {
Chat::Channel(chat) => chat.id,
Chat::Group(chat) => chat.id,
Chat::Private(chat) => chat.id,
Chat::Supergroup(chat) => chat.id,
}
}
pub fn get_username(&self) -> Option<&ChatUsername> {
match &self {
Chat::Channel(chat) => chat.username.as_ref(),
Chat::Group(_) => None,
Chat::Private(chat) => chat.username.as_ref(),
Chat::Supergroup(chat) => chat.username.as_ref(),
}
}
}
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ChannelChat {
pub id: ChatPeerId,
pub title: String,
pub username: Option<ChatUsername>,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct GroupChat {
pub id: ChatPeerId,
pub title: String,
}
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct PrivateChat {
pub id: ChatPeerId,
pub first_name: String,
pub last_name: Option<String>,
pub username: Option<ChatUsername>,
}
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct SupergroupChat {
pub id: ChatPeerId,
pub title: String,
pub is_direct_messages: Option<bool>,
pub is_forum: Option<bool>,
pub username: Option<ChatUsername>,
}
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct ChatBackground {
#[serde(rename = "type")]
pub background_type: BackgroundType,
}
impl From<BackgroundType> for ChatBackground {
fn from(value: BackgroundType) -> Self {
Self { background_type: value }
}
}
#[derive(Clone, Debug, Serialize)]
pub struct GetChat {
chat_id: ChatId,
}
impl GetChat {
pub fn new<T>(chat_id: T) -> Self
where
T: Into<ChatId>,
{
GetChat {
chat_id: chat_id.into(),
}
}
}
impl Method for GetChat {
type Response = ChatFullInfo;
fn into_payload(self) -> Result<Payload, PayloadError> {
Payload::json("getChat", self)
}
}
#[derive(Clone, Debug, Serialize)]
pub struct LeaveChat {
chat_id: ChatId,
}
impl LeaveChat {
pub fn new<T>(chat_id: T) -> Self
where
T: Into<ChatId>,
{
LeaveChat {
chat_id: chat_id.into(),
}
}
}
impl Method for LeaveChat {
type Response = bool;
fn into_payload(self) -> Result<Payload, PayloadError> {
Payload::json("leaveChat", self)
}
}
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct SetChatDescription {
chat_id: ChatId,
description: Option<String>,
}
impl SetChatDescription {
pub fn new<T>(chat_id: T) -> Self
where
T: Into<ChatId>,
{
SetChatDescription {
chat_id: chat_id.into(),
description: None,
}
}
pub fn with_description<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.description = Some(value.into());
self
}
}
impl Method for SetChatDescription {
type Response = bool;
fn into_payload(self) -> Result<Payload, PayloadError> {
Payload::json("setChatDescription", self)
}
}
#[derive(Clone, Debug, Serialize)]
pub struct SetChatTitle {
chat_id: ChatId,
title: String,
}
impl SetChatTitle {
pub fn new<A, B>(chat_id: A, title: B) -> Self
where
A: Into<ChatId>,
B: Into<String>,
{
SetChatTitle {
chat_id: chat_id.into(),
title: title.into(),
}
}
}
impl Method for SetChatTitle {
type Response = bool;
fn into_payload(self) -> Result<Payload, PayloadError> {
Payload::json("setChatTitle", self)
}
}