From 1753db3e62e4f9dcfd83851a0710d529a55e6d9a Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Tue, 19 Jul 2016 14:37:01 +0300
Subject: [PATCH] Megagroup members are always shown if the server allows us to
 view them. Bot keyboard clearing fixed when using Clear History.

---
 Telegram/SourceFiles/apiwrap.cpp                   |  1 -
 Telegram/SourceFiles/app.cpp                       |  2 --
 Telegram/SourceFiles/history.cpp                   |  6 +++---
 Telegram/SourceFiles/historywidget.cpp             |  3 ++-
 .../SourceFiles/profile/profile_members_widget.cpp |  6 ++++--
 Telegram/SourceFiles/window/top_bar_widget.cpp     | 14 +++++++++-----
 6 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp
index 0a13d6be2..08c4da448 100644
--- a/Telegram/SourceFiles/apiwrap.cpp
+++ b/Telegram/SourceFiles/apiwrap.cpp
@@ -543,7 +543,6 @@ void ApiWrap::lastParticipantsDone(ChannelData *peer, const MTPchannels_ChannelP
 	}
 	if (!keyboardBotFound) {
 		h->clearLastKeyboard();
-		if (App::main()) App::main()->updateBotKeyboard(h);
 	}
 	int newMembersCount = qMax(d.vcount.v, v.count());
 	if (newMembersCount > peer->membersCount()) {
diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp
index 7ee4b9b66..7b67ab797 100644
--- a/Telegram/SourceFiles/app.cpp
+++ b/Telegram/SourceFiles/app.cpp
@@ -858,7 +858,6 @@ namespace {
 					chat->botStatus = botStatus;
 					if (!found) {
 						h->clearLastKeyboard();
-						if (App::main()) App::main()->updateBotKeyboard(h);
 					}
 				}
 			}
@@ -961,7 +960,6 @@ namespace {
 						History *h = App::historyLoaded(chat->id);
 						if (h && h->lastKeyboardFrom == user->id) {
 							h->clearLastKeyboard();
-							if (App::main()) App::main()->updateBotKeyboard(h);
 						}
 					}
 					if (chat->botStatus > 0 && user->botInfo) {
diff --git a/Telegram/SourceFiles/history.cpp b/Telegram/SourceFiles/history.cpp
index 7f47a5220..d94e11200 100644
--- a/Telegram/SourceFiles/history.cpp
+++ b/Telegram/SourceFiles/history.cpp
@@ -151,6 +151,9 @@ void History::clearLastKeyboard() {
 			lastKeyboardHiddenId = 0;
 		}
 		lastKeyboardId = 0;
+		if (auto main = App::main()) {
+			main->updateBotKeyboard(this);
+		}
 	}
 	lastKeyboardInited = true;
 	lastKeyboardFrom = 0;
@@ -839,7 +842,6 @@ HistoryItem *History::createItem(const MTPMessage &msg, bool applyServiceAction,
 				PeerId uid = peerFromUser(d.vuser_id);
 				if (lastKeyboardFrom == uid) {
 					clearLastKeyboard();
-					if (App::main()) App::main()->updateBotKeyboard(this);
 				}
 				if (peer->isMegagroup()) {
 					if (auto user = App::userLoaded(uid)) {
@@ -2719,7 +2721,6 @@ void HistoryItem::finishEditionToEmpty() {
 	}
 	if (history()->lastKeyboardId == id) {
 		history()->clearLastKeyboard();
-		if (App::main()) App::main()->updateBotKeyboard(history());
 	}
 	if ((!out() || isPost()) && unread() && history()->unreadCount() > 0) {
 		history()->setUnreadCount(history()->unreadCount() - 1);
@@ -2767,7 +2768,6 @@ void HistoryItem::destroy() {
 	}
 	if (history()->lastKeyboardId == id) {
 		history()->clearLastKeyboard();
-		if (App::main()) App::main()->updateBotKeyboard(history());
 	}
 	if ((!out() || isPost()) && unread() && history()->unreadCount() > 0) {
 		history()->setUnreadCount(history()->unreadCount() - 1);
diff --git a/Telegram/SourceFiles/historywidget.cpp b/Telegram/SourceFiles/historywidget.cpp
index b695b0f0d..347fc6b69 100644
--- a/Telegram/SourceFiles/historywidget.cpp
+++ b/Telegram/SourceFiles/historywidget.cpp
@@ -5930,8 +5930,9 @@ void HistoryWidget::onKbToggle(bool manual) {
 		} else {
 			if (_history) {
 				_history->clearLastKeyboard();
+			} else {
+				updateBotKeyboard();
 			}
-			updateBotKeyboard();
 		}
 	} else if (!_keyboard.hasMarkup() && _keyboard.forceReply()) {
 		_kbHide.hide();
diff --git a/Telegram/SourceFiles/profile/profile_members_widget.cpp b/Telegram/SourceFiles/profile/profile_members_widget.cpp
index 34563e822..472c1b64d 100644
--- a/Telegram/SourceFiles/profile/profile_members_widget.cpp
+++ b/Telegram/SourceFiles/profile/profile_members_widget.cpp
@@ -447,7 +447,7 @@ void MembersWidget::fillMegagroupMembers(ChannelData *megagroup) {
 	t_assert(megagroup->mgInfo != nullptr);
 	if (megagroup->mgInfo->lastParticipants.isEmpty()) return;
 
-	if (!megagroup->amIn()) {
+	if (!megagroup->canViewMembers()) {
 		_list.clear();
 		return;
 	}
@@ -458,7 +458,9 @@ void MembersWidget::fillMegagroupMembers(ChannelData *megagroup) {
 	if (_sortByOnline) {
 		_list.clear();
 		_list.reserve(membersList.size());
-		addUser(megagroup, App::self())->onlineForSort = INT_MAX;
+		if (megagroup->amIn()) {
+			addUser(megagroup, App::self())->onlineForSort = INT_MAX;
+		}
 	} else if (membersList.size() >= _list.size()) {
 		if (addUsersToEnd(megagroup)) {
 			return;
diff --git a/Telegram/SourceFiles/window/top_bar_widget.cpp b/Telegram/SourceFiles/window/top_bar_widget.cpp
index d21847fe2..7218dfa8d 100644
--- a/Telegram/SourceFiles/window/top_bar_widget.cpp
+++ b/Telegram/SourceFiles/window/top_bar_widget.cpp
@@ -72,7 +72,11 @@ void TopBarWidget::onInfoClicked() {
 }
 
 void TopBarWidget::onSearch() {
-	Shortcuts::launch(qsl("search"));
+	if (auto main = App::main()) {
+		if (auto peer = main->peer()) {
+			main->searchInPeer(peer);
+		}
+	}
 }
 
 void TopBarWidget::enterEvent(QEvent *e) {
@@ -246,11 +250,11 @@ void TopBarWidget::updateMembersShowArea() {
 		if (_selCount || App::main()->overviewPeer() || !_selPeer) {
 			return false;
 		}
-		if (_selPeer->isChat()) {
-			return true;
+		if (auto chat = _selPeer->asChat()) {
+			return chat->amIn();
 		}
-		if (_selPeer->isMegagroup()) {
-			return (_selPeer->asMegagroup()->membersCount() < Global::ChatSizeMax());
+		if (auto megagroup = _selPeer->asMegagroup()) {
+			return megagroup->canViewMembers() && (megagroup->membersCount() < Global::ChatSizeMax());
 		}
 		return false;
 	};