From 866c5e9b7b3e6452580ff2120b9c31c052e33341 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Wed, 6 Jun 2018 22:56:35 +0300
Subject: [PATCH] Request common notify settings when needed.

If user / group has default notify settings we need common user
or common group notify settings to display the badge properly.
---
 Telegram/SourceFiles/apiwrap.h                    | 8 ++++----
 Telegram/SourceFiles/base/flat_map.h              | 6 ++++++
 Telegram/SourceFiles/base/flat_set.h              | 6 ++++++
 Telegram/SourceFiles/data/data_session.cpp        | 7 ++-----
 Telegram/SourceFiles/history/history.cpp          | 4 ++++
 Telegram/SourceFiles/window/window_controller.cpp | 4 ++--
 6 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h
index 8bcf55380..dbebeba7c 100644
--- a/Telegram/SourceFiles/apiwrap.h
+++ b/Telegram/SourceFiles/apiwrap.h
@@ -487,10 +487,10 @@ private:
 	QMap<uint64, QPair<uint64, mtpRequestId> > _stickerSetRequests;
 
 	QMap<ChannelData*, mtpRequestId> _channelAmInRequests;
-	std::map<not_null<UserData*>, mtpRequestId> _blockRequests;
-	std::map<not_null<PeerData*>, mtpRequestId> _exportInviteRequests;
-	std::map<PeerId, mtpRequestId> _notifySettingRequests;
-	std::map<not_null<History*>, mtpRequestId> _draftsSaveRequestIds;
+	base::flat_map<not_null<UserData*>, mtpRequestId> _blockRequests;
+	base::flat_map<not_null<PeerData*>, mtpRequestId> _exportInviteRequests;
+	base::flat_map<PeerId, mtpRequestId> _notifySettingRequests;
+	base::flat_map<not_null<History*>, mtpRequestId> _draftsSaveRequestIds;
 	base::Timer _draftsSaveTimer;
 
 	base::flat_set<mtpRequestId> _stickerSetDisenableRequests;
diff --git a/Telegram/SourceFiles/base/flat_map.h b/Telegram/SourceFiles/base/flat_map.h
index 8bed4a422..75770e4f9 100644
--- a/Telegram/SourceFiles/base/flat_map.h
+++ b/Telegram/SourceFiles/base/flat_map.h
@@ -13,6 +13,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 
 namespace base {
 
+using std::begin;
+using std::end;
+
 template <
 	typename Key,
 	typename Type,
@@ -424,6 +427,9 @@ public:
 	iterator erase(const_iterator from, const_iterator till) {
 		return impl().erase(from._impl, till._impl);
 	}
+	int erase(const Key &key) {
+		return removeAll(key);
+	}
 
 	iterator findFirst(const Key &key) {
 		if (empty()
diff --git a/Telegram/SourceFiles/base/flat_set.h b/Telegram/SourceFiles/base/flat_set.h
index 1dbbbd3b0..368bd104d 100644
--- a/Telegram/SourceFiles/base/flat_set.h
+++ b/Telegram/SourceFiles/base/flat_set.h
@@ -12,6 +12,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 
 namespace base {
 
+using std::begin;
+using std::end;
+
 template <typename Type, typename Compare = std::less<>>
 class flat_set;
 
@@ -304,6 +307,9 @@ public:
 	iterator erase(const_iterator from, const_iterator till) {
 		return impl().erase(from._impl, till._impl);
 	}
+	int erase(const Type &value) {
+		return removeAll(value);
+	}
 
 	iterator findFirst(const Type &value) {
 		if (empty()
diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp
index d3705c9e4..aecd05752 100644
--- a/Telegram/SourceFiles/data/data_session.cpp
+++ b/Telegram/SourceFiles/data/data_session.cpp
@@ -1692,12 +1692,9 @@ void Session::applyNotifySetting(
 			App::enumerateChatsChannels([&](not_null<PeerData*> peer) {
 				if (!peer->notifySettingsUnknown()
 					&& ((!peer->notifyMuteUntil()
-						&& _defaultUserNotifySettings.muteUntil())
+						&& _defaultChatNotifySettings.muteUntil())
 						|| (!peer->notifySilentPosts()
-							&& _defaultUserNotifySettings.silentPosts()))) {
-					if (!peer->notifyMuteUntil()) {
-						int a = 0;
-					}
+							&& _defaultChatNotifySettings.silentPosts()))) {
 					updateNotifySettingsLocal(peer);
 				}
 			});
diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp
index 8a52b63c6..220c0e7d5 100644
--- a/Telegram/SourceFiles/history/history.cpp
+++ b/Telegram/SourceFiles/history/history.cpp
@@ -2197,6 +2197,10 @@ void History::applyDialog(const MTPDdialog &data) {
 	Auth().data().applyNotifySetting(
 		MTP_notifyPeer(data.vpeer),
 		data.vnotify_settings);
+
+	// Request default notification settings for this type of chat.
+	Auth().data().requestNotifySettings(peer);
+
 	if (data.has_draft() && data.vdraft.type() == mtpc_draftMessage) {
 		Data::applyPeerCloudDraft(peer->id, data.vdraft.c_draftMessage());
 	}
diff --git a/Telegram/SourceFiles/window/window_controller.cpp b/Telegram/SourceFiles/window/window_controller.cpp
index 4e9e48752..44ef88b4c 100644
--- a/Telegram/SourceFiles/window/window_controller.cpp
+++ b/Telegram/SourceFiles/window/window_controller.cpp
@@ -476,9 +476,9 @@ void Navigation::showPeerInfo(
 void Controller::showSection(
 		SectionMemento &&memento,
 		const SectionShow &params) {
-	if (App::wnd()->showSectionInExistingLayer(
+	if (!params.thirdColumn && App::wnd()->showSectionInExistingLayer(
 			&memento,
-			params) && !params.thirdColumn) {
+			params)) {
 		return;
 	}
 	App::main()->showSection(std::move(memento), params);