use rustigram_types::story::{InputStoryContent, Story, StoryArea};
use crate::client::BotClient;
use crate::error::Result;
use rustigram_types::message::{MessageEntity, ParseMode};
use serde::Serialize;
use std::future::{Future, IntoFuture};
use std::pin::Pin;
macro_rules! impl_into_future {
($builder:ident, $return_ty:ty, $method:literal) => {
impl IntoFuture for $builder {
type Output = Result<$return_ty>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.client.post_json($method, &self.params).await })
}
}
};
}
#[derive(Serialize)]
struct PostStoryParams {
business_connection_id: String,
content: InputStoryContent,
active_period: u32,
#[serde(skip_serializing_if = "Option::is_none")]
caption: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
parse_mode: Option<ParseMode>,
#[serde(skip_serializing_if = "Option::is_none")]
caption_entities: Option<Vec<MessageEntity>>,
#[serde(skip_serializing_if = "Option::is_none")]
areas: Option<Vec<StoryArea>>,
#[serde(skip_serializing_if = "Option::is_none")]
post_to_chat_page: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
protect_content: Option<bool>,
}
pub struct PostStory {
client: BotClient,
params: PostStoryParams,
}
impl PostStory {
pub(crate) fn new(
client: BotClient,
business_connection_id: impl Into<String>,
content: InputStoryContent,
active_period: u32,
) -> Self {
Self {
client,
params: PostStoryParams {
business_connection_id: business_connection_id.into(),
content,
active_period,
caption: None,
parse_mode: None,
caption_entities: None,
areas: None,
post_to_chat_page: None,
protect_content: None,
},
}
}
pub fn caption(mut self, c: impl Into<String>) -> Self {
self.params.caption = Some(c.into());
self
}
pub fn parse_mode(mut self, m: ParseMode) -> Self {
self.params.parse_mode = Some(m);
self
}
pub fn caption_entities(mut self, e: Vec<MessageEntity>) -> Self {
self.params.caption_entities = Some(e);
self
}
pub fn areas(mut self, a: Vec<StoryArea>) -> Self {
self.params.areas = Some(a);
self
}
pub fn post_to_chat_page(mut self, v: bool) -> Self {
self.params.post_to_chat_page = Some(v);
self
}
pub fn protect_content(mut self, v: bool) -> Self {
self.params.protect_content = Some(v);
self
}
}
impl_into_future!(PostStory, Story, "postStory");
#[derive(Serialize)]
struct RepostStoryParams {
business_connection_id: String,
from_chat_id: i64,
from_story_id: i64,
active_period: u32,
#[serde(skip_serializing_if = "Option::is_none")]
post_to_chat_page: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
protect_content: Option<bool>,
}
pub struct RepostStory {
client: BotClient,
params: RepostStoryParams,
}
impl RepostStory {
pub(crate) fn new(
client: BotClient,
business_connection_id: impl Into<String>,
from_chat_id: i64,
from_story_id: i64,
active_period: u32,
) -> Self {
Self {
client,
params: RepostStoryParams {
business_connection_id: business_connection_id.into(),
from_chat_id,
from_story_id,
active_period,
post_to_chat_page: None,
protect_content: None,
},
}
}
pub fn post_to_chat_page(mut self, v: bool) -> Self {
self.params.post_to_chat_page = Some(v);
self
}
pub fn protect_content(mut self, v: bool) -> Self {
self.params.protect_content = Some(v);
self
}
}
impl_into_future!(RepostStory, Story, "repostStory");
#[derive(Serialize)]
struct EditStoryParams {
business_connection_id: String,
story_id: i64,
content: InputStoryContent,
#[serde(skip_serializing_if = "Option::is_none")]
caption: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
parse_mode: Option<ParseMode>,
#[serde(skip_serializing_if = "Option::is_none")]
caption_entities: Option<Vec<MessageEntity>>,
#[serde(skip_serializing_if = "Option::is_none")]
areas: Option<Vec<StoryArea>>,
}
pub struct EditStory {
client: BotClient,
params: EditStoryParams,
}
impl EditStory {
pub(crate) fn new(
client: BotClient,
business_connection_id: impl Into<String>,
story_id: i64,
content: InputStoryContent,
) -> Self {
Self {
client,
params: EditStoryParams {
business_connection_id: business_connection_id.into(),
story_id,
content,
caption: None,
parse_mode: None,
caption_entities: None,
areas: None,
},
}
}
pub fn caption(mut self, c: impl Into<String>) -> Self {
self.params.caption = Some(c.into());
self
}
pub fn parse_mode(mut self, m: ParseMode) -> Self {
self.params.parse_mode = Some(m);
self
}
pub fn caption_entities(mut self, e: Vec<MessageEntity>) -> Self {
self.params.caption_entities = Some(e);
self
}
pub fn areas(mut self, a: Vec<StoryArea>) -> Self {
self.params.areas = Some(a);
self
}
}
impl_into_future!(EditStory, Story, "editStory");
#[derive(Serialize)]
struct DeleteStoryParams {
business_connection_id: String,
story_id: i64,
}
pub struct DeleteStory {
client: BotClient,
params: DeleteStoryParams,
}
impl DeleteStory {
pub(crate) fn new(
client: BotClient,
business_connection_id: impl Into<String>,
story_id: i64,
) -> Self {
Self {
client,
params: DeleteStoryParams {
business_connection_id: business_connection_id.into(),
story_id,
},
}
}
}
impl_into_future!(DeleteStory, bool, "deleteStory");