Don't pass wide fake MsgId through MTPMessage.

This commit is contained in:
John Preston 2021-09-30 15:30:39 +04:00
parent 4ea72f8f89
commit ead695b101
16 changed files with 87 additions and 70 deletions

View file

@ -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,

View file

@ -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<MessagesSlice> RepliesList::source(
_partLoaded.events(
) | rpl::start_with_next(pushDelayed, lifetime);
_history->session().data().channelDifferenceTooLong(
_history->owner().channelDifferenceTooLong(
) | rpl::filter([=](not_null<ChannelData*> channel) {
if (_history->peer != channel || !_skippedAfter.has_value()) {
return false;

View file

@ -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) {

View file

@ -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);

View file

@ -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;

View file

@ -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()));

View file

@ -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<not_null<HistoryItem*>> History::createItems(
@ -382,9 +378,14 @@ std::vector<not_null<HistoryItem*>> History::createItems(
auto result = std::vector<not_null<HistoryItem*>>();
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<not_null<HistoryItem*>> 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;
}
}
}

View file

@ -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<HistoryItem*> 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<Element*, int> findItemAndOffset(int top) const;
MsgId nextNonHistoryEntryId();
[[nodiscard]] MsgId nextNonHistoryEntryId();
bool folderKnown() const override;
Data::Folder *folder() const override;

View file

@ -1089,6 +1089,7 @@ MessageFlags FlagsFromMTP(MTPDmessageService::Flags flags) {
not_null<HistoryItem*> HistoryItem::Create(
not_null<History*> history,
MsgId id,
const MTPMessage &message,
MessageFlags localFlags) {
return message.match([&](const MTPDmessage &data) -> HistoryItem* {
@ -1099,7 +1100,7 @@ not_null<HistoryItem*> 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*> 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);
});
}

View file

@ -81,6 +81,7 @@ class HistoryItem : public RuntimeComposer<HistoryItem> {
public:
static not_null<HistoryItem*> Create(
not_null<History*> history,
MsgId id,
const MTPMessage &message,
MessageFlags localFlags);

View file

@ -453,11 +453,12 @@ void HistoryMessage::FillForwardedInfo(
HistoryMessage::HistoryMessage(
not_null<History*> 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*> 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)) {

View file

@ -48,10 +48,12 @@ class HistoryMessage final : public HistoryItem {
public:
HistoryMessage(
not_null<History*> history,
MsgId id,
const MTPDmessage &data,
MessageFlags localFlags);
HistoryMessage(
not_null<History*> history,
MsgId id,
const MTPDmessageService &data,
MessageFlags localFlags);
HistoryMessage(

View file

@ -866,11 +866,12 @@ HistoryService::PreparedText HistoryService::prepareCallScheduledText(
HistoryService::HistoryService(
not_null<History*> 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*> 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)) {

View file

@ -69,10 +69,12 @@ public:
HistoryService(
not_null<History*> history,
MsgId id,
const MTPDmessage &data,
MessageFlags localFlags);
HistoryService(
not_null<History*> history,
MsgId id,
const MTPDmessageService &data,
MessageFlags localFlags);
HistoryService(

View file

@ -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<MTPRestrictionReason>(),
MTPint() // ttl_period
).match([&](const MTPDmessage &data) {
return history->makeMessage(data, MessageFlag::FakeHistoryItem);
return history->makeMessage(
history->nextNonHistoryEntryId(),
data,
MessageFlag::FakeHistoryItem);
}, [](auto &&) -> not_null<HistoryMessage*> {
Unexpected("Type in GenerateForwardedItem.");
});

View file

@ -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),