From 48e2a5472e1552461f7d27af4fca1ffcbcb3167b Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 31 Aug 2017 20:53:03 +0300 Subject: [PATCH] Return not_null in App::history(). --- Telegram/SourceFiles/app.cpp | 2 +- Telegram/SourceFiles/app.h | 4 ++-- Telegram/SourceFiles/history/history.cpp | 19 +++++++++++++++++-- Telegram/SourceFiles/history/history.h | 7 +++++-- .../history/history_inner_widget.cpp | 4 ++-- .../SourceFiles/history/history_widget.cpp | 6 +++--- Telegram/SourceFiles/mainwidget.cpp | 7 +++++-- Telegram/SourceFiles/mainwidget.h | 2 +- Telegram/SourceFiles/mainwindow.cpp | 2 +- .../media/player/media_player_instance.cpp | 4 ++-- Telegram/SourceFiles/overviewwidget.cpp | 4 ++-- Telegram/SourceFiles/overviewwidget.h | 3 ++- .../profile/profile_block_shared_media.cpp | 2 +- 13 files changed, 44 insertions(+), 22 deletions(-) diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp index 1561f55f53..d4b7e8a929 100644 --- a/Telegram/SourceFiles/app.cpp +++ b/Telegram/SourceFiles/app.cpp @@ -1858,7 +1858,7 @@ namespace { return ::histories; } - History *history(const PeerId &peer) { + not_null history(const PeerId &peer) { return ::histories.findOrInsert(peer); } diff --git a/Telegram/SourceFiles/app.h b/Telegram/SourceFiles/app.h index fe267ed0ce..531abac00f 100644 --- a/Telegram/SourceFiles/app.h +++ b/Telegram/SourceFiles/app.h @@ -156,11 +156,11 @@ namespace App { MTPPhoto photoFromUserPhoto(MTPint userId, MTPint date, const MTPUserProfilePhoto &photo); Histories &histories(); - History *history(const PeerId &peer); + not_null history(const PeerId &peer); History *historyFromDialog(const PeerId &peer, int32 unreadCnt, int32 maxInboxRead, int32 maxOutboxRead); History *historyLoaded(const PeerId &peer); HistoryItem *histItemById(ChannelId channelId, MsgId itemId); - inline History *history(const PeerData *peer) { + inline not_null history(const PeerData *peer) { Assert(peer != nullptr); return history(peer->id); } diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index a4a17b30b1..0867d6221f 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -589,7 +589,7 @@ History *Histories::find(const PeerId &peerId) { return (i == map.cend()) ? 0 : i.value(); } -History *Histories::findOrInsert(const PeerId &peerId) { +not_null Histories::findOrInsert(const PeerId &peerId) { auto i = map.constFind(peerId); if (i == map.cend()) { auto history = peerIsChannel(peerId) ? static_cast(new ChannelHistory(peerId)) : (new History(peerId)); @@ -598,7 +598,7 @@ History *Histories::findOrInsert(const PeerId &peerId) { return i.value(); } -History *Histories::findOrInsert(const PeerId &peerId, int32 unreadCount, int32 maxInboxRead, int32 maxOutboxRead) { +not_null Histories::findOrInsert(const PeerId &peerId, int32 unreadCount, int32 maxInboxRead, int32 maxOutboxRead) { auto i = map.constFind(peerId); if (i == map.cend()) { auto history = peerIsChannel(peerId) ? static_cast(new ChannelHistory(peerId)) : (new History(peerId)); @@ -2099,6 +2099,21 @@ const ChannelHistory *History::asChannelHistory() const { return isChannel() ? static_cast(this) : 0; } +not_null History::migrateToOrMe() const { + if (auto to = peer->migrateTo()) { + return App::history(to); + } + // We could get it by App::history(peer), but we optimize. + return const_cast(this); +} + +History *History::migrateFrom() const { + if (auto from = peer->migrateFrom()) { + return App::history(from); + } + return nullptr; +} + bool History::isDisplayedEmpty() const { return isEmpty() || ((blocks.size() == 1) && blocks.front()->items.size() == 1 && blocks.front()->items.front()->isEmpty()); } diff --git a/Telegram/SourceFiles/history/history.h b/Telegram/SourceFiles/history/history.h index 6ff1e1fea1..6295562c0c 100644 --- a/Telegram/SourceFiles/history/history.h +++ b/Telegram/SourceFiles/history/history.h @@ -53,8 +53,8 @@ public: void step_typings(TimeMs ms, bool timer); History *find(const PeerId &peerId); - History *findOrInsert(const PeerId &peerId); - History *findOrInsert(const PeerId &peerId, int32 unreadCount, int32 maxInboxRead, int32 maxOutboxRead); + not_null findOrInsert(const PeerId &peerId); + not_null findOrInsert(const PeerId &peerId, int32 unreadCount, int32 maxInboxRead, int32 maxOutboxRead); void clear(); void remove(const PeerId &peer); @@ -210,6 +210,9 @@ public: ChannelHistory *asChannelHistory(); const ChannelHistory *asChannelHistory() const; + not_null migrateToOrMe() const; + History *migrateFrom() const; + bool isEmpty() const { return blocks.isEmpty(); } diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index bd06d725f1..f83b174db2 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -87,7 +87,7 @@ int BinarySearchBlocksOrItems(const T &list, int edge) { HistoryInner::HistoryInner(HistoryWidget *historyWidget, not_null controller, Ui::ScrollArea *scroll, History *history) : TWidget(nullptr) , _controller(controller) , _peer(history->peer) -, _migrated(history->peer->migrateFrom() ? App::history(history->peer->migrateFrom()->id) : nullptr) +, _migrated(history->migrateFrom()) , _history(history) , _widget(historyWidget) , _scroll(scroll) @@ -2333,7 +2333,7 @@ void HistoryInner::notifyIsBotChanged() { } void HistoryInner::notifyMigrateUpdated() { - _migrated = _peer->migrateFrom() ? App::history(_peer->migrateFrom()->id) : 0; + _migrated = _history->migrateFrom(); } int HistoryInner::moveScrollFollowingInlineKeyboard(const HistoryItem *item, int oldKeyboardTop, int newKeyboardTop) { diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index b753fc0217..f16395e8a3 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -1526,7 +1526,7 @@ void HistoryWidget::notify_migrateUpdated(PeerData *peer) { if (peer->migrateTo()) { showHistory(peer->migrateTo()->id, (_showAtMsgId > 0) ? (-_showAtMsgId) : _showAtMsgId, true); } else if ((_migrated ? _migrated->peer : 0) != peer->migrateFrom()) { - History *migrated = peer->migrateFrom() ? App::history(peer->migrateFrom()->id) : 0; + auto migrated = _history->migrateFrom(); if (_migrated || (migrated && migrated->unreadCount() > 0)) { showHistory(peer->id, peer->migrateFrom() ? _showAtMsgId : ((_showAtMsgId < 0 && -_showAtMsgId < ServerMaxMsgId) ? ShowAtUnreadMsgId : _showAtMsgId), true); } else { @@ -1864,8 +1864,8 @@ void HistoryWidget::showHistory(const PeerId &peerId, MsgId showAtMsgId, bool re _serviceImageCacheSize = imageCacheSize(); Auth().downloader().clearPriorities(); - _history = App::history(_peer->id); - _migrated = _peer->migrateFrom() ? App::history(_peer->migrateFrom()->id) : 0; + _history = App::history(_peer); + _migrated = _history->migrateFrom(); if (_channel) { updateNotifySettings(); diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 076f3d2cd9..d1f7a1f870 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -1645,7 +1645,7 @@ void MainWidget::checkLastUpdate(bool afterSleep) { } } -void MainWidget::overviewLoaded(History *history, const MTPmessages_Messages &result, mtpRequestId req) { +void MainWidget::overviewLoaded(not_null history, const MTPmessages_Messages &result, mtpRequestId req) { OverviewsPreload::iterator it; MediaOverviewType type = OverviewCount; for (int32 i = 0; i < OverviewCount; ++i) { @@ -4009,7 +4009,10 @@ void MainWidget::onSelfParticipantUpdated(ChannelData *channel) { auto history = App::historyLoaded(channel->id); if (_updatedChannels.contains(channel)) { _updatedChannels.remove(channel); - if ((history ? history : App::history(channel->id))->isEmpty()) { + if (!history) { + history = App::history(channel); + } + if (history->isEmpty()) { checkPeerHistory(channel); } else { history->asChannelHistory()->checkJoinedMessage(true); diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h index e8578466e4..af50b0f2ef 100644 --- a/Telegram/SourceFiles/mainwidget.h +++ b/Telegram/SourceFiles/mainwidget.h @@ -515,7 +515,7 @@ private: void readRequestDone(PeerData *peer); void messagesAffected(PeerData *peer, const MTPmessages_AffectedMessages &result); - void overviewLoaded(History *history, const MTPmessages_Messages &result, mtpRequestId req); + void overviewLoaded(not_null history, const MTPmessages_Messages &result, mtpRequestId req); void mediaOverviewUpdated(const Notify::PeerUpdate &update); Window::SectionSlideParams prepareShowAnimation(bool willHaveTopBarShadow, bool willHaveTabbedSection); diff --git a/Telegram/SourceFiles/mainwindow.cpp b/Telegram/SourceFiles/mainwindow.cpp index 92ec368070..0f0b4e7b78 100644 --- a/Telegram/SourceFiles/mainwindow.cpp +++ b/Telegram/SourceFiles/mainwindow.cpp @@ -267,7 +267,7 @@ void MainWindow::setupIntro() { void MainWindow::serviceNotification(const TextWithEntities &message, const MTPMessageMedia &media, int32 date, bool force) { if (date <= 0) date = unixtime(); - auto h = (_main && App::userLoaded(ServiceUserId)) ? App::history(ServiceUserId) : nullptr; + auto h = (_main && App::userLoaded(ServiceUserId)) ? App::history(ServiceUserId).get() : nullptr; if (!h || (!force && h->isEmpty())) { _delayedServiceMsgs.push_back(DelayedServiceMsg(message, media, date)); return sendServiceHistoryRequest(); diff --git a/Telegram/SourceFiles/media/player/media_player_instance.cpp b/Telegram/SourceFiles/media/player/media_player_instance.cpp index 53e9857ee0..501f89c5c2 100644 --- a/Telegram/SourceFiles/media/player/media_player_instance.cpp +++ b/Telegram/SourceFiles/media/player/media_player_instance.cpp @@ -131,8 +131,8 @@ void Instance::setCurrent(const AudioMsgId &audioId) { auto migrated = data->migrated; auto item = data->current ? App::histItemById(data->current.contextId()) : nullptr; if (item) { - data->history = item->history()->peer->migrateTo() ? App::history(item->history()->peer->migrateTo()) : item->history(); - data->migrated = data->history->peer->migrateFrom() ? App::history(data->history->peer->migrateFrom()) : nullptr; + data->history = item->history()->migrateToOrMe(); + data->migrated = data->history->migrateFrom(); } else { data->history = nullptr; data->migrated = nullptr; diff --git a/Telegram/SourceFiles/overviewwidget.cpp b/Telegram/SourceFiles/overviewwidget.cpp index 34c22e544e..71f994483f 100644 --- a/Telegram/SourceFiles/overviewwidget.cpp +++ b/Telegram/SourceFiles/overviewwidget.cpp @@ -55,8 +55,8 @@ OverviewInner::OverviewInner(OverviewWidget *overview, Ui::ScrollArea *scroll, P , _peer(peer->migrateTo() ? peer->migrateTo() : peer) , _type(type) , _reversed(_type != OverviewFiles && _type != OverviewLinks) -, _migrated(_peer->migrateFrom() ? App::history(_peer->migrateFrom()->id) : 0) -, _history(App::history(_peer->id)) +, _history(App::history(_peer)) +, _migrated(_history->migrateFrom()) , _channel(peerToChannel(_peer->id)) , _rowWidth(st::msgMinWidth) , _search(this, st::overviewFilter, langFactory(lng_dlg_filter)) diff --git a/Telegram/SourceFiles/overviewwidget.h b/Telegram/SourceFiles/overviewwidget.h index bc0ad184bc..adde682491 100644 --- a/Telegram/SourceFiles/overviewwidget.h +++ b/Telegram/SourceFiles/overviewwidget.h @@ -177,7 +177,8 @@ private: PeerData *_peer; MediaOverviewType _type; bool _reversed; - History *_migrated, *_history; + History *_history; + History *_migrated; ChannelId _channel; bool _selMode = false; diff --git a/Telegram/SourceFiles/profile/profile_block_shared_media.cpp b/Telegram/SourceFiles/profile/profile_block_shared_media.cpp index 8841c56058..ba852b4706 100644 --- a/Telegram/SourceFiles/profile/profile_block_shared_media.cpp +++ b/Telegram/SourceFiles/profile/profile_block_shared_media.cpp @@ -51,7 +51,7 @@ QString getButtonText(MediaOverviewType type, int count) { SharedMediaWidget::SharedMediaWidget(QWidget *parent, PeerData *peer) : BlockWidget(parent, peer, lang(lng_profile_shared_media)) , _history(App::history(peer)) -, _migrated(peer->migrateFrom() ? App::history(peer->migrateFrom()) : nullptr) { +, _migrated(_history->migrateFrom()) { auto observeEvents = Notify::PeerUpdate::Flag::SharedMediaChanged | Notify::PeerUpdate::Flag::UserCommonChatsChanged; subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(observeEvents, [this](const Notify::PeerUpdate &update) {