Fix history items cleanup.

This commit is contained in:
John Preston 2018-06-09 11:52:37 +03:00
parent 5c4b81434e
commit bf775cb4ca
2 changed files with 38 additions and 45 deletions

View File

@ -50,8 +50,7 @@ namespace {
UserData *self = nullptr; UserData *self = nullptr;
using PeersData = QHash<PeerId, PeerData*>; std::unordered_map<PeerId, std::unique_ptr<PeerData>> peersData;
PeersData peersData;
using LocationsData = QHash<LocationCoords, LocationData*>; using LocationsData = QHash<LocationCoords, LocationData*>;
LocationsData locationsData; LocationsData locationsData;
@ -747,7 +746,7 @@ namespace App {
} else if (chat->version <= d.vversion.v && chat->count > 0) { } else if (chat->version <= d.vversion.v && chat->count > 0) {
chat->version = d.vversion.v; chat->version = d.vversion.v;
auto canEdit = chat->canEdit(); auto canEdit = chat->canEdit();
UserData *user = App::userLoaded(d.vuser_id.v); const auto user = App::userLoaded(d.vuser_id.v);
if (user) { if (user) {
if (chat->participants.empty()) { if (chat->participants.empty()) {
if (chat->count > 0) { if (chat->count > 0) {
@ -1074,40 +1073,43 @@ namespace App {
} }
PeerData *peer(const PeerId &id, PeerData::LoadedStatus restriction) { PeerData *peer(const PeerId &id, PeerData::LoadedStatus restriction) {
if (!id) return nullptr; if (!id) {
return nullptr;
}
auto i = peersData.constFind(id); auto i = peersData.find(id);
if (i == peersData.cend()) { if (i == peersData.cend()) {
PeerData *newData = nullptr; auto newData = [&]() -> std::unique_ptr<PeerData> {
if (peerIsUser(id)) { if (peerIsUser(id)) {
newData = new UserData(id); return std::make_unique<UserData>(id);
} else if (peerIsChat(id)) { } else if (peerIsChat(id)) {
newData = new ChatData(id); return std::make_unique<ChatData>(id);
} else if (peerIsChannel(id)) { } else if (peerIsChannel(id)) {
newData = new ChannelData(id); return std::make_unique<ChannelData>(id);
} }
Assert(newData != nullptr); Unexpected("Peer id type.");
}();
newData->input = MTPinputPeer(MTP_inputPeerEmpty()); newData->input = MTPinputPeer(MTP_inputPeerEmpty());
i = peersData.insert(id, newData); i = peersData.emplace(id, std::move(newData)).first;
} }
switch (restriction) { switch (restriction) {
case PeerData::MinimalLoaded: { case PeerData::MinimalLoaded: {
if (i.value()->loadedStatus == PeerData::NotLoaded) { if (i->second->loadedStatus == PeerData::NotLoaded) {
return nullptr; return nullptr;
} }
} break; } break;
case PeerData::FullLoaded: { case PeerData::FullLoaded: {
if (i.value()->loadedStatus != PeerData::FullLoaded) { if (i->second->loadedStatus != PeerData::FullLoaded) {
return nullptr; return nullptr;
} }
} break; } break;
} }
return i.value(); return i->second.get();
} }
void enumerateUsers(Fn<void(not_null<UserData*>)> action) { void enumerateUsers(Fn<void(not_null<UserData*>)> action) {
for_const (const auto peer, peersData) { for (const auto &[peerId, peer] : peersData) {
if (const auto user = peer->asUser()) { if (const auto user = peer->asUser()) {
action(user); action(user);
} }
@ -1116,9 +1118,9 @@ namespace App {
void enumerateChatsChannels( void enumerateChatsChannels(
Fn<void(not_null<PeerData*>)> action) { Fn<void(not_null<PeerData*>)> action) {
for_const (const auto peer, peersData) { for (const auto &[peerId, peer] : peersData) {
if (!peer->isUser()) { if (!peer->isUser()) {
action(peer); action(peer.get());
} }
} }
} }
@ -1128,10 +1130,10 @@ namespace App {
} }
PeerData *peerByName(const QString &username) { PeerData *peerByName(const QString &username) {
QString uname(username.trimmed()); const auto uname = username.trimmed();
for_const (PeerData *peer, peersData) { for (const auto &[peerId, peer] : peersData) {
if (!peer->userName().compare(uname, Qt::CaseInsensitive)) { if (!peer->userName().compare(uname, Qt::CaseInsensitive)) {
return peer; return peer.get();
} }
} }
return nullptr; return nullptr;
@ -1246,24 +1248,16 @@ namespace App {
void historyClearMsgs() { void historyClearMsgs() {
::dependentItems.clear(); ::dependentItems.clear();
QVector<HistoryItem*> toDelete; const auto oldData = base::take(msgsData);
for_const (auto item, msgsData) { const auto oldChannelData = base::take(channelMsgsData);
if (!item->mainView()) { for (const auto item : oldData) {
toDelete.push_back(item);
}
}
for_const (auto &chMsgsData, channelMsgsData) {
for_const (auto item, chMsgsData) {
if (!item->mainView()) {
toDelete.push_back(item);
}
}
}
msgsData.clear();
channelMsgsData.clear();
for_const (auto item, toDelete) {
delete item; delete item;
} }
for (const auto &data : oldChannelData) {
for (const auto item : data) {
delete item;
}
}
clearMousedItems(); clearMousedItems();
} }
@ -1275,10 +1269,7 @@ namespace App {
cSetSavedPeersByTime(SavedPeersByTime()); cSetSavedPeersByTime(SavedPeersByTime());
cSetRecentInlineBots(RecentInlineBots()); cSetRecentInlineBots(RecentInlineBots());
for_const (auto peer, ::peersData) { peersData.clear();
delete peer;
}
::peersData.clear();
if (AuthSession::Exists()) { if (AuthSession::Exists()) {
Auth().api().clearWebPageRequests(); Auth().api().clearWebPageRequests();

View File

@ -99,8 +99,10 @@ not_null<History*> Histories::findOrInsert(PeerId peerId) {
} }
void Histories::clear() { void Histories::clear() {
for (const auto &[peerId, history] : _map) {
history->unloadBlocks();
}
App::historyClearMsgs(); App::historyClearMsgs();
_map.clear(); _map.clear();
_unreadFull = _unreadMuted = 0; _unreadFull = _unreadMuted = 0;