Fix local history clearing.

This commit is contained in:
John Preston 2019-04-08 13:16:45 +04:00
parent f980023f49
commit fb244c00b9
9 changed files with 84 additions and 96 deletions

View File

@ -2383,8 +2383,7 @@ void ApiWrap::clearHistory(not_null<PeerData*> peer, bool revoke) {
if (const auto last = history->chatListMessage()) { if (const auto last = history->chatListMessage()) {
Local::addSavedPeer(history->peer, ItemDateTime(last)); Local::addSavedPeer(history->peer, ItemDateTime(last));
} }
history->clear(); history->clear(History::ClearType::ClearHistory);
history->markFullyLoaded();
} }
if (const auto channel = peer->asChannel()) { if (const auto channel = peer->asChannel()) {
if (const auto migrated = peer->migrateFrom()) { if (const auto migrated = peer->migrateFrom()) {

View File

@ -167,7 +167,7 @@ void Session::clear() {
_sendActions.clear(); _sendActions.clear();
for (const auto &[peerId, history] : _histories) { for (const auto &[peerId, history] : _histories) {
history->unloadBlocks(); history->clear(History::ClearType::Unload);
} }
App::historyClearMsgs(); App::historyClearMsgs();
_histories.clear(); _histories.clear();
@ -727,7 +727,9 @@ void Session::deleteConversationLocally(not_null<PeerData*> peer) {
if (history) { if (history) {
setPinnedDialog(history, false); setPinnedDialog(history, false);
App::main()->removeDialog(history); App::main()->removeDialog(history);
history->clear(); history->clear(peer->isChannel()
? History::ClearType::Unload
: History::ClearType::DeleteChat);
} }
if (const auto channel = peer->asMegagroup()) { if (const auto channel = peer->asMegagroup()) {
channel->addFlags(MTPDchannel::Flag::f_left); channel->addFlags(MTPDchannel::Flag::f_left);
@ -736,8 +738,6 @@ void Session::deleteConversationLocally(not_null<PeerData*> peer) {
migrated->updateChatListExistence(); migrated->updateChatListExistence();
} }
} }
} else if (history) {
history->markFullyLoaded();
} }
} }

View File

@ -2102,7 +2102,7 @@ void History::getReadyFor(MsgId msgId) {
const auto migrated = owner().history(peer->migrateFrom()->id); const auto migrated = owner().history(peer->migrateFrom()->id);
migrated->getReadyFor(-msgId); migrated->getReadyFor(-msgId);
if (migrated->isEmpty()) { if (migrated->isEmpty()) {
unloadBlocks(); clear(ClearType::Unload);
} }
return; return;
} }
@ -2110,7 +2110,7 @@ void History::getReadyFor(MsgId msgId) {
if (const auto migratePeer = peer->migrateFrom()) { if (const auto migratePeer = peer->migrateFrom()) {
if (const auto migrated = owner().historyLoaded(migratePeer)) { if (const auto migrated = owner().historyLoaded(migratePeer)) {
if (migrated->unreadCount()) { if (migrated->unreadCount()) {
unloadBlocks(); clear(ClearType::Unload);
migrated->getReadyFor(msgId); migrated->getReadyFor(msgId);
return; return;
} }
@ -2118,10 +2118,10 @@ void History::getReadyFor(MsgId msgId) {
} }
} }
if (!isReadyFor(msgId)) { if (!isReadyFor(msgId)) {
unloadBlocks(); clear(ClearType::Unload);
if (const auto migratePeer = peer->migrateFrom()) { if (const auto migratePeer = peer->migrateFrom()) {
if (const auto migrated = owner().historyLoaded(migratePeer)) { if (const auto migrated = owner().historyLoaded(migratePeer)) {
migrated->unloadBlocks(); migrated->clear(ClearType::Unload);
} }
} }
if (msgId == ShowAtTheEndMsgId) { if (msgId == ShowAtTheEndMsgId) {
@ -2144,18 +2144,15 @@ void History::setNotLoadedAtBottom() {
} }
} }
void History::markFullyLoaded() { void History::clearSharedMedia() {
_loadedAtTop = _loadedAtBottom = true; session().storage().remove(
if (isEmpty()) { Storage::SharedMediaRemoveAll(peer->id));
session().storage().remove( if (const auto channel = peer->asChannel()) {
Storage::SharedMediaRemoveAll(peer->id)); if (const auto feed = channel->feed()) {
if (const auto channel = peer->asChannel()) { session().storage().remove(
if (const auto feed = channel->feed()) { Storage::FeedMessagesRemoveAll(
session().storage().remove( feed->id(),
Storage::FeedMessagesRemoveAll( channel->bareId()));
feed->id(),
channel->bareId()));
}
} }
} }
} }
@ -2455,23 +2452,17 @@ void History::dialogEntryApplied() {
return; return;
} }
if (!chatListMessage()) { if (!chatListMessage()) {
if (const auto chat = peer->asChat()) { if (const auto channel = peer->asChannel()) {
if (!chat->haveLeft()) {
Local::addSavedPeer(
peer,
ParseDateTime(chatListTimeId()));
}
} else if (const auto channel = peer->asChannel()) {
const auto inviter = channel->inviter; const auto inviter = channel->inviter;
if (inviter != 0 && channel->amIn()) { if (inviter != 0 && channel->amIn()) {
if (const auto from = owner().userLoaded(inviter)) { if (const auto from = owner().userLoaded(inviter)) {
unloadBlocks(); clear(ClearType::Unload);
addNewerSlice(QVector<MTPMessage>()); addNewerSlice(QVector<MTPMessage>());
insertJoinedMessage(true); insertJoinedMessage(true);
} }
} }
} else { } else {
clear(); clear(ClearType::DeleteChat);
} }
return; return;
} }
@ -2921,7 +2912,7 @@ bool History::hasOrphanMediaGroupPart() const {
bool History::removeOrphanMediaGroupPart() { bool History::removeOrphanMediaGroupPart() {
if (hasOrphanMediaGroupPart()) { if (hasOrphanMediaGroupPart()) {
unloadBlocks(); clear(ClearType::Unload);
return true; return true;
} }
return false; return false;
@ -2941,54 +2932,50 @@ QVector<MsgId> History::collectMessagesFromUserToDelete(
return result; return result;
} }
void History::clear() { void History::clear(ClearType type) {
clearBlocks(false);
}
void History::unloadBlocks() {
clearBlocks(true);
}
void History::clearBlocks(bool leaveItems) {
_unreadBarView = nullptr; _unreadBarView = nullptr;
_firstUnreadView = nullptr; _firstUnreadView = nullptr;
_joinedMessage = nullptr; _joinedMessage = nullptr;
if (scrollTopItem) { forgetScrollState();
forgetScrollState(); if (type == ClearType::Unload) {
} blocks.clear();
if (leaveItems) {
_owner->notifyHistoryUnloaded(this); _owner->notifyHistoryUnloaded(this);
lastKeyboardInited = false;
_loadedAtTop = _loadedAtBottom = false;
} else { } else {
if (peer->isChannel()) {
// We left the channel.
_lastMessage = std::nullopt;
} else {
// History was deleted.
setLastMessage(nullptr);
}
notifies.clear(); notifies.clear();
_owner->notifyHistoryCleared(this); _owner->notifyHistoryCleared(this);
}
blocks.clear();
if (leaveItems) {
lastKeyboardInited = false;
} else {
changeUnreadCount(-unreadCount()); changeUnreadCount(-unreadCount());
if (auto channel = peer->asChannel()) { if (type == ClearType::DeleteChat) {
setLastMessage(nullptr);
} else {
if (_lastMessage && IsServerMsgId((*_lastMessage)->id)) {
(*_lastMessage)->applyEditionToHistoryCleared();
} else {
_lastMessage = std::nullopt;
}
}
const auto tillId = (_lastMessage && *_lastMessage)
? (*_lastMessage)->id
: std::numeric_limits<MsgId>::max();
clearUpTill(tillId);
if (blocks.empty() && _lastMessage && *_lastMessage) {
addItemToBlock(*_lastMessage);
}
_loadedAtTop = _loadedAtBottom = _lastMessage.has_value();
clearSharedMedia();
clearLastKeyboard();
if (const auto channel = peer->asChannel()) {
channel->clearPinnedMessage(); channel->clearPinnedMessage();
if (const auto feed = channel->feed()) { if (const auto feed = channel->feed()) {
// Should be after resetting the _lastMessage. // Should be after resetting the _lastMessage.
feed->historyCleared(this); feed->historyCleared(this);
} }
} }
clearLastKeyboard();
} }
_owner->notifyHistoryChangeDelayed(this); _owner->notifyHistoryChangeDelayed(this);
_loadedAtTop = false;
_loadedAtBottom = !leaveItems;
forgetScrollState();
if (const auto chat = peer->asChat()) { if (const auto chat = peer->asChat()) {
chat->lastAuthors.clear(); chat->lastAuthors.clear();
chat->markupSenders.clear(); chat->markupSenders.clear();
@ -3007,17 +2994,7 @@ void History::clearUpTill(MsgId availableMinId) {
const auto itemId = item->id; const auto itemId = item->id;
if (IsServerMsgId(itemId) && itemId >= availableMinId) { if (IsServerMsgId(itemId) && itemId >= availableMinId) {
if (itemId == availableMinId) { if (itemId == availableMinId) {
auto fromId = 0; item->applyEditionToHistoryCleared();
auto replyToId = 0;
item->applyEdition(MTP_messageService(
MTP_flags(0),
MTP_int(itemId),
MTP_int(fromId),
peerToMTP(peer->id),
MTP_int(replyToId),
MTP_int(item->date()),
MTP_messageActionHistoryClear()
).c_messageService());
} }
break; break;
} }

View File

@ -84,9 +84,12 @@ public:
QVector<MsgId> collectMessagesFromUserToDelete( QVector<MsgId> collectMessagesFromUserToDelete(
not_null<UserData*> user) const; not_null<UserData*> user) const;
void clear(); enum class ClearType {
void markFullyLoaded(); Unload,
void unloadBlocks(); DeleteChat,
ClearHistory,
};
void clear(ClearType type);
void clearUpTill(MsgId availableMinId); void clearUpTill(MsgId availableMinId);
void applyGroupAdminChanges( void applyGroupAdminChanges(
@ -377,8 +380,7 @@ private:
// when the last item from this block was detached and // when the last item from this block was detached and
// calls the required previousItemChanged() // calls the required previousItemChanged()
void removeBlock(not_null<HistoryBlock*> block); void removeBlock(not_null<HistoryBlock*> block);
void clearSharedMedia();
void clearBlocks(bool leaveItems);
not_null<HistoryItem*> addNewItem( not_null<HistoryItem*> addNewItem(
not_null<HistoryItem*> item, not_null<HistoryItem*> item,

View File

@ -374,6 +374,21 @@ void HistoryItem::clearMainView() {
void HistoryItem::addToUnreadMentions(UnreadMentionType type) { void HistoryItem::addToUnreadMentions(UnreadMentionType type) {
} }
void HistoryItem::applyEditionToHistoryCleared() {
const auto fromId = 0;
const auto replyToId = 0;
applyEdition(
MTP_messageService(
MTP_flags(0),
MTP_int(id),
MTP_int(fromId),
peerToMTP(history()->peer->id),
MTP_int(replyToId),
MTP_int(date()),
MTP_messageActionHistoryClear()
).c_messageService());
}
void HistoryItem::indexAsNewItem() { void HistoryItem::indexAsNewItem() {
if (IsServerMsgId(id)) { if (IsServerMsgId(id)) {
CrashReports::SetAnnotation("addToUnreadMentions", QString::number(id)); CrashReports::SetAnnotation("addToUnreadMentions", QString::number(id));

View File

@ -188,6 +188,7 @@ public:
} }
virtual void applyEdition(const MTPDmessageService &message) { virtual void applyEdition(const MTPDmessageService &message) {
} }
void applyEditionToHistoryCleared();
virtual void updateSentMedia(const MTPMessageMedia *media) { virtual void updateSentMedia(const MTPMessageMedia *media) {
} }
virtual void updateReplyMarkup(const MTPReplyMarkup *markup) { virtual void updateReplyMarkup(const MTPReplyMarkup *markup) {

View File

@ -937,19 +937,15 @@ void HistoryMessage::applyEdition(const MTPDmessage &message) {
void HistoryMessage::applyEdition(const MTPDmessageService &message) { void HistoryMessage::applyEdition(const MTPDmessageService &message) {
if (message.vaction.type() == mtpc_messageActionHistoryClear) { if (message.vaction.type() == mtpc_messageActionHistoryClear) {
applyEditionToEmpty(); setReplyMarkup(nullptr);
refreshMedia(nullptr);
setEmptyText();
setViewsCount(-1);
finishEditionToEmpty();
} }
} }
void HistoryMessage::applyEditionToEmpty() {
setReplyMarkup(nullptr);
refreshMedia(nullptr);
setEmptyText();
setViewsCount(-1);
finishEditionToEmpty();
}
void HistoryMessage::updateSentMedia(const MTPMessageMedia *media) { void HistoryMessage::updateSentMedia(const MTPMessageMedia *media) {
if (_flags & MTPDmessage_ClientFlag::f_from_inline_bot) { if (_flags & MTPDmessage_ClientFlag::f_from_inline_bot) {
if (!media || !_media || !_media->updateInlineResultMedia(*media)) { if (!media || !_media || !_media->updateInlineResultMedia(*media)) {

View File

@ -159,8 +159,6 @@ private:
// It should show the receipt for the payed invoice. Still let mobile apps do that. // It should show the receipt for the payed invoice. Still let mobile apps do that.
void replaceBuyWithReceiptInMarkup(); void replaceBuyWithReceiptInMarkup();
void applyEditionToEmpty();
void setReplyMarkup(const MTPReplyMarkup *markup); void setReplyMarkup(const MTPReplyMarkup *markup);
struct CreateConfig; struct CreateConfig;

View File

@ -1653,7 +1653,7 @@ void HistoryWidget::showHistory(
if (_migrated if (_migrated
&& !_migrated->isEmpty() && !_migrated->isEmpty()
&& (!_history->loadedAtTop() || !_migrated->loadedAtBottom())) { && (!_history->loadedAtTop() || !_migrated->loadedAtBottom())) {
_migrated->unloadBlocks(); _migrated->clear(History::ClearType::Unload);
} }
_topBar->setActiveChat(_history); _topBar->setActiveChat(_history);
@ -2306,9 +2306,9 @@ void HistoryWidget::messagesReceived(PeerData *peer, const MTPmessages_Messages
if (_history->loadedAtBottom() && App::wnd()) App::wnd()->checkHistoryActivation(); if (_history->loadedAtBottom() && App::wnd()) App::wnd()->checkHistoryActivation();
} else if (_firstLoadRequest == requestId) { } else if (_firstLoadRequest == requestId) {
if (toMigrated) { if (toMigrated) {
_history->unloadBlocks(); _history->clear(History::ClearType::Unload);
} else if (_migrated) { } else if (_migrated) {
_migrated->unloadBlocks(); _migrated->clear(History::ClearType::Unload);
} }
addMessagesToFront(peer, *histList); addMessagesToFront(peer, *histList);
_firstLoadRequest = 0; _firstLoadRequest = 0;
@ -2320,9 +2320,9 @@ void HistoryWidget::messagesReceived(PeerData *peer, const MTPmessages_Messages
historyLoaded(); historyLoaded();
} else if (_delayedShowAtRequest == requestId) { } else if (_delayedShowAtRequest == requestId) {
if (toMigrated) { if (toMigrated) {
_history->unloadBlocks(); _history->clear(History::ClearType::Unload);
} else if (_migrated) { } else if (_migrated) {
_migrated->unloadBlocks(); _migrated->clear(History::ClearType::Unload);
} }
_delayedShowAtRequest = 0; _delayedShowAtRequest = 0;
@ -5250,7 +5250,7 @@ void HistoryWidget::handlePeerMigration() {
&& to && to
&& !from->isEmpty() && !from->isEmpty()
&& (!from->loadedAtBottom() || !to->loadedAtTop())) { && (!from->loadedAtBottom() || !to->loadedAtTop())) {
from->unloadBlocks(); from->clear(History::ClearType::Unload);
} }
} }