From ead695b101cab24a3154597cc8151397bb719f9e Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 30 Sep 2021 15:30:39 +0400 Subject: [PATCH] Don't pass wide fake MsgId through MTPMessage. --- .../boxes/background_preview_box.cpp | 2 +- .../SourceFiles/data/data_replies_list.cpp | 4 +-- .../data/data_scheduled_messages.cpp | 12 ++++--- Telegram/SourceFiles/data/data_session.cpp | 9 +++++ Telegram/SourceFiles/data/data_session.h | 5 +++ .../admin_log/history_admin_log_item.cpp | 35 +++++++------------ Telegram/SourceFiles/history/history.cpp | 35 +++++++++++-------- Telegram/SourceFiles/history/history.h | 7 ++-- Telegram/SourceFiles/history/history_item.cpp | 19 +++++----- Telegram/SourceFiles/history/history_item.h | 1 + .../SourceFiles/history/history_message.cpp | 6 ++-- .../SourceFiles/history/history_message.h | 2 ++ .../SourceFiles/history/history_service.cpp | 6 ++-- .../SourceFiles/history/history_service.h | 2 ++ .../settings/settings_privacy_controllers.cpp | 7 ++-- .../support/support_autocomplete.cpp | 5 ++- 16 files changed, 87 insertions(+), 70 deletions(-) diff --git a/Telegram/SourceFiles/boxes/background_preview_box.cpp b/Telegram/SourceFiles/boxes/background_preview_box.cpp index 441197117..1285000b9 100644 --- a/Telegram/SourceFiles/boxes/background_preview_box.cpp +++ b/Telegram/SourceFiles/boxes/background_preview_box.cpp @@ -295,7 +295,7 @@ bool ServiceCheck::checkRippleStartPosition(QPoint position) const { const auto viaBotId = UserId(); const auto groupedId = uint64(); const auto item = history->makeMessage( - history->owner().nextNonHistoryEntryId(), + history->nextNonHistoryEntryId(), flags, replyTo, viaBotId, diff --git a/Telegram/SourceFiles/data/data_replies_list.cpp b/Telegram/SourceFiles/data/data_replies_list.cpp index 7f47cfeab..7d348fbe2 100644 --- a/Telegram/SourceFiles/data/data_replies_list.cpp +++ b/Telegram/SourceFiles/data/data_replies_list.cpp @@ -29,7 +29,7 @@ constexpr auto kMessagesPerPage = 50; TimeId date, const QString &text) { return history->makeServiceMessage( - history->session().data().nextNonHistoryEntryId(), + history->nextNonHistoryEntryId(), MessageFlag::FakeHistoryItem, date, HistoryService::PreparedText{ text }); @@ -101,7 +101,7 @@ rpl::producer RepliesList::source( _partLoaded.events( ) | rpl::start_with_next(pushDelayed, lifetime); - _history->session().data().channelDifferenceTooLong( + _history->owner().channelDifferenceTooLong( ) | rpl::filter([=](not_null channel) { if (_history->peer != channel || !_skippedAfter.has_value()) { return false; diff --git a/Telegram/SourceFiles/data/data_scheduled_messages.cpp b/Telegram/SourceFiles/data/data_scheduled_messages.cpp index ba5cb3a12..61b988403 100644 --- a/Telegram/SourceFiles/data/data_scheduled_messages.cpp +++ b/Telegram/SourceFiles/data/data_scheduled_messages.cpp @@ -32,18 +32,18 @@ constexpr auto kRequestTimeLimit = 60 * crl::time(1000); && (item->date() > base::unixtime::now()); } -MTPMessage PrepareMessage(const MTPMessage &message, MsgId id) { +MTPMessage PrepareMessage(const MTPMessage &message) { return message.match([&](const MTPDmessageEmpty &data) { return MTP_messageEmpty( data.vflags(), - MTP_int(id), + data.vid(), data.vpeer_id() ? *data.vpeer_id() : MTPPeer()); }, [&](const MTPDmessageService &data) { return MTP_messageService( MTP_flags(data.vflags().v | MTPDmessageService::Flag( MTPDmessage::Flag::f_from_scheduled)), - MTP_int(id), + data.vid(), data.vfrom_id() ? *data.vfrom_id() : MTPPeer(), data.vpeer_id(), data.vreply_to() ? *data.vreply_to() : MTPMessageReplyHeader(), @@ -53,7 +53,7 @@ MTPMessage PrepareMessage(const MTPMessage &message, MsgId id) { }, [&](const MTPDmessage &data) { return MTP_message( MTP_flags(data.vflags().v | MTPDmessage::Flag::f_from_scheduled), - MTP_int(id), + data.vid(), data.vfrom_id() ? *data.vfrom_id() : MTPPeer(), data.vpeer_id(), data.vfwd_from() ? *data.vfwd_from() : MTPMessageFwdHeader(), @@ -192,6 +192,7 @@ void ScheduledMessages::sendNowSimpleMessage( const auto views = 1; const auto forwards = 0; history->addNewMessage( + update.vid().v, MTP_message( MTP_flags(flags), update.vid(), @@ -463,7 +464,8 @@ HistoryItem *ScheduledMessages::append( } const auto item = _session->data().addNewMessage( - PrepareMessage(message, history->nextNonHistoryEntryId()), + history->nextNonHistoryEntryId(), + PrepareMessage(message), MessageFlags(), // localFlags NewMessageType::Existing); if (!item || item->history() != history) { diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index 3402ff998..5a709b7b8 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -2166,12 +2166,21 @@ HistoryItem *Session::addNewMessage( const MTPMessage &data, MessageFlags localFlags, NewMessageType type) { + return addNewMessage(IdFromMessage(data), data, localFlags, type); +} + +HistoryItem *Session::addNewMessage( + MsgId id, + const MTPMessage &data, + MessageFlags localFlags, + NewMessageType type) { const auto peerId = PeerFromMessage(data); if (!peerId) { return nullptr; } const auto result = history(peerId)->addNewMessage( + id, data, localFlags, type); diff --git a/Telegram/SourceFiles/data/data_session.h b/Telegram/SourceFiles/data/data_session.h index 3818adbb0..630fec10f 100644 --- a/Telegram/SourceFiles/data/data_session.h +++ b/Telegram/SourceFiles/data/data_session.h @@ -397,6 +397,11 @@ public: const MTPMessage &data, MessageFlags localFlags, NewMessageType type); + HistoryItem *addNewMessage( // Override message id. + MsgId id, + const MTPMessage &data, + MessageFlags localFlags, + NewMessageType type); [[nodiscard]] int unreadBadge() const; [[nodiscard]] bool unreadBadgeMuted() const; diff --git a/Telegram/SourceFiles/history/admin_log/history_admin_log_item.cpp b/Telegram/SourceFiles/history/admin_log/history_admin_log_item.cpp index f55de6eb5..cff886247 100644 --- a/Telegram/SourceFiles/history/admin_log/history_admin_log_item.cpp +++ b/Telegram/SourceFiles/history/admin_log/history_admin_log_item.cpp @@ -56,14 +56,11 @@ TimeId ExtractSentDate(const MTPMessage &message) { }); } -MTPMessage PrepareLogMessage( - const MTPMessage &message, - MsgId newId, - TimeId newDate) { +MTPMessage PrepareLogMessage(const MTPMessage &message, TimeId newDate) { return message.match([&](const MTPDmessageEmpty &data) { return MTP_messageEmpty( data.vflags(), - MTP_int(newId), + data.vid(), data.vpeer_id() ? *data.vpeer_id() : MTPPeer()); }, [&](const MTPDmessageService &data) { const auto removeFlags = MTPDmessageService::Flag::f_out @@ -72,7 +69,7 @@ MTPMessage PrepareLogMessage( | MTPDmessageService::Flag::f_ttl_period; return MTP_messageService( MTP_flags(data.vflags().v & ~removeFlags), - MTP_int(newId), + data.vid(), data.vfrom_id() ? *data.vfrom_id() : MTPPeer(), data.vpeer_id(), MTPMessageReplyHeader(), @@ -93,7 +90,7 @@ MTPMessage PrepareLogMessage( | MTPDmessage::Flag::f_ttl_period; return MTP_message( MTP_flags(data.vflags().v & ~removeFlags), - MTP_int(newId), + data.vid(), data.vfrom_id() ? *data.vfrom_id() : MTPPeer(), data.vpeer_id(), data.vfwd_from() ? *data.vfwd_from() : MTPMessageFwdHeader(), @@ -669,10 +666,8 @@ void GenerateItems( auto detachExistingItem = false; addPart( history->createItem( - PrepareLogMessage( - action.vmessage(), - history->nextNonHistoryEntryId(), - date), + history->nextNonHistoryEntryId(), + PrepareLogMessage(action.vmessage(), date), MessageFlag::AdminLogEntry, detachExistingItem), ExtractSentDate(action.vmessage())); @@ -698,10 +693,8 @@ void GenerateItems( auto oldValue = ExtractEditedText(session, action.vprev_message()); auto detachExistingItem = false; auto body = history->createItem( - PrepareLogMessage( - action.vnew_message(), - history->nextNonHistoryEntryId(), - date), + history->nextNonHistoryEntryId(), + PrepareLogMessage(action.vnew_message(), date), MessageFlag::AdminLogEntry, detachExistingItem); if (oldValue.text.isEmpty()) { @@ -724,10 +717,8 @@ void GenerateItems( auto detachExistingItem = false; addPart( history->createItem( - PrepareLogMessage( - action.vmessage(), - history->nextNonHistoryEntryId(), - date), + history->nextNonHistoryEntryId(), + PrepareLogMessage(action.vmessage(), date), MessageFlag::AdminLogEntry, detachExistingItem), ExtractSentDate(action.vmessage())); @@ -821,10 +812,8 @@ void GenerateItems( auto detachExistingItem = false; addPart( history->createItem( - PrepareLogMessage( - action.vmessage(), - history->nextNonHistoryEntryId(), - date), + history->nextNonHistoryEntryId(), + PrepareLogMessage(action.vmessage(), date), MessageFlag::AdminLogEntry, detachExistingItem), ExtractSentDate(action.vmessage())); diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index b77562154..73ca8861c 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -360,21 +360,17 @@ void History::setForwardDraft(Data::ForwardDraft &&draft) { } HistoryItem *History::createItem( + MsgId id, const MTPMessage &message, MessageFlags localFlags, bool detachExistingItem) { - const auto messageId = IdFromMessage(message); - if (!messageId) { - return nullptr; - } - - if (const auto result = owner().message(channelId(), messageId)) { + if (const auto result = owner().message(channelId(), id)) { if (detachExistingItem) { result->removeMainView(); } return result; } - return HistoryItem::Create(this, message, localFlags); + return HistoryItem::Create(this, id, message, localFlags); } std::vector> History::createItems( @@ -382,9 +378,14 @@ std::vector> History::createItems( auto result = std::vector>(); result.reserve(data.size()); const auto localFlags = MessageFlags(); + const auto detachExistingItem = true; for (auto i = data.cend(), e = data.cbegin(); i != e;) { - const auto detachExistingItem = true; - const auto item = createItem(*--i, localFlags, detachExistingItem); + const auto &data = *--i; + const auto item = createItem( + IdFromMessage(data), + data, + localFlags, + detachExistingItem); if (item) { result.emplace_back(item); } @@ -393,11 +394,12 @@ std::vector> History::createItems( } HistoryItem *History::addNewMessage( + MsgId id, const MTPMessage &msg, MessageFlags localFlags, NewMessageType type) { const auto detachExistingItem = (type == NewMessageType::Unread); - const auto item = createItem(msg, localFlags, detachExistingItem); + const auto item = createItem(id, msg, localFlags, detachExistingItem); if (!item) { return nullptr; } @@ -744,11 +746,14 @@ void History::addUnreadMentionsSlice(const MTPmessages_Messages &result) { const auto localFlags = MessageFlags(); const auto type = NewMessageType::Existing; for (const auto &message : *messages) { - if (const auto item = addNewMessage(message, localFlags, type)) { - if (item->isUnreadMention()) { - _unreadMentions.insert(item->id); - added = true; - } + const auto item = addNewMessage( + IdFromMessage(message), + message, + localFlags, + type); + if (item && item->isUnreadMention()) { + _unreadMentions.insert(item->id); + added = true; } } } diff --git a/Telegram/SourceFiles/history/history.h b/Telegram/SourceFiles/history/history.h index 2b93ed4b0..77f054504 100644 --- a/Telegram/SourceFiles/history/history.h +++ b/Telegram/SourceFiles/history/history.h @@ -134,12 +134,10 @@ public: void unpinAllMessages(); HistoryItem *addNewMessage( + MsgId id, const MTPMessage &msg, MessageFlags localFlags, NewMessageType type); - HistoryItem *addToHistory( - const MTPMessage &msg, - MessageFlags localFlags); not_null addNewLocalMessage( MsgId id, MessageFlags flags, @@ -194,6 +192,7 @@ public: // Used only internally and for channel admin log. HistoryItem *createItem( + MsgId id, const MTPMessage &message, MessageFlags localFlags, bool detachExistingItem); @@ -418,7 +417,7 @@ public: [[nodiscard]] std::pair findItemAndOffset(int top) const; - MsgId nextNonHistoryEntryId(); + [[nodiscard]] MsgId nextNonHistoryEntryId(); bool folderKnown() const override; Data::Folder *folder() const override; diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index b37aad5ad..553b1ae09 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -1089,6 +1089,7 @@ MessageFlags FlagsFromMTP(MTPDmessageService::Flags flags) { not_null HistoryItem::Create( not_null history, + MsgId id, const MTPMessage &message, MessageFlags localFlags) { return message.match([&](const MTPDmessage &data) -> HistoryItem* { @@ -1099,7 +1100,7 @@ not_null HistoryItem::Create( if (checked == MediaCheckResult::Unsupported) { return CreateUnsupportedMessage( history, - data.vid().v, + id, FlagsFromMTP(data.vflags().v) | localFlags, MsgId(0), // No need to pass reply_to data here. data.vvia_bot_id().value_or_empty(), @@ -1110,28 +1111,24 @@ not_null HistoryItem::Create( tr::lng_message_empty(tr::now) }; return history->makeServiceMessage( - data.vid().v, + id, FlagsFromMTP(data.vflags().v) | localFlags, data.vdate().v, text, data.vfrom_id() ? peerFromMTP(*data.vfrom_id()) : PeerId(0)); } else if (checked == MediaCheckResult::HasTimeToLive) { - return history->makeServiceMessage(data, localFlags); + return history->makeServiceMessage(id, data, localFlags); } - return history->makeMessage(data, localFlags); + return history->makeMessage(id, data, localFlags); }, [&](const MTPDmessageService &data) -> HistoryItem* { if (data.vaction().type() == mtpc_messageActionPhoneCall) { - return history->makeMessage(data, localFlags); + return history->makeMessage(id, data, localFlags); } - return history->makeServiceMessage(data, localFlags); + return history->makeServiceMessage(id, data, localFlags); }, [&](const MTPDmessageEmpty &data) -> HistoryItem* { const auto text = HistoryService::PreparedText{ tr::lng_message_empty(tr::now) }; - return history->makeServiceMessage( - data.vid().v, - localFlags, - TimeId(0), - text); + return history->makeServiceMessage(id, localFlags, TimeId(0), text); }); } diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index feaa262de..a15beb394 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -81,6 +81,7 @@ class HistoryItem : public RuntimeComposer { public: static not_null Create( not_null history, + MsgId id, const MTPMessage &message, MessageFlags localFlags); diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp index 16df90221..c0cab7111 100644 --- a/Telegram/SourceFiles/history/history_message.cpp +++ b/Telegram/SourceFiles/history/history_message.cpp @@ -453,11 +453,12 @@ void HistoryMessage::FillForwardedInfo( HistoryMessage::HistoryMessage( not_null history, + MsgId id, const MTPDmessage &data, MessageFlags localFlags) : HistoryItem( history, - data.vid().v, + id, FlagsFromMTP(data.vflags().v) | localFlags, data.vdate().v, data.vfrom_id() ? peerFromMTP(*data.vfrom_id()) : PeerId(0)) { @@ -509,11 +510,12 @@ HistoryMessage::HistoryMessage( HistoryMessage::HistoryMessage( not_null history, + MsgId id, const MTPDmessageService &data, MessageFlags localFlags) : HistoryItem( history, - data.vid().v, + id, FlagsFromMTP(data.vflags().v) | localFlags, data.vdate().v, data.vfrom_id() ? peerFromMTP(*data.vfrom_id()) : PeerId(0)) { diff --git a/Telegram/SourceFiles/history/history_message.h b/Telegram/SourceFiles/history/history_message.h index 88440d555..b2823c70c 100644 --- a/Telegram/SourceFiles/history/history_message.h +++ b/Telegram/SourceFiles/history/history_message.h @@ -48,10 +48,12 @@ class HistoryMessage final : public HistoryItem { public: HistoryMessage( not_null history, + MsgId id, const MTPDmessage &data, MessageFlags localFlags); HistoryMessage( not_null history, + MsgId id, const MTPDmessageService &data, MessageFlags localFlags); HistoryMessage( diff --git a/Telegram/SourceFiles/history/history_service.cpp b/Telegram/SourceFiles/history/history_service.cpp index f5eed00d6..cf1a304d6 100644 --- a/Telegram/SourceFiles/history/history_service.cpp +++ b/Telegram/SourceFiles/history/history_service.cpp @@ -866,11 +866,12 @@ HistoryService::PreparedText HistoryService::prepareCallScheduledText( HistoryService::HistoryService( not_null history, + MsgId id, const MTPDmessage &data, MessageFlags localFlags) : HistoryItem( history, - data.vid().v, + id, FlagsFromMTP(data.vflags().v) | localFlags, data.vdate().v, data.vfrom_id() ? peerFromMTP(*data.vfrom_id()) : PeerId(0)) { @@ -880,11 +881,12 @@ HistoryService::HistoryService( HistoryService::HistoryService( not_null history, + MsgId id, const MTPDmessageService &data, MessageFlags localFlags) : HistoryItem( history, - data.vid().v, + id, FlagsFromMTP(data.vflags().v) | localFlags, data.vdate().v, data.vfrom_id() ? peerFromMTP(*data.vfrom_id()) : PeerId(0)) { diff --git a/Telegram/SourceFiles/history/history_service.h b/Telegram/SourceFiles/history/history_service.h index 8922b0e22..b8725535c 100644 --- a/Telegram/SourceFiles/history/history_service.h +++ b/Telegram/SourceFiles/history/history_service.h @@ -69,10 +69,12 @@ public: HistoryService( not_null history, + MsgId id, const MTPDmessage &data, MessageFlags localFlags); HistoryService( not_null history, + MsgId id, const MTPDmessageService &data, MessageFlags localFlags); HistoryService( diff --git a/Telegram/SourceFiles/settings/settings_privacy_controllers.cpp b/Telegram/SourceFiles/settings/settings_privacy_controllers.cpp index 9dfccc6f5..c7e32e517 100644 --- a/Telegram/SourceFiles/settings/settings_privacy_controllers.cpp +++ b/Telegram/SourceFiles/settings/settings_privacy_controllers.cpp @@ -136,7 +136,7 @@ AdminLog::OwnedItem GenerateForwardedItem( const auto flags = Flag::f_from_id | Flag::f_fwd_from; const auto item = MTP_message( MTP_flags(flags), - MTP_int(history->owner().nextNonHistoryEntryId()), + MTP_int(0), peerToMTP(history->peer->id), peerToMTP(history->peer->id), MTP_messageFwdHeader( @@ -166,7 +166,10 @@ AdminLog::OwnedItem GenerateForwardedItem( MTPVector(), MTPint() // ttl_period ).match([&](const MTPDmessage &data) { - return history->makeMessage(data, MessageFlag::FakeHistoryItem); + return history->makeMessage( + history->nextNonHistoryEntryId(), + data, + MessageFlag::FakeHistoryItem); }, [](auto &&) -> not_null { Unexpected("Type in GenerateForwardedItem."); }); diff --git a/Telegram/SourceFiles/support/support_autocomplete.cpp b/Telegram/SourceFiles/support/support_autocomplete.cpp index d3e5841ec..853c462d8 100644 --- a/Telegram/SourceFiles/support/support_autocomplete.cpp +++ b/Telegram/SourceFiles/support/support_autocomplete.cpp @@ -19,7 +19,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/view/history_view_service_message.h" #include "history/history_message.h" #include "lang/lang_keys.h" -#include "data/data_session.h" #include "base/unixtime.h" #include "base/call_delayed.h" #include "base/qt_adapters.h" @@ -281,7 +280,7 @@ AdminLog::OwnedItem GenerateCommentItem( const auto viaBotId = UserId(); const auto groupedId = uint64(); const auto item = history->makeMessage( - history->owner().nextNonHistoryEntryId(), + history->nextNonHistoryEntryId(), flags, replyTo, viaBotId, @@ -304,7 +303,7 @@ AdminLog::OwnedItem GenerateContactItem( const auto postAuthor = QString(); const auto groupedId = uint64(); const auto item = history->makeMessage( - history->owner().nextNonHistoryEntryId(), + history->nextNonHistoryEntryId(), (MessageFlag::HasFromId | MessageFlag::Outgoing | MessageFlag::FakeHistoryItem),