2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 06:26:18 +00:00

FullMsgId rely on PeerId instead of ChannelId.

This commit is contained in:
John Preston
2021-12-09 11:32:54 +04:00
parent a8f05a01ed
commit 5e7e7eaa83
61 changed files with 446 additions and 475 deletions

View File

@@ -285,7 +285,7 @@ void Session::clear() {
_sponsoredMessages = nullptr;
_dependentMessages.clear();
base::take(_messages);
base::take(_channelMessages);
base::take(_nonChannelMessages);
_messageByRandomId.clear();
_sentMessagesData.clear();
cSetRecentInlineBots(RecentInlineBots());
@@ -1334,20 +1334,31 @@ rpl::producer<not_null<HistoryItem*>> Session::newItemAdded() const {
return _newItemAdded.events();
}
void Session::changeMessageId(ChannelId channel, MsgId wasId, MsgId nowId) {
const auto list = messagesListForInsert(channel);
void Session::changeMessageId(PeerId peerId, MsgId wasId, MsgId nowId) {
const auto list = messagesListForInsert(peerId);
auto i = list->find(wasId);
Assert(i != list->end());
auto owned = std::move(i->second);
const auto item = i->second;
list->erase(i);
const auto [j, ok] = list->emplace(nowId, std::move(owned));
const auto [j, ok] = list->emplace(nowId, item);
if (!peerIsChannel(peerId)) {
if (IsServerMsgId(wasId)) {
const auto k = _nonChannelMessages.find(wasId);
Assert(k != end(_nonChannelMessages));
_nonChannelMessages.erase(k);
}
if (IsServerMsgId(nowId)) {
_nonChannelMessages.emplace(nowId, item);
}
}
Ensures(ok);
}
void Session::notifyItemIdChange(IdChange event) {
const auto item = event.item;
changeMessageId(item->history()->channelId(), event.oldId, item->id);
changeMessageId(item->history()->peer->id, event.oldId, item->id);
_itemIdChanges.fire_copy(event);
@@ -1797,7 +1808,7 @@ void Session::reorderTwoPinnedChats(
bool Session::checkEntitiesAndViewsUpdate(const MTPDmessage &data) {
const auto peer = peerFromMTP(data.vpeer_id());
const auto existing = message(peerToChannel(peer), data.vid().v);
const auto existing = message(peer, data.vid().v);
if (!existing) {
return false;
}
@@ -1817,8 +1828,7 @@ void Session::updateEditedMessage(const MTPMessage &data) {
-> HistoryItem* {
return nullptr;
}, [&](const auto &data) {
const auto peer = peerFromMTP(data.vpeer_id());
return message(peerToChannel(peer), data.vid().v);
return message(peerFromMTP(data.vpeer_id()), data.vid().v);
});
if (!existing) {
return;
@@ -1885,23 +1895,19 @@ void Session::processExistingMessages(
});
}
const Session::Messages *Session::messagesList(ChannelId channelId) const {
if (channelId == NoChannel) {
return &_messages;
}
const auto i = _channelMessages.find(channelId);
return (i != end(_channelMessages)) ? &i->second : nullptr;
const Session::Messages *Session::messagesList(PeerId peerId) const {
const auto i = _messages.find(peerId);
return (i != end(_messages)) ? &i->second : nullptr;
}
auto Session::messagesListForInsert(ChannelId channelId)
auto Session::messagesListForInsert(PeerId peerId)
-> not_null<Messages*> {
return (channelId == NoChannel)
? &_messages
: &_channelMessages[channelId];
return &_messages[peerId];
}
void Session::registerMessage(not_null<HistoryItem*> item) {
const auto list = messagesListForInsert(item->channelId());
const auto peerId = item->history()->peer->id;
const auto list = messagesListForInsert(peerId);
const auto itemId = item->id;
const auto i = list->find(itemId);
if (i != list->end()) {
@@ -1909,6 +1915,10 @@ void Session::registerMessage(not_null<HistoryItem*> item) {
i->second->destroy();
}
list->emplace(itemId, item);
if (!peerIsChannel(peerId) && IsServerMsgId(itemId)) {
_nonChannelMessages.emplace(itemId, item);
}
}
void Session::registerMessageTTL(TimeId when, not_null<HistoryItem*> item) {
@@ -1963,12 +1973,10 @@ void Session::checkTTLs() {
}
void Session::processMessagesDeleted(
ChannelId channelId,
PeerId peerId,
const QVector<MTPint> &data) {
const auto list = messagesList(channelId);
const auto affected = (channelId != NoChannel)
? historyLoaded(peerFromChannel(channelId))
: nullptr;
const auto list = messagesList(peerId);
const auto affected = historyLoaded(peerId);
if (!list && !affected) {
return;
}
@@ -1991,6 +1999,22 @@ void Session::processMessagesDeleted(
}
}
void Session::processNonChannelMessagesDeleted(const QVector<MTPint> &data) {
auto historiesToCheck = base::flat_set<not_null<History*>>();
for (const auto &messageId : data) {
if (const auto item = nonChannelMessage(messageId.v)) {
const auto history = item->history();
item->destroy();
if (!history->chatListMessageKnown()) {
historiesToCheck.emplace(history);
}
}
}
for (const auto &history : historiesToCheck) {
history->requestChatListMessage();
}
}
void Session::removeDependencyMessage(not_null<HistoryItem*> item) {
const auto i = _dependentMessages.find(item);
if (i == end(_dependentMessages)) {
@@ -2006,13 +2030,18 @@ void Session::removeDependencyMessage(not_null<HistoryItem*> item) {
void Session::unregisterMessage(not_null<HistoryItem*> item) {
const auto peerId = item->history()->peer->id;
const auto itemId = item->id;
_itemRemoved.fire_copy(item);
session().changes().messageUpdated(
item,
Data::MessageUpdate::Flag::Destroyed);
groups().unregisterMessage(item);
removeDependencyMessage(item);
messagesListForInsert(peerToChannel(peerId))->erase(item->id);
messagesListForInsert(peerId)->erase(itemId);
if (!peerIsChannel(peerId) && IsServerMsgId(itemId)) {
_nonChannelMessages.erase(itemId);
}
}
MsgId Session::nextLocalMessageId() {
@@ -2035,12 +2064,12 @@ bool Session::suggestToGigagroup(not_null<ChannelData*> group) const {
return _suggestToGigagroup.contains(group);
}
HistoryItem *Session::message(ChannelId channelId, MsgId itemId) const {
HistoryItem *Session::message(PeerId peerId, MsgId itemId) const {
if (!itemId) {
return nullptr;
}
const auto data = messagesList(channelId);
const auto data = messagesList(peerId);
if (!data) {
return nullptr;
}
@@ -2050,13 +2079,21 @@ HistoryItem *Session::message(ChannelId channelId, MsgId itemId) const {
}
HistoryItem *Session::message(
const ChannelData *channel,
not_null<const PeerData*> peer,
MsgId itemId) const {
return message(channel ? peerToChannel(channel->id) : 0, itemId);
return message(peer->id, itemId);
}
HistoryItem *Session::message(FullMsgId itemId) const {
return message(itemId.channel, itemId.msg);
return message(itemId.peer, itemId.msg);
}
HistoryItem *Session::nonChannelMessage(MsgId itemId) const {
if (!IsServerMsgId(itemId)) {
return nullptr;
}
const auto i = _nonChannelMessages.find(itemId);
return (i != end(_nonChannelMessages)) ? i->second.get() : nullptr;
}
void Session::updateDependentMessages(not_null<HistoryItem*> item) {