Skip to content

Commit

Permalink
Migrate factchecks to TDLib.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Sep 18, 2024
1 parent 6d2057c commit 940d58c
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Telegram/SourceFiles/api/api_updates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
Expand Down
50 changes: 50 additions & 0 deletions Telegram/SourceFiles/data/components/factchecks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Main::Session*> session)
: _session(session)
#if 0 // mtp
, _requestTimer([=] { request(); }) {
#endif
{
}

#if 0 // mtp
void Factchecks::requestFor(not_null<HistoryItem*> item) {
subscribeIfNotYet();

Expand Down Expand Up @@ -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<HistoryView::WebPage> Factchecks::makeMedia(
not_null<HistoryView::Message*> view,
Expand Down Expand Up @@ -154,11 +185,17 @@ bool Factchecks::canEdit(not_null<HistoryItem*> item) const {
}

bool Factchecks::canEdit() const {
#if 0 // mtp
return _session->appConfig().get<bool>(u"can_edit_factcheck"_q, false);
#endif
return _canEdit;
}

int Factchecks::lengthLimit() const {
#if 0 // mtp
return _session->appConfig().get<int>(u"factcheck_length_limit"_q, 1024);
#endif
return _lengthLimit;
}

void Factchecks::save(
Expand All @@ -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,
Expand All @@ -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<TLformattedText>()
: Api::FormattedTextToTdb(text))
)).done([=] {
done(QString());
}).fail([=](const Error &error) {
done(error.message);
}).send();
}

void Factchecks::save(
Expand Down
15 changes: 15 additions & 0 deletions Telegram/SourceFiles/data/components/factchecks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,7 +36,11 @@ class Factchecks final {
public:
explicit Factchecks(not_null<Main::Session*> session);

#if 0 // mtp
void requestFor(not_null<HistoryItem*> item);
#endif
void apply(const Tdb::TLDupdateMessageFactCheck &data);
bool apply(const Tdb::TLDupdateOption &data);
[[nodiscard]] std::unique_ptr<HistoryView::WebPage> makeMedia(
not_null<HistoryView::Message*> view,
not_null<HistoryMessageFactcheck*> factcheck);
Expand All @@ -52,16 +61,22 @@ class Factchecks final {
private:
[[nodiscard]] bool canEdit() const;

#if 0 // mtp
void subscribeIfNotYet();
void request();
#endif

const not_null<Main::Session*> _session;

#if 0 // mtp
base::Timer _requestTimer;
base::flat_set<not_null<HistoryItem*>> _pending;
std::vector<HistoryItem*> _requested;
mtpRequestId _requestId = 0;
bool _subscribed = false;
#endif
bool _canEdit = false;
int _lengthLimit = 1024;

rpl::lifetime _lifetime;

Expand Down
3 changes: 3 additions & 0 deletions Telegram/SourceFiles/history/history_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ HistoryItem::HistoryItem(
data.vunread_reactions().v);
}
applyTTL(data);
setFactcheck(FromTL(data.vfact_check()));
}

HistoryItem::HistoryItem(
Expand Down Expand Up @@ -1706,10 +1707,12 @@ void HistoryItem::setFactcheck(MessageFactcheck info) {
}
}

#if 0 // mtp
bool HistoryItem::hasUnrequestedFactcheck() const {
const auto factcheck = Get<HistoryMessageFactcheck>();
return factcheck && factcheck->data.needCheck && !factcheck->requested;
}
#endif

TextWithEntities HistoryItem::factcheckText() const {
if (const auto factcheck = Get<HistoryMessageFactcheck>()) {
Expand Down
2 changes: 2 additions & 0 deletions Telegram/SourceFiles/history/history_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ class HistoryItem final : public RuntimeComposer<HistoryItem> {
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<Data::Thread*> notificationThread() const;
Expand Down
13 changes: 13 additions & 0 deletions Telegram/SourceFiles/history/history_item_components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,7 @@ HistoryMessageLogEntryOriginal &HistoryMessageLogEntryOriginal::operator=(

HistoryMessageLogEntryOriginal::~HistoryMessageLogEntryOriginal() = default;

#if 0 // mtp
MessageFactcheck FromMTP(
not_null<HistoryItem*> item,
const tl::conditional<MTPFactCheck> &factcheck) {
Expand Down Expand Up @@ -1187,6 +1188,18 @@ MessageFactcheck FromMTP(
result.needCheck = data.is_need_check();
return result;
}
#endif

MessageFactcheck FromTL(const tl::conditional<Tdb::TLfactCheck> &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()) {
Expand Down
5 changes: 5 additions & 0 deletions Telegram/SourceFiles/history/history_item_components.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ For license and copyright information please follow this link:

namespace Tdb {
class TLmessageReplyTo;
class TLfactCheck;
} // namespace Tdb

struct WebPageData;
Expand Down Expand Up @@ -603,12 +604,16 @@ struct MessageFactcheck {
}
};

#if 0 // mtp
[[nodiscard]] MessageFactcheck FromMTP(
not_null<HistoryItem*> item,
const tl::conditional<MTPFactCheck> &factcheck);
[[nodiscard]] MessageFactcheck FromMTP(
not_null<Main::Session*> session,
const tl::conditional<MTPFactCheck> &factcheck);
#endif
[[nodiscard]] MessageFactcheck FromTL(
const tl::conditional<Tdb::TLfactCheck> &factcheck);

struct HistoryMessageFactcheck
: public RuntimeComponent<HistoryMessageFactcheck, HistoryItem> {
Expand Down
2 changes: 2 additions & 0 deletions Telegram/SourceFiles/history/view/history_view_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions Telegram/SourceFiles/main/main_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ bool Session::apply(const TLDupdateOption &update) {
if (update.vname().v == "is_premium_available") {
_premiumPossible = OptionValue<bool>(update.vvalue());
return true;
} else if (factchecks().apply(update)) {
return true;
}
return _api->apply(update);
}
Expand Down

0 comments on commit 940d58c

Please sign in to comment.