From 7b496b374103d86bcca1ae0d4f25461638ffe43e Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Fri, 30 Jun 2017 13:32:10 +0300
Subject: [PATCH] Improve in-app changelogs for alpha version.

---
 Telegram/SourceFiles/apiwrap.cpp | 51 ++++++++++++++++++++++----------
 Telegram/SourceFiles/apiwrap.h   |  2 ++
 2 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp
index bc9e66925..b92ce46f8 100644
--- a/Telegram/SourceFiles/apiwrap.cpp
+++ b/Telegram/SourceFiles/apiwrap.cpp
@@ -53,24 +53,29 @@ ApiWrap::ApiWrap(gsl::not_null<AuthSession*> session)
 
 void ApiWrap::start() {
 	Window::Theme::Background()->start();
-	auto oldVersion = Local::oldMapVersion();
-	if (oldVersion > 0 && oldVersion < AppVersion) {
-		_changelogSubscription = subscribe(_session->data().moreChatsLoaded(), [this, oldVersion] {
-			auto oldVersionString = qsl("%1.%2.%3").arg(oldVersion / 1000000).arg((oldVersion % 1000000) / 1000).arg(oldVersion % 1000);
-			request(MTPhelp_GetAppChangelog(MTP_string(oldVersionString))).done([this, oldVersion](const MTPUpdates &result) {
+	requestAppChangelogs();
+}
+
+void ApiWrap::requestAppChangelogs() {
+	auto oldAppVersion = Local::oldMapVersion();
+	if (oldAppVersion > 0 && oldAppVersion < AppVersion) {
+		_changelogSubscription = subscribe(_session->data().moreChatsLoaded(), [this, oldAppVersion] {
+			auto oldVersionString = qsl("%1.%2.%3").arg(oldAppVersion / 1000000).arg((oldAppVersion % 1000000) / 1000).arg(oldAppVersion % 1000);
+			request(MTPhelp_GetAppChangelog(MTP_string(oldVersionString))).done([this, oldAppVersion](const MTPUpdates &result) {
 				applyUpdates(result);
 
-				auto addLocalChangelog = [this, oldVersion](int changeVersion, const char *changes) {
-					if (oldVersion < changeVersion) {
-						auto changeVersionString = QString::number(changeVersion / 1000000) + '.' + QString::number((changeVersion % 1000000) / 1000) + ((changeVersion % 1000) ? ('.' + QString::number(changeVersion % 1000)) : QString());
-						auto text = qsl("New in version %1:\n\n").arg(changeVersionString) + QString::fromUtf8(changes).trimmed();
-						auto textWithEntities = TextWithEntities { text };
-						textParseEntities(textWithEntities.text, TextParseLinks, &textWithEntities.entities);
-						App::main()->serviceNotification(textWithEntities, MTP_messageMediaEmpty(), unixtime());
-					}
-				};
-				if (cAlphaVersion() || cBetaVersion()) {
-					addLocalChangelog(1001008, "\xE2\x80\x94 Toggle night mode in the main menu.\n");
+				auto resultEmpty = true;
+				switch (result.type()) {
+				case mtpc_updateShortMessage:
+				case mtpc_updateShortChatMessage:
+				case mtpc_updateShort: resultEmpty = false; break;
+				case mtpc_updatesCombined: resultEmpty = result.c_updatesCombined().vupdates.v.isEmpty(); break;
+				case mtpc_updates: resultEmpty = result.c_updates().vupdates.v.isEmpty(); break;
+				case mtpc_updatesTooLong:
+				case mtpc_updateShortSentMessage: LOG(("API Error: Bad updates type in app changelog.")); break;
+				}
+				if (resultEmpty && (cAlphaVersion() || cBetaVersion())) {
+					addLocalAlphaChangelogs(oldAppVersion);
 				}
 			}).send();
 			unsubscribe(base::take(_changelogSubscription));
@@ -78,6 +83,20 @@ void ApiWrap::start() {
 	}
 }
 
+void ApiWrap::addLocalAlphaChangelogs(int oldAppVersion) {
+	auto addLocalChangelog = [this, oldAppVersion](int changeVersion, const char *changes) {
+		if (oldAppVersion < changeVersion) {
+			auto changeVersionString = QString::number(changeVersion / 1000000) + '.' + QString::number((changeVersion % 1000000) / 1000) + ((changeVersion % 1000) ? ('.' + QString::number(changeVersion % 1000)) : QString());
+			auto text = qsl("New in version %1:\n\n").arg(changeVersionString) + QString::fromUtf8(changes).trimmed();
+			auto textWithEntities = TextWithEntities { text };
+			textParseEntities(textWithEntities.text, TextParseLinks, &textWithEntities.entities);
+			App::main()->serviceNotification(textWithEntities, MTP_messageMediaEmpty(), unixtime());
+		}
+	};
+
+	addLocalChangelog(1001008, "\xE2\x80\x94 Toggle night mode in the main menu.\n");
+}
+
 void ApiWrap::applyUpdates(const MTPUpdates &updates, uint64 sentMessageRandomId) {
 	App::main()->feedUpdates(updates, sentMessageRandomId);
 }
diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h
index 513f22270..950cada53 100644
--- a/Telegram/SourceFiles/apiwrap.h
+++ b/Telegram/SourceFiles/apiwrap.h
@@ -101,6 +101,8 @@ private:
 	};
 	using MessageDataRequests = QMap<MsgId, MessageDataRequest>;
 
+	void requestAppChangelogs();
+	void addLocalAlphaChangelogs(int oldAppVersion);
 	void updatesReceived(const MTPUpdates &updates);
 	void checkQuitPreventFinished();