From c10588a7dc248e427a6191841a68410f98904ac2 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 30 Nov 2017 16:50:13 +0400 Subject: [PATCH] Merge group-supergroup history in jump-to-date. Fixes #4094. --- Telegram/SourceFiles/apiwrap.cpp | 44 +++++++++++++--- Telegram/SourceFiles/apiwrap.h | 6 +++ .../SourceFiles/window/window_controller.cpp | 50 ++++++++++++++----- 3 files changed, 82 insertions(+), 18 deletions(-) diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 31b403edc2..88dbc7332d 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -1960,7 +1960,11 @@ void ApiWrap::applyUpdateNoPtsCheck(const MTPUpdate &update) { } } -void ApiWrap::jumpToDate(not_null peer, const QDate &date) { +template +void ApiWrap::requestMessageAfterDate( + not_null peer, + const QDate &date, + Callback &&callback) { // API returns a message with date <= offset_date. // So we request a message with offset_date = desired_date - 1 and add_offset = -1. // This should give us the first message with date >= desired_date. @@ -1980,7 +1984,11 @@ void ApiWrap::jumpToDate(not_null peer, const QDate &date) { MTP_int(maxId), MTP_int(minId), MTP_int(historyHash) - )).done([peer](const MTPmessages_Messages &result) { + )).done([ + peer, + offsetDate, + callback = std::forward(callback) + ](const MTPmessages_Messages &result) { auto getMessagesList = [&result, peer]() -> const QVector* { auto handleMessages = [](auto &messages) { App::feedUsers(messages.vusers); @@ -2011,15 +2019,39 @@ void ApiWrap::jumpToDate(not_null peer, const QDate &date) { if (auto list = getMessagesList()) { App::feedMsgs(*list, NewMessageExisting); for (auto &message : *list) { - auto id = idFromMessage(message); - Ui::showPeerHistory(peer, id); - return; + if (dateFromMessage(message) >= offsetDate) { + callback(idFromMessage(message)); + return; + } } } - Ui::showPeerHistory(peer, ShowAtUnreadMsgId); + callback(ShowAtUnreadMsgId); }).send(); } +void ApiWrap::jumpToDate(not_null peer, const QDate &date) { + if (auto channel = peer->migrateTo()) { + jumpToDate(channel, date); + return; + } + auto jumpToDateInPeer = [peer, date, this] { + requestMessageAfterDate(peer, date, [peer](MsgId resultId) { + Ui::showPeerHistory(peer, resultId); + }); + }; + if (auto chat = peer->migrateFrom()) { + requestMessageAfterDate(chat, date, [=](MsgId resultId) { + if (resultId) { + Ui::showPeerHistory(chat, resultId); + } else { + jumpToDateInPeer(); + } + }); + } else { + jumpToDateInPeer(); + } +} + void ApiWrap::preloadEnoughUnreadMentions(not_null history) { auto fullCount = history->getUnreadMentionsCount(); auto loadedCount = history->getUnreadMentionsLoadedCount(); diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index 2fb0273c5c..efca429c3e 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -216,6 +216,12 @@ private: void saveChatAdmins(not_null chat); void sendSaveChatAdminsRequests(not_null chat); + template + void requestMessageAfterDate( + not_null peer, + const QDate &date, + Callback &&callback); + int applyAffectedHistory( not_null peer, const MTPmessages_AffectedHistory &result); diff --git a/Telegram/SourceFiles/window/window_controller.cpp b/Telegram/SourceFiles/window/window_controller.cpp index 0a25a91075..7645531792 100644 --- a/Telegram/SourceFiles/window/window_controller.cpp +++ b/Telegram/SourceFiles/window/window_controller.cpp @@ -280,7 +280,10 @@ void Controller::showJumpToDate(not_null peer, QDate requestedDate) { } return QDate::currentDate(); }; - auto maxPeerDate = [peer] { + auto maxPeerDate = [](not_null peer) { + if (auto channel = peer->migrateTo()) { + peer = channel; + } if (auto history = App::historyLoaded(peer)) { if (!history->lastMsgDate.isNull()) { return history->lastMsgDate.date(); @@ -288,22 +291,45 @@ void Controller::showJumpToDate(not_null peer, QDate requestedDate) { } return QDate::currentDate(); }; - auto minPeerDate = [peer] { - if (auto history = App::historyLoaded(peer)) { - if (history->loadedAtTop()) { - if (history->isEmpty()) { - return QDate::currentDate(); + auto minPeerDate = [](not_null peer) { + const auto startDate = [] { + // Telegram was launched in August 2013 :) + return QDate(2013, 8, 1); + }; + if (auto chat = peer->migrateFrom()) { + if (auto history = App::historyLoaded(chat)) { + if (history->loadedAtTop()) { + if (!history->isEmpty()) { + return history->blocks.front()->items.front()->date.date(); + } + } else { + return startDate(); } - return history->blocks.front()->items.front()->date.date(); } } - return QDate(2013, 8, 1); // Telegram was launched in August 2013 :) + if (auto history = App::historyLoaded(peer)) { + if (history->loadedAtTop()) { + if (!history->isEmpty()) { + return history->blocks.front()->items.front()->date.date(); + } + return QDate::currentDate(); + } + } + return startDate(); }; - auto highlighted = requestedDate.isNull() ? currentPeerDate() : requestedDate; + auto highlighted = requestedDate.isNull() + ? currentPeerDate() + : requestedDate; auto month = highlighted; - auto box = Box(month, highlighted, [this, peer](const QDate &date) { Auth().api().jumpToDate(peer, date); }); - box->setMinDate(minPeerDate()); - box->setMaxDate(maxPeerDate()); + auto callback = [this, peer](const QDate &date) { + Auth().api().jumpToDate(peer, date); + }; + auto box = Box( + month, + highlighted, + std::move(callback)); + box->setMinDate(minPeerDate(peer)); + box->setMaxDate(maxPeerDate(peer)); Ui::show(std::move(box)); }