From 41e46e4924c378f92ea1519fb441d2f5ae0cf374 Mon Sep 17 00:00:00 2001 From: AlexeyZavar Date: Sun, 6 Jul 2025 01:04:48 +0300 Subject: [PATCH] fix: send image as photo & cleanup --- Telegram/SourceFiles/apiwrap.cpp | 7 ++- Telegram/SourceFiles/apiwrap.h | 1 - .../ayu/features/forward/ayu_forward.cpp | 61 ++++++++++--------- .../ayu/features/forward/ayu_forward.h | 9 ++- .../ayu/features/forward/ayu_sync.cpp | 14 ++++- .../ayu/features/forward/ayu_sync.h | 29 ++++----- Telegram/SourceFiles/data/data_channel.cpp | 2 + Telegram/SourceFiles/data/data_chat.cpp | 1 + .../data/data_chat_participant_status.cpp | 5 +- Telegram/SourceFiles/data/data_peer.cpp | 1 + Telegram/SourceFiles/data/data_types.h | 2 +- Telegram/SourceFiles/history/history_item.cpp | 1 + .../SourceFiles/history/history_widget.cpp | 1 + Telegram/SourceFiles/mainwidget.cpp | 4 ++ 14 files changed, 80 insertions(+), 58 deletions(-) diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 3080d38df5..e8ba4a54cf 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -90,6 +90,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ayu/ayu_settings.h" #include "ayu/ayu_worker.h" #include "ayu/utils/telegram_helpers.h" +#include "ayu/features/forward/ayu_forward.h" namespace { @@ -3286,6 +3287,7 @@ void ApiWrap::forwardMessages( }); return; } + const auto ayuIntelligentForwardNeeded = AyuForward::isAyuForwardNeeded(draft.items); if (ayuIntelligentForwardNeeded) { crl::async([=] { @@ -3782,12 +3784,11 @@ void ApiWrap::sendMessage(MessageToSend &&message) { : Data::ForumTopic::kGeneralId; const auto topic = peer->forumTopicFor(topicRootId); - const bool canTexts = topic + const bool canSendTexts = topic ? Data::CanSendTexts(topic) : Data::CanSendTexts(peer); - if (!(canTexts || AyuForward::isForwarding((peer->id))) - || Api::SendDice(message)) { + if (!canSendTexts || AyuForward::isForwarding(peer->id) || Api::SendDice(message)) { return; } local().saveRecentSentHashtags(textWithTags.text); diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index efdd6afae6..529114b0a1 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -12,7 +12,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/sender.h" #include "data/stickers/data_stickers_set.h" #include "data/data_messages.h" -#include "ayu/features/forward/ayu_forward.h" class TaskQueue; struct MessageGroupId; diff --git a/Telegram/SourceFiles/ayu/features/forward/ayu_forward.cpp b/Telegram/SourceFiles/ayu/features/forward/ayu_forward.cpp index 7b8a1fce0c..651666b543 100644 --- a/Telegram/SourceFiles/ayu/features/forward/ayu_forward.cpp +++ b/Telegram/SourceFiles/ayu/features/forward/ayu_forward.cpp @@ -1,25 +1,28 @@ +// This is the source code of AyuGram for Desktop. +// +// We do not and cannot prevent the use of our code, +// but be respectful and credit the original author. +// +// Copyright @Radolyn, 2025 #include "ayu_forward.h" -#include -#include -#include -#include -#include #include "apiwrap.h" #include "ayu_sync.h" +#include "lang_auto.h" #include "ayu/utils/telegram_helpers.h" +#include "base/random.h" #include "base/unixtime.h" #include "core/application.h" #include "data/data_changes.h" -#include "data/data_channel.h" -#include "data/data_chat.h" #include "data/data_document.h" +#include "data/data_peer.h" #include "data/data_photo.h" #include "data/data_session.h" -#include "data/data_user.h" +#include "history/history_item.h" #include "storage/file_download.h" #include "storage/localimageloader.h" #include "storage/storage_account.h" #include "storage/storage_media_prepare.h" +#include "styles/style_boxes.h" #include "ui/chat/attach/attach_prepare.h" #include "ui/text/text_utilities.h" @@ -93,20 +96,22 @@ std::pair stateName(const PeerId &id) { return std::make_pair(status, partString); } + void ForwardState::updateBottomBar(const Main::Session &session, const PeerId *peer, const State &st) { state = st; session.changes().peerUpdated(session.data().peer(*peer), Data::PeerUpdate::Flag::Rights); } - static Ui::PreparedList prepareMedia(not_null session, const std::vector> &items, - int &i) { + int &i, + std::vector> &groupMedia) { const auto prepare = [&](not_null media) { + groupMedia.emplace_back(media); auto prepared = Ui::PreparedFile(AyuSync::filePath(session, media)); - Storage::PrepareDetails(prepared, st::sendMediaPreviewSize, 1280); + Storage::PrepareDetails(prepared, st::sendMediaPreviewSize, PhotoSideLimit()); return prepared; }; @@ -138,7 +143,8 @@ void sendMedia( not_null session, std::shared_ptr bundle, not_null primaryMedia, - Api::MessageToSend &&message) { + Api::MessageToSend &&message, + bool sendImagesAsPhotos) { if (const auto document = primaryMedia->document(); document && document->sticker()) { AyuSync::sendStickerSync(session, message, document); return; @@ -151,9 +157,8 @@ void sendMedia( return SendMediaType::Audio; } else if (document->isVideoMessage()) { return SendMediaType::Round; - } else { - return SendMediaType::File; } + return SendMediaType::File; } return SendMediaType::Photo; }(); @@ -181,17 +186,8 @@ void sendMedia( // at least try to send it as squared-video } - // workaround - auto isTherePhotos = false; - for (auto &group : bundle->groups) { - for (Ui::PreparedFile &file : group.list.files) { - if (file.type == Ui::PreparedFile::Type::Photo) { - isTherePhotos = true; - break; - } - } - } - if (mediaType == SendMediaType::File && isTherePhotos) { + // workaround for media albums consisting of video and photos + if (sendImagesAsPhotos) { mediaType = SendMediaType::Photo; } @@ -331,7 +327,7 @@ void forwardMessages( } } state->totalMessages = items.size(); - if (toBeDownloaded.size()) { + if (!toBeDownloaded.empty()) { state->state = ForwardState::State::Downloading; state->updateBottomBar(*session, &peer->id, ForwardState::State::Downloading); AyuSync::loadDocuments(session, toBeDownloaded); @@ -369,11 +365,18 @@ void forwardMessages( continue; } - auto preparedMedia = prepareMedia(session, items, i); + std::vector> groupMedia; + auto preparedMedia = prepareMedia(session, items, i, groupMedia); Ui::SendFilesWay way; way.setGroupFiles(true); - way.setSendImagesAsPhotos(true); + way.setSendImagesAsPhotos(false); + for (const auto &media2 : groupMedia) { + if (media2->photo()) { + way.setSendImagesAsPhotos(true); + break; + } + } auto groups = Ui::DivideByGroups( std::move(preparedMedia), @@ -385,7 +388,7 @@ void forwardMessages( way, message.textWithTags, false); - sendMedia(session, bundle, media, std::move(message)); + sendMedia(session, bundle, media, std::move(message), way.sendImagesAsPhotos()); } // if there are grouped messages // "i" is incremented in prepareMedia diff --git a/Telegram/SourceFiles/ayu/features/forward/ayu_forward.h b/Telegram/SourceFiles/ayu/features/forward/ayu_forward.h index f63df19eec..cd582624f4 100644 --- a/Telegram/SourceFiles/ayu/features/forward/ayu_forward.h +++ b/Telegram/SourceFiles/ayu/features/forward/ayu_forward.h @@ -1,8 +1,13 @@ +// This is the source code of AyuGram for Desktop. +// +// We do not and cannot prevent the use of our code, +// but be respectful and credit the original author. +// +// Copyright @Radolyn, 2025 #pragma once -#include
- #include "history/history.h" +#include "main/main_session.h" namespace AyuForward { bool isForwarding(const PeerId &id); diff --git a/Telegram/SourceFiles/ayu/features/forward/ayu_sync.cpp b/Telegram/SourceFiles/ayu/features/forward/ayu_sync.cpp index f71148c3a4..f8958c248b 100644 --- a/Telegram/SourceFiles/ayu/features/forward/ayu_sync.cpp +++ b/Telegram/SourceFiles/ayu/features/forward/ayu_sync.cpp @@ -1,16 +1,24 @@ +// This is the source code of AyuGram for Desktop. +// +// We do not and cannot prevent the use of our code, +// but be respectful and credit the original author. +// +// Copyright @Radolyn, 2025 #include "ayu_sync.h" - -#include "api/api_sending.h" #include "apiwrap.h" +#include "api/api_sending.h" #include "core/application.h" -#include "data/data_changes.h" +#include "core/core_settings.h" +#include "core/file_utilities.h" #include "data/data_document.h" #include "data/data_photo.h" #include "data/data_photo_media.h" #include "data/data_session.h" +#include "history/history.h" #include "history/history_item.h" #include "main/main_session.h" #include "storage/file_download_mtproto.h" +#include "storage/localimageloader.h" class TimedCountDownLatch { diff --git a/Telegram/SourceFiles/ayu/features/forward/ayu_sync.h b/Telegram/SourceFiles/ayu/features/forward/ayu_sync.h index 51e8d66740..a10c5dc023 100644 --- a/Telegram/SourceFiles/ayu/features/forward/ayu_sync.h +++ b/Telegram/SourceFiles/ayu/features/forward/ayu_sync.h @@ -1,28 +1,21 @@ +// This is the source code of AyuGram for Desktop. +// +// We do not and cannot prevent the use of our code, +// but be respectful and credit the original author. +// +// Copyright @Radolyn, 2025 #pragma once -#include "data/data_media_types.h" -#include "data/data_session.h" -#include -#include -#include -#include #include "apiwrap.h" -#include "base/unixtime.h" +#include "base/random.h" #include "data/data_document.h" +#include "data/data_media_types.h" #include "data/data_photo.h" -#include "data/data_session.h" -#include "storage/file_download_mtproto.h" -#include "storage/file_upload.h" -#include "api/api_peer_photo.h" -#include "core/application.h" -#include "core/core_settings.h" -#include "data/data_channel.h" -#include "data/data_chat.h" -#include "storage/localimageloader.h" -#include "storage/storage_media_prepare.h" -#include "ui/chat/attach/attach_prepare.h" +#include "history/history_item.h" #include "storage/file_download.h" +#include "storage/file_upload.h" #include "storage/storage_account.h" +#include "ui/chat/attach/attach_prepare.h" namespace AyuSync { diff --git a/Telegram/SourceFiles/data/data_channel.cpp b/Telegram/SourceFiles/data/data_channel.cpp index 2fef35be56..602b32cceb 100644 --- a/Telegram/SourceFiles/data/data_channel.cpp +++ b/Telegram/SourceFiles/data/data_channel.cpp @@ -619,9 +619,11 @@ bool ChannelData::canAddAdmins() const { return amCreator() || (adminRights() & AdminRight::AddAdmins); } + bool ChannelData::isAyuNoForwards() const { return flags() & Flag::AyuNoForwards; } + bool ChannelData::allowsForwarding() const { return !(flags() & Flag::NoForwards); } diff --git a/Telegram/SourceFiles/data/data_chat.cpp b/Telegram/SourceFiles/data/data_chat.cpp index 63859240a2..25a14f56a1 100644 --- a/Telegram/SourceFiles/data/data_chat.cpp +++ b/Telegram/SourceFiles/data/data_chat.cpp @@ -63,6 +63,7 @@ ChatAdminRightsInfo ChatData::defaultAdminRights(not_null user) { | Flag::ManageCall | (isCreator ? Flag::AddAdmins : Flag(0))); } + bool ChatData::isAyuNoForwards() const { return flags() & Flag::AyuNoForwards; } diff --git a/Telegram/SourceFiles/data/data_chat_participant_status.cpp b/Telegram/SourceFiles/data/data_chat_participant_status.cpp index f337c74136..1a25babb7f 100644 --- a/Telegram/SourceFiles/data/data_chat_participant_status.cpp +++ b/Telegram/SourceFiles/data/data_chat_participant_status.cpp @@ -7,7 +7,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "data/data_chat_participant_status.h" -#include "ayu/features/forward/ayu_forward.h" #include "base/unixtime.h" #include "boxes/peers/edit_peer_permissions_box.h" #include "chat_helpers/compose/compose_show.h" @@ -26,6 +25,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_session_controller.h" #include "styles/style_widgets.h" +// AyuGram includes +#include "ayu/features/forward/ayu_forward.h" + + namespace { [[nodiscard]] ChatAdminRights ChatAdminRightsFlags( diff --git a/Telegram/SourceFiles/data/data_peer.cpp b/Telegram/SourceFiles/data/data_peer.cpp index fca05dc9c3..c9436af405 100644 --- a/Telegram/SourceFiles/data/data_peer.cpp +++ b/Telegram/SourceFiles/data/data_peer.cpp @@ -1385,6 +1385,7 @@ Data::ForumTopic *PeerData::forumTopicFor(MsgId rootId) const { } return nullptr; } + bool PeerData::isAyuNoForwards() const { if (const auto user = asUser()) { return false; diff --git a/Telegram/SourceFiles/data/data_types.h b/Telegram/SourceFiles/data/data_types.h index 14a99b78d9..b87e1dcb21 100644 --- a/Telegram/SourceFiles/data/data_types.h +++ b/Telegram/SourceFiles/data/data_types.h @@ -353,7 +353,7 @@ enum class MessageFlag : uint64 { HideDisplayDate = (1ULL << 51), - AyuNoForwards = (1ULL << 63), + AyuNoForwards = (1ULL << 63), }; inline constexpr bool is_flag_type(MessageFlag) { return true; } using MessageFlags = base::flags; diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index 137705f892..7866c6fe3c 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -1766,6 +1766,7 @@ bool HistoryItem::isScheduled() const { bool HistoryItem::isSponsored() const { return _flags & MessageFlag::Sponsored; } + bool HistoryItem::isAyuNoForwards() const { return _flags & MessageFlag::AyuNoForwards; } diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index b753d50203..38c6c2cd79 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -187,6 +187,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ayu/ayu_settings.h" #include "ayu/utils/telegram_helpers.h" #include "ayu/features/messageshot/message_shot.h" +#include "ayu/features/forward/ayu_forward.h" #include "ayu/ui/boxes/message_shot_box.h" #include "boxes/abstract_box.h" diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 6c1649770b..60d6cc5299 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -96,6 +96,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include #include +// AyuGram includes +#include "ayu/features/forward/ayu_forward.h" + + namespace { void ClearBotStartToken(PeerData *peer) {