From 3d8dadc2e7d014d283e47a4f85fc6bc7dbf18cd7 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sun, 25 Sep 2016 22:04:02 +0300 Subject: [PATCH] Fixed reading issue in channels after getChannelDifference(). In App::history() readInboxBefore property was reset each time. --- Telegram/SourceFiles/app.cpp | 2 +- Telegram/SourceFiles/history.cpp | 17 +++-- Telegram/SourceFiles/history.h | 1 + Telegram/SourceFiles/mainwidget.cpp | 62 +++++++++---------- .../SourceFiles/settings/settings_widget.cpp | 6 ++ 5 files changed, 51 insertions(+), 37 deletions(-) diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp index 4f50262d87..e8d7636474 100644 --- a/Telegram/SourceFiles/app.cpp +++ b/Telegram/SourceFiles/app.cpp @@ -1875,7 +1875,7 @@ namespace { } History *history(const PeerId &peer) { - return ::histories.findOrInsert(peer, 0, 0, 0); + return ::histories.findOrInsert(peer); } History *historyFromDialog(const PeerId &peer, int32 unreadCnt, int32 maxInboxRead, int32 maxOutboxRead) { diff --git a/Telegram/SourceFiles/history.cpp b/Telegram/SourceFiles/history.cpp index 810914726d..be131950d0 100644 --- a/Telegram/SourceFiles/history.cpp +++ b/Telegram/SourceFiles/history.cpp @@ -574,6 +574,15 @@ History *Histories::find(const PeerId &peerId) { return (i == map.cend()) ? 0 : i.value(); } +History *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)); + i = map.insert(peerId, history); + } + return i.value(); +} + History *Histories::findOrInsert(const PeerId &peerId, int32 unreadCount, int32 maxInboxRead, int32 maxOutboxRead) { auto i = map.constFind(peerId); if (i == map.cend()) { @@ -584,10 +593,10 @@ History *Histories::findOrInsert(const PeerId &peerId, int32 unreadCount, int32 history->outboxReadBefore = maxOutboxRead + 1; } else { auto history = i.value(); - if (unreadCount >= history->unreadCount()) { + if (unreadCount > history->unreadCount()) { history->setUnreadCount(unreadCount); - history->inboxReadBefore = maxInboxRead + 1; } + accumulate_max(history->inboxReadBefore, maxInboxRead + 1); accumulate_max(history->outboxReadBefore, maxOutboxRead + 1); } return i.value(); @@ -1442,8 +1451,8 @@ MsgId History::inboxRead(MsgId upTo) { updateChatListEntry(); if (peer->migrateTo()) { - if (History *h = App::historyLoaded(peer->migrateTo()->id)) { - h->updateChatListEntry(); + if (auto migrateTo = App::historyLoaded(peer->migrateTo()->id)) { + migrateTo->updateChatListEntry(); } } diff --git a/Telegram/SourceFiles/history.h b/Telegram/SourceFiles/history.h index 72cc5f101e..b6119cd768 100644 --- a/Telegram/SourceFiles/history.h +++ b/Telegram/SourceFiles/history.h @@ -48,6 +48,7 @@ public: void step_typings(uint64 ms, bool timer); History *find(const PeerId &peerId); + History *findOrInsert(const PeerId &peerId); History *findOrInsert(const PeerId &peerId, int32 unreadCount, int32 maxInboxRead, int32 maxOutboxRead); void clear(); diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 7252dfce4a..45ea448aa5 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -2875,18 +2875,18 @@ void MainWidget::gotChannelDifference(ChannelData *channel, const MTPupdates_Cha bool isFinal = true; switch (diff.type()) { case mtpc_updates_channelDifferenceEmpty: { - const auto &d(diff.c_updates_channelDifferenceEmpty()); + auto &d = diff.c_updates_channelDifferenceEmpty(); if (d.has_timeout()) timeout = d.vtimeout.v; isFinal = d.is_final(); channel->ptsInit(d.vpts.v); } break; case mtpc_updates_channelDifferenceTooLong: { - const auto &d(diff.c_updates_channelDifferenceTooLong()); + auto &d = diff.c_updates_channelDifferenceTooLong(); App::feedUsers(d.vusers); App::feedChats(d.vchats); - History *h = App::historyLoaded(channel->id); + auto h = App::historyLoaded(channel->id); if (h) { h->setNotLoadedAtBottom(); } @@ -2912,7 +2912,7 @@ void MainWidget::gotChannelDifference(ChannelData *channel, const MTPupdates_Cha } break; case mtpc_updates_channelDifference: { - const auto &d(diff.c_updates_channelDifference()); + auto &d = diff.c_updates_channelDifference(); App::feedUsers(d.vusers); App::feedChats(d.vchats); @@ -2921,11 +2921,11 @@ void MainWidget::gotChannelDifference(ChannelData *channel, const MTPupdates_Cha feedMessageIds(d.vother_updates); // feed messages and groups, copy from App::feedMsgs - History *h = App::history(channel->id); - const auto &vmsgs(d.vnew_messages.c_vector().v); - QMap msgsIds; - for (int32 i = 0, l = vmsgs.size(); i < l; ++i) { - const auto &msg(vmsgs.at(i)); + auto h = App::history(channel->id); + auto &vmsgs = d.vnew_messages.c_vector().v; + QMap msgsIds; + for (int i = 0, l = vmsgs.size(); i < l; ++i) { + auto &msg = vmsgs[i]; switch (msg.type()) { case mtpc_message: { const auto &d(msg.c_message()); @@ -2939,9 +2939,9 @@ void MainWidget::gotChannelDifference(ChannelData *channel, const MTPupdates_Cha case mtpc_messageService: msgsIds.insert((uint64(uint32(msg.c_messageService().vid.v)) << 32) | uint64(i), i + 1); break; } } - for (QMap::const_iterator i = msgsIds.cbegin(), e = msgsIds.cend(); i != e; ++i) { - if (i.value() > 0) { // add message - const auto &msg(vmsgs.at(i.value() - 1)); + for_const (auto msgIndex, msgsIds) { + if (msgIndex > 0) { // add message + auto &msg = vmsgs.at(msgIndex - 1); if (channel->id != peerFromMessage(msg)) { LOG(("API Error: message with invalid peer returned in channelDifference, channelId: %1, peer: %2").arg(peerToChannel(channel->id)).arg(peerFromMessage(msg))); continue; // wtf @@ -4501,13 +4501,13 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } break; case mtpc_updatePrivacy: { - const auto &d(update.c_updatePrivacy()); + auto &d = update.c_updatePrivacy(); } break; /////// Channel updates case mtpc_updateChannel: { - const auto &d(update.c_updateChannel()); - if (ChannelData *channel = App::channelLoaded(d.vchannel_id.v)) { + auto &d = update.c_updateChannel(); + if (auto channel = App::channelLoaded(d.vchannel_id.v)) { App::markPeerUpdated(channel); channel->inviter = 0; if (!channel->amIn()) { @@ -4521,7 +4521,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { case mtpc_updateNewChannelMessage: { auto &d = update.c_updateNewChannelMessage(); - ChannelData *channel = App::channelLoaded(peerToChannel(peerFromMessage(d.vmessage))); + auto channel = App::channelLoaded(peerToChannel(peerFromMessage(d.vmessage))); DataIsLoadedResult isDataLoaded = allDataLoadedForMessage(d.vmessage); if (!requestingDifference() && (!channel || isDataLoaded != DataIsLoadedResult::Ok)) { MTP_LOG(0, ("getDifference { good - after not all data loaded in updateNewChannelMessage }%1").arg(cTestMode() ? " TESTMODE" : "")); @@ -4556,8 +4556,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } } if (needToAdd) { - HistoryItem *item = App::histories().addNewMessage(d.vmessage, NewMessageUnread); - if (item) { + if (auto item = App::histories().addNewMessage(d.vmessage, NewMessageUnread)) { _history->peerMessagesUpdated(item->history()->peer->id); } } @@ -4567,8 +4566,8 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } break; case mtpc_updateEditChannelMessage: { - const auto &d(update.c_updateEditChannelMessage()); - ChannelData *channel = App::channelLoaded(peerToChannel(peerFromMessage(d.vmessage))); + auto &d = update.c_updateEditChannelMessage(); + auto channel = App::channelLoaded(peerToChannel(peerFromMessage(d.vmessage))); if (channel && !_handlingChannelDifference) { if (channel->ptsRequesting()) { // skip global updates while getting channel difference @@ -4587,7 +4586,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } break; case mtpc_updateEditMessage: { - const auto &d(update.c_updateEditMessage()); + auto &d = update.c_updateEditMessage(); if (!ptsUpdated(d.vpts.v, d.vpts_count.v, update)) { return; @@ -4600,9 +4599,9 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } break; case mtpc_updateChannelPinnedMessage: { - const auto &d(update.c_updateChannelPinnedMessage()); + auto &d = update.c_updateChannelPinnedMessage(); - if (ChannelData *channel = App::channelLoaded(d.vchannel_id.v)) { + if (auto channel = App::channelLoaded(d.vchannel_id.v)) { if (channel->isMegagroup()) { channel->mgInfo->pinnedMsgId = d.vid.v; if (App::api()) { @@ -4613,13 +4612,12 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } break; case mtpc_updateReadChannelInbox: { - auto &d(update.c_updateReadChannelInbox()); - auto channel = App::channelLoaded(d.vchannel_id.v); + auto &d = update.c_updateReadChannelInbox(); App::feedInboxRead(peerFromChannel(d.vchannel_id.v), d.vmax_id.v); } break; case mtpc_updateReadChannelOutbox: { - auto &d(update.c_updateReadChannelOutbox()); + auto &d = update.c_updateReadChannelOutbox(); auto peerId = peerFromChannel(d.vchannel_id.v); auto when = requestingDifference() ? 0 : unixtime(); App::feedOutboxRead(peerId, d.vmax_id.v, when); @@ -4629,8 +4627,8 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } break; case mtpc_updateDeleteChannelMessages: { - const auto &d(update.c_updateDeleteChannelMessages()); - ChannelData *channel = App::channelLoaded(d.vchannel_id.v); + auto &d = update.c_updateDeleteChannelMessages(); + auto channel = App::channelLoaded(d.vchannel_id.v); if (channel && !_handlingChannelDifference) { if (channel->ptsRequesting()) { // skip global updates while getting channel difference @@ -4650,8 +4648,8 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } break; case mtpc_updateChannelTooLong: { - const auto &d(update.c_updateChannelTooLong()); - if (ChannelData *channel = App::channelLoaded(d.vchannel_id.v)) { + auto &d = update.c_updateChannelTooLong(); + if (auto channel = App::channelLoaded(d.vchannel_id.v)) { if (!d.has_pts() || channel->pts() < d.vpts.v) { getChannelDifference(channel); } @@ -4659,8 +4657,8 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } break; case mtpc_updateChannelMessageViews: { - const auto &d(update.c_updateChannelMessageViews()); - if (HistoryItem *item = App::histItemById(d.vchannel_id.v, d.vid.v)) { + auto &d = update.c_updateChannelMessageViews(); + if (auto item = App::histItemById(d.vchannel_id.v, d.vid.v)) { item->setViewsCount(d.vviews.v); } } break; diff --git a/Telegram/SourceFiles/settings/settings_widget.cpp b/Telegram/SourceFiles/settings/settings_widget.cpp index 5c591cb000..114883840c 100644 --- a/Telegram/SourceFiles/settings/settings_widget.cpp +++ b/Telegram/SourceFiles/settings/settings_widget.cpp @@ -26,6 +26,7 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org #include "styles/style_settings.h" #include "ui/scrollarea.h" #include "mainwindow.h" +#include "mainwidget.h" #include "localstorage.h" #include "boxes/confirmbox.h" #include "application.h" @@ -80,6 +81,11 @@ void fillCodes() { }); Ui::showLayer(box.release()); }); + Codes.insert(qsl("getdifference"), []() { + if (auto main = App::main()) { + main->getDifference(); + } + }); } void codesFeedString(const QString &text) {