From 57438867b6b3c83f048772da5ce518497f11a703 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 30 Sep 2024 14:45:55 +0400 Subject: [PATCH] Respect the maximum quote length. --- Telegram/Resources/langs/lang.strings | 2 ++ .../view/controls/history_view_draft_options.cpp | 10 +++++++++- .../SourceFiles/history/view/history_view_element.cpp | 9 ++++++++- .../SourceFiles/history/view/history_view_element.h | 1 + Telegram/SourceFiles/main/main_app_config.cpp | 4 ++++ Telegram/SourceFiles/main/main_app_config.h | 2 ++ 6 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index b1ec14723..0da7bbf62 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -3611,6 +3611,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_reply_header_short" = "Reply"; "lng_reply_quote_selected" = "Quote Selected"; "lng_reply_from_private_chat" = "This reply is from a private chat."; +"lng_reply_quote_long_title" = "Quote too long!"; +"lng_reply_quote_long_text" = "The selected text is too long to quote."; "lng_link_options_header" = "Link Preview Settings"; "lng_link_header_short" = "Link"; "lng_link_move_up" = "Move Up"; diff --git a/Telegram/SourceFiles/history/view/controls/history_view_draft_options.cpp b/Telegram/SourceFiles/history/view/controls/history_view_draft_options.cpp index 5927e14cf..c680d6a23 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_draft_options.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_draft_options.cpp @@ -35,6 +35,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/buttons.h" #include "ui/widgets/discrete_sliders.h" #include "ui/painter.h" +#include "ui/toast/toast.h" #include "ui/vertical_list.h" #include "ui/ui_utility.h" #include "window/themes/window_theme.h" @@ -843,7 +844,14 @@ void DraftOptionsBox( : tr::lng_reply_quote_selected(); }) | rpl::flatten_latest(); box->addButton(std::move(save), [=] { - finish(resolveReply(), state->webpage); + if (state->quote.current().overflown) { + show->showToast({ + .title = tr::lng_reply_quote_long_title(tr::now), + .text = tr::lng_reply_quote_long_text(tr::now), + }); + } else { + finish(resolveReply(), state->webpage); + } }); box->addButton(tr::lng_cancel(), [=] { diff --git a/Telegram/SourceFiles/history/view/history_view_element.cpp b/Telegram/SourceFiles/history/view/history_view_element.cpp index 883b51a5a..1828f56ce 100644 --- a/Telegram/SourceFiles/history/view/history_view_element.cpp +++ b/Telegram/SourceFiles/history/view/history_view_element.cpp @@ -27,6 +27,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/core_settings.h" #include "core/click_handler_types.h" #include "core/ui_integration.h" +#include "main/main_app_config.h" #include "main/main_session.h" #include "chat_helpers/stickers_emoji_pack.h" #include "window/window_session_controller.h" @@ -1652,6 +1653,12 @@ SelectedQuote Element::FindSelectedQuote( if (modified.empty() || modified.to > result.text.size()) { return {}; } + const auto session = &item->history()->session(); + const auto limit = session->appConfig().quoteLengthMax(); + const auto overflown = (modified.from + limit < modified.to); + if (overflown) { + modified.to = modified.from + limit; + } result.text = result.text.mid( modified.from, modified.to - modified.from); @@ -1678,7 +1685,7 @@ SelectedQuote Element::FindSelectedQuote( ++i; } } - return { item, result, modified.from }; + return { item, result, modified.from, overflown }; } TextSelection Element::FindSelectionFromQuote( diff --git a/Telegram/SourceFiles/history/view/history_view_element.h b/Telegram/SourceFiles/history/view/history_view_element.h index 1d52d7925..d1ef6c282 100644 --- a/Telegram/SourceFiles/history/view/history_view_element.h +++ b/Telegram/SourceFiles/history/view/history_view_element.h @@ -294,6 +294,7 @@ struct SelectedQuote { HistoryItem *item = nullptr; TextWithEntities text; int offset = 0; + bool overflown = false; explicit operator bool() const { return item && !text.empty(); diff --git a/Telegram/SourceFiles/main/main_app_config.cpp b/Telegram/SourceFiles/main/main_app_config.cpp index 11e81958c..dc5f6d51f 100644 --- a/Telegram/SourceFiles/main/main_app_config.cpp +++ b/Telegram/SourceFiles/main/main_app_config.cpp @@ -38,6 +38,10 @@ void AppConfig::start() { }, _lifetime); } +int AppConfig::quoteLengthMax() const { + return get(u"quote_length_max"_q, 1024); +} + void AppConfig::refresh(bool force) { if (_requestId || !_api) { if (force) { diff --git a/Telegram/SourceFiles/main/main_app_config.h b/Telegram/SourceFiles/main/main_app_config.h index 09fe65ea1..fc02858a3 100644 --- a/Telegram/SourceFiles/main/main_app_config.h +++ b/Telegram/SourceFiles/main/main_app_config.h @@ -63,6 +63,8 @@ public: return _ignoreRestrictionChanges.events(); } + [[nodiscard]] int quoteLengthMax() const; + void refresh(bool force = false); private: