Return not_null<History*> in App::history().

This commit is contained in:
John Preston 2017-08-31 20:53:03 +03:00
parent 5a20014b1a
commit 48e2a5472e
13 changed files with 44 additions and 22 deletions

View File

@ -1858,7 +1858,7 @@ namespace {
return ::histories;
}
History *history(const PeerId &peer) {
not_null<History*> history(const PeerId &peer) {
return ::histories.findOrInsert(peer);
}

View File

@ -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*> 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*> history(const PeerData *peer) {
Assert(peer != nullptr);
return history(peer->id);
}

View File

@ -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<History*> Histories::findOrInsert(const PeerId &peerId) {
auto i = map.constFind(peerId);
if (i == map.cend()) {
auto history = peerIsChannel(peerId) ? static_cast<History*>(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<History*> 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<History*>(new ChannelHistory(peerId)) : (new History(peerId));
@ -2099,6 +2099,21 @@ const ChannelHistory *History::asChannelHistory() const {
return isChannel() ? static_cast<const ChannelHistory*>(this) : 0;
}
not_null<History*> 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<History*>(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());
}

View File

@ -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<History*> findOrInsert(const PeerId &peerId);
not_null<History*> 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<History*> migrateToOrMe() const;
History *migrateFrom() const;
bool isEmpty() const {
return blocks.isEmpty();
}

View File

@ -87,7 +87,7 @@ int BinarySearchBlocksOrItems(const T &list, int edge) {
HistoryInner::HistoryInner(HistoryWidget *historyWidget, not_null<Window::Controller*> 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) {

View File

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

View File

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

View File

@ -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*> history, const MTPmessages_Messages &result, mtpRequestId req);
void mediaOverviewUpdated(const Notify::PeerUpdate &update);
Window::SectionSlideParams prepareShowAnimation(bool willHaveTopBarShadow, bool willHaveTabbedSection);

View File

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

View File

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

View File

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

View File

@ -177,7 +177,8 @@ private:
PeerData *_peer;
MediaOverviewType _type;
bool _reversed;
History *_migrated, *_history;
History *_history;
History *_migrated;
ChannelId _channel;
bool _selMode = false;

View File

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