From 9cccea9a871ea802f8a4dbdad43a9b96fd0752d8 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 18 Feb 2020 16:15:43 +0400 Subject: [PATCH] All read history done through Data::Histories. --- Telegram/SourceFiles/apiwrap.cpp | 80 +------------------ Telegram/SourceFiles/apiwrap.h | 15 ---- Telegram/SourceFiles/data/data_histories.cpp | 5 +- Telegram/SourceFiles/data/data_histories.h | 1 + Telegram/SourceFiles/history/history.cpp | 47 +++++++++-- Telegram/SourceFiles/history/history.h | 2 +- .../SourceFiles/window/window_peer_menu.cpp | 2 +- 7 files changed, 48 insertions(+), 104 deletions(-) diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 860489070..9e6d5d085 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -4432,7 +4432,7 @@ void ApiWrap::userPhotosDone( //} void ApiWrap::sendAction(const SendAction &action) { - readServerHistory(action.history); + action.history->readInbox(); action.history->getReadyFor(ShowAtTheEndMsgId); _sendActions.fire_copy(action); } @@ -4459,7 +4459,7 @@ void ApiWrap::forwardMessages( const auto history = action.history; const auto peer = history->peer; - readServerHistory(history); + history->readInbox(); const auto channelPost = peer->isChannel() && !peer->isMegagroup(); const auto silentPost = action.options.silent @@ -6000,46 +6000,6 @@ void ApiWrap::reloadPollResults(not_null item) { _pollReloadRequestIds.emplace(itemId, requestId); } -void ApiWrap::readServerHistory(not_null history) { - if (history->unreadCount()) { - readServerHistoryForce(history); - } - if (history->unreadMark()) { - changeDialogUnreadMark(history, false); - } -} - -void ApiWrap::readServerHistoryForce( - not_null history, - MsgId upTo) { - const auto peer = history->peer; - if (!upTo) { - upTo = history->readInbox(); - if (!upTo) { - return; - } - } - if (const auto channel = peer->asChannel()) { - if (!channel->amIn()) { - return; // no read request for channels that I didn't join - } else if (const auto migrateFrom = channel->migrateFrom()) { - if (const auto migrated = _session->data().historyLoaded(migrateFrom)) { - readServerHistory(migrated); - } - } - } - - if (_readRequests.contains(peer)) { - const auto i = _readRequestsPending.find(peer); - if (i == _readRequestsPending.cend()) { - _readRequestsPending.emplace(peer, upTo); - } else if (i->second < upTo) { - i->second = upTo; - } - } else { - sendReadRequest(peer, upTo); - } -} // // #feed //void ApiWrap::readFeed( // not_null feed, @@ -6093,39 +6053,3 @@ void ApiWrap::readServerHistoryForce( // _feedReadTimer.callOnce(delay); // } //} - -void ApiWrap::sendReadRequest(not_null peer, MsgId upTo) { - const auto requestId = [&] { - const auto finished = [=] { - _readRequests.remove(peer); - if (const auto next = _readRequestsPending.take(peer)) { - sendReadRequest(peer, *next); - } else if (const auto history - = _session->data().historyLoaded(peer)) { - if (!history->unreadCountKnown()) { - requestDialogEntry(history); - } - } - }; - if (const auto channel = peer->asChannel()) { - return request(MTPchannels_ReadHistory( - channel->inputChannel, - MTP_int(upTo) - )).done([=](const MTPBool &result) { - finished(); - }).fail([=](const RPCError &error) { - finished(); - }).send(); - } - return request(MTPmessages_ReadHistory( - peer->input, - MTP_int(upTo) - )).done([=](const MTPmessages_AffectedMessages &result) { - applyAffectedMessages(peer, result); - finished(); - }).fail([=](const RPCError &error) { - finished(); - }).send(); - }(); - _readRequests.emplace(peer, requestId, upTo); -} diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index 137169659..f7bddf0af 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -389,8 +389,6 @@ public: const QString &lastName, const SendAction &action); void shareContact(not_null user, const SendAction &action); - void readServerHistory(not_null history); - void readServerHistoryForce(not_null history, MsgId upTo = 0); //void readFeed( // #feed // not_null feed, // Data::MessagePosition position); @@ -626,7 +624,6 @@ private: not_null peer, bool justClear, bool revoke); - void sendReadRequest(not_null peer, MsgId upTo); int applyAffectedHistory( not_null peer, const MTPmessages_AffectedHistory &result); @@ -800,18 +797,6 @@ private: rpl::event_stream _sendActions; - struct ReadRequest { - ReadRequest(mtpRequestId requestId, MsgId upTo) - : requestId(requestId) - , upTo(upTo) { - } - - mtpRequestId requestId = 0; - MsgId upTo = 0; - }; - base::flat_map, ReadRequest> _readRequests; - base::flat_map, MsgId> _readRequestsPending; - std::unique_ptr _fileLoader; base::flat_map> _sendingAlbums; diff --git a/Telegram/SourceFiles/data/data_histories.cpp b/Telegram/SourceFiles/data/data_histories.cpp index fa30a0743..9c8dc7d82 100644 --- a/Telegram/SourceFiles/data/data_histories.cpp +++ b/Telegram/SourceFiles/data/data_histories.cpp @@ -98,7 +98,10 @@ void Histories::readInboxTill( return; } } - const auto tillId = item->id; + readInboxTill(history, item->id); +} + +void Histories::readInboxTill(not_null history, MsgId tillId) { if (!history->readInboxTillNeedsRequest(tillId)) { return; } diff --git a/Telegram/SourceFiles/data/data_histories.h b/Telegram/SourceFiles/data/data_histories.h index de62ef09d..ebf6f8648 100644 --- a/Telegram/SourceFiles/data/data_histories.h +++ b/Telegram/SourceFiles/data/data_histories.h @@ -36,6 +36,7 @@ public: void readInboxTill( not_null history, not_null item); + void readInboxTill(not_null history, MsgId tillId); void sendPendingReadInbox(not_null history); private: diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 325bae8f5..c7be764d9 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -1614,16 +1614,47 @@ void History::readClientSideMessages() { } } -MsgId History::readInbox() { - const auto upTo = msgIdForRead(); - readClientSideMessages(); - if (unreadCountKnown()) { - setUnreadCount(0); +void History::readInbox() { + if (_lastMessage) { + if (!*_lastMessage) { + owner().histories().readInboxTill(this, 0); + return; + } else if (IsServerMsgId((*_lastMessage)->id)) { + readInboxTill(*_lastMessage); + return; + } } - if (upTo) { - inboxRead(upTo); + if (loadedAtBottom()) { + const auto last = [&]() -> HistoryItem* { + for (const auto &block : ranges::view::reverse(blocks)) { + const auto &messages = block->messages; + for (const auto &item : ranges::view::reverse(messages)) { + if (IsServerMsgId(item->data()->id)) { + return item->data(); + } + } + } + return nullptr; + }(); + if (last) { + readInboxTill(last); + return; + } else if (loadedAtTop()) { + owner().histories().readInboxTill(this, 0); + return; + } } - return upTo; + session().api().requestDialogEntry(this, [=] { + Expects(_lastMessage.has_value()); + + if (!*_lastMessage) { + owner().histories().readInboxTill(this, 0); + } else if (IsServerMsgId((*_lastMessage)->id)) { + readInboxTill(*_lastMessage); + } else { + Unexpected("Local _lastMessage after requestDialogEntry."); + } + }); } void History::readInboxTill(not_null item) { diff --git a/Telegram/SourceFiles/history/history.h b/Telegram/SourceFiles/history/history.h index 9b57e4d62..47d833970 100644 --- a/Telegram/SourceFiles/history/history.h +++ b/Telegram/SourceFiles/history/history.h @@ -158,7 +158,7 @@ public: void unregisterLocalMessage(not_null item); [[nodiscard]] HistoryItem *latestSendingMessage() const; - MsgId readInbox(); + void readInbox(); void readInboxTill(not_null item); [[nodiscard]] bool readInboxTillNeedsRequest(MsgId tillId); void applyInboxReadUpdate( diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp index 5fa03afab..47881396a 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.cpp +++ b/Telegram/SourceFiles/window/window_peer_menu.cpp @@ -303,7 +303,7 @@ void Filler::addToggleUnreadMark() { const auto markAsRead = isUnread(peer); const auto handle = [&](not_null history) { if (markAsRead) { - peer->session().api().readServerHistory(history); + history->readInbox(); } else { peer->session().api().changeDialogUnreadMark( history,