From 940d58cb6fda64a65387f1de9d7313b0dd6e7c6a Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 18 Sep 2024 13:35:20 +0400 Subject: [PATCH] Migrate factchecks to TDLib. --- Telegram/SourceFiles/api/api_updates.cpp | 3 ++ .../data/components/factchecks.cpp | 50 +++++++++++++++++++ .../SourceFiles/data/components/factchecks.h | 15 ++++++ Telegram/SourceFiles/history/history_item.cpp | 3 ++ Telegram/SourceFiles/history/history_item.h | 2 + .../history/history_item_components.cpp | 13 +++++ .../history/history_item_components.h | 5 ++ .../history/view/history_view_message.cpp | 2 + Telegram/SourceFiles/main/main_session.cpp | 2 + 9 files changed, 95 insertions(+) diff --git a/Telegram/SourceFiles/api/api_updates.cpp b/Telegram/SourceFiles/api/api_updates.cpp index 470f4fbc9a0226..09923f91d411f9 100644 --- a/Telegram/SourceFiles/api/api_updates.cpp +++ b/Telegram/SourceFiles/api/api_updates.cpp @@ -73,6 +73,7 @@ For license and copyright information please follow this link: #include "chat_helpers/stickers_emoji_pack.h" #include "boxes/peers/add_participants_box.h" #include "window/notifications_manager.h" +#include "data/components/factchecks.h" namespace Api { namespace { @@ -2863,6 +2864,8 @@ void Updates::applyUpdate(const TLupdate &update) { item->history()->unreadReactions().setCount( data.vunread_reaction_count().v); } + }, [&](const TLDupdateMessageFactCheck &data) { + _session->factchecks().apply(data); }, [&](const TLDupdateMessageLiveLocationViewed &data) { }, [&](const TLDupdateNewChat &data) { owner.processPeer(data.vchat()); diff --git a/Telegram/SourceFiles/data/components/factchecks.cpp b/Telegram/SourceFiles/data/components/factchecks.cpp index 77251925ef34bb..af6f9ad1ba38a0 100644 --- a/Telegram/SourceFiles/data/components/factchecks.cpp +++ b/Telegram/SourceFiles/data/components/factchecks.cpp @@ -22,18 +22,27 @@ For license and copyright information please follow this link: #include "main/main_session.h" #include "ui/layers/show.h" +#include "tdb/tdb_tl_scheme.h" +#include "tdb/tdb_sender.h" + namespace Data { namespace { +using namespace Tdb; + constexpr auto kRequestDelay = crl::time(1000); } // namespace Factchecks::Factchecks(not_null session) : _session(session) +#if 0 // mtp , _requestTimer([=] { request(); }) { +#endif +{ } +#if 0 // mtp void Factchecks::requestFor(not_null item) { subscribeIfNotYet(); @@ -121,6 +130,28 @@ void Factchecks::request() { } }).send(); } +#endif + +void Factchecks::apply(const TLDupdateMessageFactCheck &data) { + const auto fullId = FullMsgId{ + peerFromTdbChat(data.vchat_id()), + data.vmessage_id().v + }; + if (const auto item = _session->data().message(fullId)) { + item->setFactcheck(FromTL(&data.vfact_check())); + } +} + +bool Factchecks::apply(const Tdb::TLDupdateOption &data) { + if (data.vname().v == u"can_edit_fact_check"_q) { + _canEdit = data.vvalue().c_optionValueBoolean().vvalue().v; + } else if (data.vname().v == u"fact_check_length_max"_q) { + _lengthLimit = data.vvalue().c_optionValueInteger().vvalue().v; + } else { + return false; + } + return true; +} std::unique_ptr Factchecks::makeMedia( not_null view, @@ -154,11 +185,17 @@ bool Factchecks::canEdit(not_null item) const { } bool Factchecks::canEdit() const { +#if 0 // mtp return _session->appConfig().get(u"can_edit_factcheck"_q, false); +#endif + return _canEdit; } int Factchecks::lengthLimit() const { +#if 0 // mtp return _session->appConfig().get(u"factcheck_length_limit"_q, 1024); +#endif + return _lengthLimit; } void Factchecks::save( @@ -168,6 +205,7 @@ void Factchecks::save( const auto item = _session->data().message(itemId); if (!item) { return; +#if 0 // mtp } else if (text.empty()) { _session->api().request(MTPmessages_DeleteFactCheck( item->history()->peer->input, @@ -194,7 +232,19 @@ void Factchecks::save( }).fail([=](const MTP::Error &error) { done(error.type()); }).send(); +#endif } + _session->sender().request(TLsetMessageFactCheck( + peerToTdbChat(item->history()->peer->id), + tl_int53(item->id.bare), + (text.empty() + ? std::optional() + : Api::FormattedTextToTdb(text)) + )).done([=] { + done(QString()); + }).fail([=](const Error &error) { + done(error.message); + }).send(); } void Factchecks::save( diff --git a/Telegram/SourceFiles/data/components/factchecks.h b/Telegram/SourceFiles/data/components/factchecks.h index a7eaca3d4023a2..c2cc544ee5bc95 100644 --- a/Telegram/SourceFiles/data/components/factchecks.h +++ b/Telegram/SourceFiles/data/components/factchecks.h @@ -9,6 +9,11 @@ For license and copyright information please follow this link: #include "base/timer.h" +namespace Tdb { +class TLDupdateMessageFactCheck; +class TLDupdateOption; +} // namespace Tdb + class HistoryItem; struct HistoryMessageFactcheck; @@ -31,7 +36,11 @@ class Factchecks final { public: explicit Factchecks(not_null session); +#if 0 // mtp void requestFor(not_null item); +#endif + void apply(const Tdb::TLDupdateMessageFactCheck &data); + bool apply(const Tdb::TLDupdateOption &data); [[nodiscard]] std::unique_ptr makeMedia( not_null view, not_null factcheck); @@ -52,16 +61,22 @@ class Factchecks final { private: [[nodiscard]] bool canEdit() const; +#if 0 // mtp void subscribeIfNotYet(); void request(); +#endif const not_null _session; +#if 0 // mtp base::Timer _requestTimer; base::flat_set> _pending; std::vector _requested; mtpRequestId _requestId = 0; bool _subscribed = false; +#endif + bool _canEdit = false; + int _lengthLimit = 1024; rpl::lifetime _lifetime; diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index 3e826a257f4564..af0ec9c3ba872d 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -582,6 +582,7 @@ HistoryItem::HistoryItem( data.vunread_reactions().v); } applyTTL(data); + setFactcheck(FromTL(data.vfact_check())); } HistoryItem::HistoryItem( @@ -1706,10 +1707,12 @@ void HistoryItem::setFactcheck(MessageFactcheck info) { } } +#if 0 // mtp bool HistoryItem::hasUnrequestedFactcheck() const { const auto factcheck = Get(); return factcheck && factcheck->data.needCheck && !factcheck->requested; } +#endif TextWithEntities HistoryItem::factcheckText() const { if (const auto factcheck = Get()) { diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index 97d5355e25a1d4..678ae97d0e2a17 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -242,7 +242,9 @@ class HistoryItem final : public RuntimeComposer { const QString &label, const TextWithEntities &content); void setFactcheck(MessageFactcheck info); +#if 0 // mtp [[nodiscard]] bool hasUnrequestedFactcheck() const; +#endif [[nodiscard]] TextWithEntities factcheckText() const; [[nodiscard]] not_null notificationThread() const; diff --git a/Telegram/SourceFiles/history/history_item_components.cpp b/Telegram/SourceFiles/history/history_item_components.cpp index bf332a1d178f4b..e9b8dffd4533c6 100644 --- a/Telegram/SourceFiles/history/history_item_components.cpp +++ b/Telegram/SourceFiles/history/history_item_components.cpp @@ -1159,6 +1159,7 @@ HistoryMessageLogEntryOriginal &HistoryMessageLogEntryOriginal::operator=( HistoryMessageLogEntryOriginal::~HistoryMessageLogEntryOriginal() = default; +#if 0 // mtp MessageFactcheck FromMTP( not_null item, const tl::conditional &factcheck) { @@ -1187,6 +1188,18 @@ MessageFactcheck FromMTP( result.needCheck = data.is_need_check(); return result; } +#endif + +MessageFactcheck FromTL(const tl::conditional &factcheck) { + auto result = MessageFactcheck(); + if (!factcheck) { + return result; + } + const auto &data = factcheck->data(); + result.text = Api::FormattedTextFromTdb(data.vtext()); + result.country = data.vcountry_code().v; + return result; +} HistoryDocumentCaptioned::HistoryDocumentCaptioned() : caption(st::msgFileMinWidth - st::msgPadding.left() - st::msgPadding.right()) { diff --git a/Telegram/SourceFiles/history/history_item_components.h b/Telegram/SourceFiles/history/history_item_components.h index 8580ca0999de60..bf89918f99e633 100644 --- a/Telegram/SourceFiles/history/history_item_components.h +++ b/Telegram/SourceFiles/history/history_item_components.h @@ -17,6 +17,7 @@ For license and copyright information please follow this link: namespace Tdb { class TLmessageReplyTo; +class TLfactCheck; } // namespace Tdb struct WebPageData; @@ -603,12 +604,16 @@ struct MessageFactcheck { } }; +#if 0 // mtp [[nodiscard]] MessageFactcheck FromMTP( not_null item, const tl::conditional &factcheck); [[nodiscard]] MessageFactcheck FromMTP( not_null session, const tl::conditional &factcheck); +#endif +[[nodiscard]] MessageFactcheck FromTL( + const tl::conditional &factcheck); struct HistoryMessageFactcheck : public RuntimeComponent { diff --git a/Telegram/SourceFiles/history/view/history_view_message.cpp b/Telegram/SourceFiles/history/view/history_view_message.cpp index 49e6000a3c194e..3a07e28fc71339 100644 --- a/Telegram/SourceFiles/history/view/history_view_message.cpp +++ b/Telegram/SourceFiles/history/view/history_view_message.cpp @@ -1092,9 +1092,11 @@ void Message::draw(Painter &p, const PaintContext &context) const { const auto item = data(); const auto media = this->media(); +#if 0 // mtp if (item->hasUnrequestedFactcheck()) { item->history()->session().factchecks().requestFor(item); } +#endif const auto stm = context.messageStyle(); const auto bubble = drawBubble(); diff --git a/Telegram/SourceFiles/main/main_session.cpp b/Telegram/SourceFiles/main/main_session.cpp index 1aa9021f1ade3c..79a268693833eb 100644 --- a/Telegram/SourceFiles/main/main_session.cpp +++ b/Telegram/SourceFiles/main/main_session.cpp @@ -302,6 +302,8 @@ bool Session::apply(const TLDupdateOption &update) { if (update.vname().v == "is_premium_available") { _premiumPossible = OptionValue(update.vvalue()); return true; + } else if (factchecks().apply(update)) { + return true; } return _api->apply(update); }