Merge group-supergroup history in jump-to-date.

Fixes #4094.
This commit is contained in:
John Preston 2017-11-30 16:50:13 +04:00
parent 13ab055fe0
commit c10588a7dc
3 changed files with 82 additions and 18 deletions

View File

@ -1960,7 +1960,11 @@ void ApiWrap::applyUpdateNoPtsCheck(const MTPUpdate &update) {
}
}
void ApiWrap::jumpToDate(not_null<PeerData*> peer, const QDate &date) {
template <typename Callback>
void ApiWrap::requestMessageAfterDate(
not_null<PeerData*> 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<PeerData*> 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>(callback)
](const MTPmessages_Messages &result) {
auto getMessagesList = [&result, peer]() -> const QVector<MTPMessage>* {
auto handleMessages = [](auto &messages) {
App::feedUsers(messages.vusers);
@ -2011,15 +2019,39 @@ void ApiWrap::jumpToDate(not_null<PeerData*> 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<PeerData*> 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*> history) {
auto fullCount = history->getUnreadMentionsCount();
auto loadedCount = history->getUnreadMentionsLoadedCount();

View File

@ -216,6 +216,12 @@ private:
void saveChatAdmins(not_null<ChatData*> chat);
void sendSaveChatAdminsRequests(not_null<ChatData*> chat);
template <typename Callback>
void requestMessageAfterDate(
not_null<PeerData*> peer,
const QDate &date,
Callback &&callback);
int applyAffectedHistory(
not_null<PeerData*> peer,
const MTPmessages_AffectedHistory &result);

View File

@ -280,7 +280,10 @@ void Controller::showJumpToDate(not_null<PeerData*> peer, QDate requestedDate) {
}
return QDate::currentDate();
};
auto maxPeerDate = [peer] {
auto maxPeerDate = [](not_null<PeerData*> 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<PeerData*> 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<PeerData*> 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<CalendarBox>(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<CalendarBox>(
month,
highlighted,
std::move(callback));
box->setMinDate(minPeerDate(peer));
box->setMaxDate(maxPeerDate(peer));
Ui::show(std::move(box));
}