Skip to content

Commit

Permalink
remove story download logic, as bots cannot access them
Browse files Browse the repository at this point in the history
  • Loading branch information
jelni committed Dec 30, 2024
1 parent e0fea46 commit 0ac9ba1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/commands/different_dimension_me.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl CommandTrait for DifferentDimensionMe {
.await?
.ok_or("send or reply to an image.")?;

let file = message_image.file()?;
let file = message_image.file();

if file.size > 4 * MEBIBYTE {
Err("the image cannot be larger than 4 MiB.")?;
Expand Down
7 changes: 3 additions & 4 deletions src/commands/gemini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ impl CommandTrait for Gemini {

if let Some(content) = message.content {
if let Some(message_image) =
telegram_utils::get_message_attachment(Cow::Owned(content), true, ctx.client_id)
.await?
telegram_utils::get_message_attachment(Cow::Owned(content), true)
{
let file = message_image.file()?;
let file = message_image.file();

if file.size > 64 * MEBIBYTE {
return Err(CommandError::Custom(
Expand All @@ -97,7 +96,7 @@ impl CommandTrait for Gemini {
&ctx.bot_state.http_client,
open_file,
file.size.try_into().unwrap(),
message_image.mime_type()?,
message_image.mime_type(),
)
.await?;

Expand Down
96 changes: 17 additions & 79 deletions src/utilities/telegram_utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::borrow::{Borrow, Cow};
use std::borrow::Cow;

use tdlib::enums::{
self, ChatMemberStatus, ChatType, InlineKeyboardButtonType, MessageContent, MessageReplyTo,
ReplyMarkup, StickerFormat, StoryContent,
ReplyMarkup, StickerFormat,
};
use tdlib::functions;
use tdlib::types::{
Expand All @@ -13,7 +13,6 @@ use tdlib::types::{

use super::cache::CompactChat;
use crate::bot::TdResult;
use crate::commands::CommandError;

pub trait MainUsername {
fn main_username(&self) -> Option<&String>;
Expand All @@ -34,13 +33,12 @@ pub enum MessageAttachment<'a> {
Video(Cow<'a, Video>),
VideoNote(Cow<'a, VideoNote>),
VoiceNote(Cow<'a, VoiceNote>),
Story(Box<Cow<'a, StoryContent>>),
ChatChangePhoto(Cow<'a, ChatPhoto>),
}

impl MessageAttachment<'_> {
pub fn file(&self) -> Result<&File, CommandError> {
let file = match self {
pub fn file(&self) -> &File {
match self {
Self::Animation(animation) => &animation.animation,
Self::Audio(audio) => &audio.audio,
Self::Document(document) => &document.document,
Expand All @@ -49,23 +47,14 @@ impl MessageAttachment<'_> {
Self::Video(video) => &video.video,
Self::VideoNote(video_note) => &video_note.video,
Self::VoiceNote(voice_note) => &voice_note.voice,
Self::Story(story) => match story.as_ref().borrow() {
enums::StoryContent::Photo(photo) => largest_photo(&photo.photo.sizes).unwrap(),
enums::StoryContent::Video(video) => &video.video.video,
enums::StoryContent::Unsupported => {
return Err(CommandError::Custom("Unsupported story type".into()))
}
},
Self::ChatChangePhoto(chat_change_photo) => {
largest_photo(&chat_change_photo.sizes).unwrap()
}
};

Ok(file)
}
}

pub fn mime_type(&self) -> Result<&str, CommandError> {
let mime_type = match self {
pub fn mime_type(&self) -> &str {
match self {
Self::Animation(animation) => &animation.mime_type,
Self::Audio(audio) => &audio.mime_type,
Self::Document(document) => &document.mime_type,
Expand All @@ -78,16 +67,7 @@ impl MessageAttachment<'_> {
Self::Video(video) => &video.mime_type,
Self::VideoNote(_) => "video/mp4",
Self::VoiceNote(voice_note) => &voice_note.mime_type,
Self::Story(story) => match story.as_ref().borrow() {
enums::StoryContent::Photo(_) => "image/jpeg",
enums::StoryContent::Video(_) => "video/mp4",
enums::StoryContent::Unsupported => {
return Err(CommandError::Custom("Unsupported story type".into()))
}
},
};

Ok(mime_type)
}
}
}

Expand All @@ -100,19 +80,16 @@ pub const fn get_message_text(content: &MessageContent) -> Option<&FormattedText
MessageContent::MessagePhoto(message) => &message.caption,
MessageContent::MessageVideo(message) => &message.caption,
MessageContent::MessageVoiceNote(message) => &message.caption,
// MessageContent::MessageStory is not supported, because it requires async calls
_ => return None,
};

Some(formatted_text)
}

#[expect(clippy::too_many_lines, reason = "this code duplication is horrible")]
pub async fn get_message_attachment(
pub fn get_message_attachment(
content: Cow<'_, MessageContent>,
include_non_images: bool,
client_id: i32,
) -> TdResult<Option<MessageAttachment<'_>>> {
) -> Option<MessageAttachment<'_>> {
let attachment = match content {
Cow::Borrowed(content) => match content {
MessageContent::MessageAnimation(message) if include_non_images => {
Expand All @@ -132,7 +109,7 @@ pub async fn get_message_attachment(
StickerFormat::Tgs | StickerFormat::Webm if include_non_images => {
MessageAttachment::Sticker(Cow::Borrowed(&message.sticker))
}
_ => return Ok(None),
_ => return None,
},
MessageContent::MessageVideo(message) if include_non_images => {
MessageAttachment::Video(Cow::Borrowed(&message.video))
Expand All @@ -143,29 +120,10 @@ pub async fn get_message_attachment(
MessageContent::MessageVoiceNote(message) if include_non_images => {
MessageAttachment::VoiceNote(Cow::Borrowed(&message.voice_note))
}
MessageContent::MessageStory(message) => {
let enums::Story::Story(story) = functions::get_story(
message.story_sender_chat_id,
message.story_id,
true,
client_id,
)
.await?;

match story.content {
enums::StoryContent::Photo(_) => {
MessageAttachment::Story(Box::new(Cow::Owned(story.content)))
}
enums::StoryContent::Video(_) if include_non_images => {
MessageAttachment::Story(Box::new(Cow::Owned(story.content)))
}
_ => return Ok(None),
}
}
MessageContent::MessageChatChangePhoto(message) => {
MessageAttachment::ChatChangePhoto(Cow::Borrowed(&message.photo))
}
_ => return Ok(None),
_ => return None,
},
Cow::Owned(content) => match content {
MessageContent::MessageAnimation(message) => {
Expand All @@ -185,7 +143,7 @@ pub async fn get_message_attachment(
StickerFormat::Tgs | StickerFormat::Webm if include_non_images => {
MessageAttachment::Sticker(Cow::Owned(message.sticker))
}
_ => return Ok(None),
_ => return None,
},
MessageContent::MessageVideo(message) => {
MessageAttachment::Video(Cow::Owned(message.video))
Expand All @@ -196,33 +154,14 @@ pub async fn get_message_attachment(
MessageContent::MessageVoiceNote(message) => {
MessageAttachment::VoiceNote(Cow::Owned(message.voice_note))
}
MessageContent::MessageStory(message) => {
let enums::Story::Story(story) = functions::get_story(
message.story_sender_chat_id,
message.story_id,
true,
client_id,
)
.await?;

match story.content {
enums::StoryContent::Photo(_) => {
MessageAttachment::Story(Box::new(Cow::Owned(story.content)))
}
enums::StoryContent::Video(_) if include_non_images => {
MessageAttachment::Story(Box::new(Cow::Owned(story.content)))
}
_ => return Ok(None),
}
}
MessageContent::MessageChatChangePhoto(message) => {
MessageAttachment::ChatChangePhoto(Cow::Owned(message.photo))
}
_ => return Ok(None),
_ => return None,
},
};

Ok(Some(attachment))
Some(attachment)
}

fn largest_photo(sizes: &[PhotoSize]) -> Option<&File> {
Expand All @@ -238,8 +177,7 @@ pub async fn get_message_or_reply_attachment(
client_id: i32,
) -> TdResult<Option<MessageAttachment>> {
if let Some(attachment) =
get_message_attachment(Cow::Borrowed(&message.content), include_non_images, client_id)
.await?
get_message_attachment(Cow::Borrowed(&message.content), include_non_images)
{
return Ok(Some(attachment));
}
Expand All @@ -260,7 +198,7 @@ pub async fn get_message_or_reply_attachment(
Cow::Owned(message.content)
};

get_message_attachment(content, include_non_images, client_id).await
Ok(get_message_attachment(content, include_non_images))
}

pub fn donate_markup(name: &str, url: impl Into<String>) -> ReplyMarkup {
Expand Down

0 comments on commit 0ac9ba1

Please sign in to comment.