All read history done through Data::Histories.

This commit is contained in:
John Preston 2020-02-18 16:15:43 +04:00
parent b0e1ae3948
commit 9cccea9a87
7 changed files with 48 additions and 104 deletions

View File

@ -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<HistoryItem*> item) {
_pollReloadRequestIds.emplace(itemId, requestId);
}
void ApiWrap::readServerHistory(not_null<History*> history) {
if (history->unreadCount()) {
readServerHistoryForce(history);
}
if (history->unreadMark()) {
changeDialogUnreadMark(history, false);
}
}
void ApiWrap::readServerHistoryForce(
not_null<History*> 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<Data::Feed*> feed,
@ -6093,39 +6053,3 @@ void ApiWrap::readServerHistoryForce(
// _feedReadTimer.callOnce(delay);
// }
//}
void ApiWrap::sendReadRequest(not_null<PeerData*> 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);
}

View File

@ -389,8 +389,6 @@ public:
const QString &lastName,
const SendAction &action);
void shareContact(not_null<UserData*> user, const SendAction &action);
void readServerHistory(not_null<History*> history);
void readServerHistoryForce(not_null<History*> history, MsgId upTo = 0);
//void readFeed( // #feed
// not_null<Data::Feed*> feed,
// Data::MessagePosition position);
@ -626,7 +624,6 @@ private:
not_null<PeerData*> peer,
bool justClear,
bool revoke);
void sendReadRequest(not_null<PeerData*> peer, MsgId upTo);
int applyAffectedHistory(
not_null<PeerData*> peer,
const MTPmessages_AffectedHistory &result);
@ -800,18 +797,6 @@ private:
rpl::event_stream<SendAction> _sendActions;
struct ReadRequest {
ReadRequest(mtpRequestId requestId, MsgId upTo)
: requestId(requestId)
, upTo(upTo) {
}
mtpRequestId requestId = 0;
MsgId upTo = 0;
};
base::flat_map<not_null<PeerData*>, ReadRequest> _readRequests;
base::flat_map<not_null<PeerData*>, MsgId> _readRequestsPending;
std::unique_ptr<TaskQueue> _fileLoader;
base::flat_map<uint64, std::shared_ptr<SendingAlbum>> _sendingAlbums;

View File

@ -98,7 +98,10 @@ void Histories::readInboxTill(
return;
}
}
const auto tillId = item->id;
readInboxTill(history, item->id);
}
void Histories::readInboxTill(not_null<History*> history, MsgId tillId) {
if (!history->readInboxTillNeedsRequest(tillId)) {
return;
}

View File

@ -36,6 +36,7 @@ public:
void readInboxTill(
not_null<History*> history,
not_null<HistoryItem*> item);
void readInboxTill(not_null<History*> history, MsgId tillId);
void sendPendingReadInbox(not_null<History*> history);
private:

View File

@ -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<HistoryItem*> item) {

View File

@ -158,7 +158,7 @@ public:
void unregisterLocalMessage(not_null<HistoryItem*> item);
[[nodiscard]] HistoryItem *latestSendingMessage() const;
MsgId readInbox();
void readInbox();
void readInboxTill(not_null<HistoryItem*> item);
[[nodiscard]] bool readInboxTillNeedsRequest(MsgId tillId);
void applyInboxReadUpdate(

View File

@ -303,7 +303,7 @@ void Filler::addToggleUnreadMark() {
const auto markAsRead = isUnread(peer);
const auto handle = [&](not_null<History*> history) {
if (markAsRead) {
peer->session().api().readServerHistory(history);
history->readInbox();
} else {
peer->session().api().changeDialogUnreadMark(
history,