From 83919492d36c226a6e03a35ed4c0b1c4f1088751 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Mon, 8 Oct 2018 16:41:14 +0300
Subject: [PATCH] Ignore unread counts from deleted (support).

---
 Telegram/SourceFiles/history/history.cpp | 28 +++++++++++++++++++++++-
 Telegram/SourceFiles/history/history.h   |  2 ++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp
index c61fbbd46..0370399c3 100644
--- a/Telegram/SourceFiles/history/history.cpp
+++ b/Telegram/SourceFiles/history/history.cpp
@@ -1714,6 +1714,9 @@ void History::setUnreadCount(int newUnreadCount) {
 }
 
 void History::setUnreadMark(bool unread) {
+	if (clearUnreadOnClientSide()) {
+		unread = false;
+	}
 	if (_unreadMark != unread) {
 		_unreadMark = unread;
 		if (!_unreadCount || !*_unreadCount) {
@@ -2285,6 +2288,18 @@ void History::applyDialog(const MTPDdialog &data) {
 	}
 }
 
+bool History::clearUnreadOnClientSide() const {
+	if (!Auth().supportMode()) {
+		return false;
+	}
+	if (const auto user = peer->asUser()) {
+		if (user->flags() & MTPDuser::Flag::f_deleted) {
+			return true;
+		}
+	}
+	return false;
+}
+
 bool History::skipUnreadUpdateForClientSideUnread() const {
 	if (peer->id != peerFromUser(ServiceUserId)) {
 		return false;
@@ -2296,11 +2311,16 @@ bool History::skipUnreadUpdateForClientSideUnread() const {
 	return true;
 }
 
+bool History::skipUnreadUpdate() const {
+	return skipUnreadUpdateForClientSideUnread()
+		|| clearUnreadOnClientSide();
+}
+
 void History::applyDialogFields(
 		int unreadCount,
 		MsgId maxInboxRead,
 		MsgId maxOutboxRead) {
-	if (!skipUnreadUpdateForClientSideUnread()) {
+	if (!skipUnreadUpdate()) {
 		setUnreadCount(unreadCount);
 		setInboxReadTill(maxInboxRead);
 	}
@@ -2320,6 +2340,12 @@ void History::applyDialogTopMessage(MsgId topMessageId) {
 	} else {
 		setLastMessage(nullptr);
 	}
+	if (clearUnreadOnClientSide()) {
+		setUnreadCount(0);
+		if (const auto last = lastMessage()) {
+			setInboxReadTill(last->id);
+		}
+	}
 }
 
 void History::setInboxReadTill(MsgId upTo) {
diff --git a/Telegram/SourceFiles/history/history.h b/Telegram/SourceFiles/history/history.h
index 4b3c5ae73..e80255eb8 100644
--- a/Telegram/SourceFiles/history/history.h
+++ b/Telegram/SourceFiles/history/history.h
@@ -471,6 +471,8 @@ private:
 
 	void addItemsToLists(const std::vector<not_null<HistoryItem*>> &items);
 	void clearSendAction(not_null<UserData*> from);
+	bool clearUnreadOnClientSide() const;
+	bool skipUnreadUpdate() const;
 	bool skipUnreadUpdateForClientSideUnread() const;
 
 	HistoryItem *lastAvailableMessage() const;