From 2ade6be1468a68e270e1fdde3c0b878eaa3ec2ac Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Thu, 14 Oct 2021 15:42:51 +0400
Subject: [PATCH] Show correct phrase in local join messages.

---
 Telegram/SourceFiles/api/api_updates.cpp      |  1 +
 Telegram/SourceFiles/apiwrap.cpp              | 19 ++++++++++++------
 Telegram/SourceFiles/data/data_channel.h      |  1 +
 Telegram/SourceFiles/history/history.cpp      | 20 +++++++++++--------
 .../SourceFiles/history/history_service.cpp   | 15 ++++++++++----
 .../SourceFiles/history/history_service.h     |  3 ++-
 6 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/Telegram/SourceFiles/api/api_updates.cpp b/Telegram/SourceFiles/api/api_updates.cpp
index eebb2895f..ab0749cb5 100644
--- a/Telegram/SourceFiles/api/api_updates.cpp
+++ b/Telegram/SourceFiles/api/api_updates.cpp
@@ -2113,6 +2113,7 @@ void Updates::feedUpdate(const MTPUpdate &update) {
 		auto &d = update.c_updateChannel();
 		if (const auto channel = session().data().channelLoaded(d.vchannel_id())) {
 			channel->inviter = UserId(0);
+			channel->inviteViaRequest = false;
 			if (channel->amIn()) {
 				if (channel->isMegagroup()
 					&& !channel->amCreator()
diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp
index 2575fc8dc..370c82019 100644
--- a/Telegram/SourceFiles/apiwrap.cpp
+++ b/Telegram/SourceFiles/apiwrap.cpp
@@ -1618,9 +1618,13 @@ void ApiWrap::requestSelfParticipant(not_null<ChannelData*> channel) {
 		return;
 	}
 
-	const auto finalize = [=](UserId inviter, TimeId inviteDate) {
+	const auto finalize = [=](
+			UserId inviter = -1,
+			TimeId inviteDate = 0,
+			bool inviteViaRequest = false) {
 		channel->inviter = inviter;
 		channel->inviteDate = inviteDate;
+		channel->inviteViaRequest = inviteViaRequest;
 		if (const auto history = _session->data().historyLoaded(channel)) {
 			if (history->lastMessageKnown()) {
 				history->checkLocalMessages();
@@ -1641,7 +1645,10 @@ void ApiWrap::requestSelfParticipant(not_null<ChannelData*> channel) {
 
 			const auto &participant = data.vparticipant();
 			participant.match([&](const MTPDchannelParticipantSelf &data) {
-				finalize(data.vinviter_id().v, data.vdate().v);
+				finalize(
+					data.vinviter_id().v,
+					data.vdate().v,
+					data.is_via_invite());
 			}, [&](const MTPDchannelParticipantCreator &) {
 				if (channel->mgInfo) {
 					channel->mgInfo->creator = _session->user();
@@ -1654,13 +1661,13 @@ void ApiWrap::requestSelfParticipant(not_null<ChannelData*> channel) {
 				finalize(inviter, data.vdate().v);
 			}, [&](const MTPDchannelParticipantBanned &data) {
 				LOG(("API Error: Got self banned participant."));
-				finalize(-1, 0);
+				finalize();
 			}, [&](const MTPDchannelParticipant &data) {
 				LOG(("API Error: Got self regular participant."));
-				finalize(-1, 0);
+				finalize();
 			}, [&](const MTPDchannelParticipantLeft &data) {
 				LOG(("API Error: Got self left participant."));
-				finalize(-1, 0);
+				finalize();
 			});
 		});
 	}).fail([=](const MTP::Error &error) {
@@ -1668,7 +1675,7 @@ void ApiWrap::requestSelfParticipant(not_null<ChannelData*> channel) {
 		if (error.type() == qstr("CHANNEL_PRIVATE")) {
 			channel->privateErrorReceived();
 		}
-		finalize(-1, 0);
+		finalize();
 	}).afterDelay(kSmallDelayMs).send();
 }
 
diff --git a/Telegram/SourceFiles/data/data_channel.h b/Telegram/SourceFiles/data/data_channel.h
index 426db0454..d4980f7f0 100644
--- a/Telegram/SourceFiles/data/data_channel.h
+++ b/Telegram/SourceFiles/data/data_channel.h
@@ -412,6 +412,7 @@ public:
 	// > 0 - user who invited me to channel, < 0 - not in channel.
 	UserId inviter = 0;
 	TimeId inviteDate = 0;
+	bool inviteViaRequest = false;
 
 private:
 	struct InvitePeek {
diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp
index 0871af5fa..039fb5e53 100644
--- a/Telegram/SourceFiles/history/history.cpp
+++ b/Telegram/SourceFiles/history/history.cpp
@@ -2879,16 +2879,17 @@ MsgRange History::rangeForDifferenceRequest() const {
 }
 
 HistoryService *History::insertJoinedMessage() {
-	if (!isChannel()
+	const auto channel = peer->asChannel();
+	if (!channel
 		|| _joinedMessage
-		|| !peer->asChannel()->amIn()
+		|| !channel->amIn()
 		|| (peer->isMegagroup()
-			&& peer->asChannel()->mgInfo->joinedMessageFound)) {
+			&& channel->mgInfo->joinedMessageFound)) {
 		return _joinedMessage;
 	}
 
-	const auto inviter = peer->asChannel()->inviter
-		? owner().userLoaded(peer->asChannel()->inviter)
+	const auto inviter = (channel->inviter.bare > 0)
+		? owner().userLoaded(channel->inviter)
 		: nullptr;
 	if (!inviter) {
 		return nullptr;
@@ -2898,12 +2899,15 @@ HistoryService *History::insertJoinedMessage() {
 		&& peer->migrateFrom()
 		&& !blocks.empty()
 		&& blocks.front()->messages.front()->data()->id == 1) {
-		peer->asChannel()->mgInfo->joinedMessageFound = true;
+		channel->mgInfo->joinedMessageFound = true;
 		return nullptr;
 	}
 
-	const auto inviteDate = peer->asChannel()->inviteDate;
-	_joinedMessage = GenerateJoinedMessage(this, inviteDate, inviter);
+	_joinedMessage = GenerateJoinedMessage(
+		this,
+		channel->inviteDate,
+		inviter,
+		channel->inviteViaRequest);
 	insertLocalMessage(_joinedMessage);
 	return _joinedMessage;
 }
diff --git a/Telegram/SourceFiles/history/history_service.cpp b/Telegram/SourceFiles/history/history_service.cpp
index 6553db987..bb484a865 100644
--- a/Telegram/SourceFiles/history/history_service.cpp
+++ b/Telegram/SourceFiles/history/history_service.cpp
@@ -1243,7 +1243,8 @@ HistoryService::~HistoryService() {
 
 HistoryService::PreparedText GenerateJoinedText(
 		not_null<History*> history,
-		not_null<UserData*> inviter) {
+		not_null<UserData*> inviter,
+		bool viaRequest) {
 	if (inviter->id != history->session().userPeerId()) {
 		auto result = HistoryService::PreparedText{};
 		result.links.push_back(inviter->createOpenLink());
@@ -1255,6 +1256,9 @@ HistoryService::PreparedText GenerateJoinedText(
 				textcmdLink(1, inviter->name));
 		return result;
 	} else if (history->isMegagroup()) {
+		if (viaRequest) {
+			return { tr::lng_action_you_joined_by_request(tr::now) };
+		}
 		auto self = history->session().user();
 		auto result = HistoryService::PreparedText{};
 		result.links.push_back(self->createOpenLink());
@@ -1264,18 +1268,21 @@ HistoryService::PreparedText GenerateJoinedText(
 			textcmdLink(1, self->name));
 		return result;
 	}
-	return { tr::lng_action_you_joined(tr::now) };
+	return { viaRequest
+		? tr::lng_action_you_joined_by_request_channel(tr::now)
+		: tr::lng_action_you_joined(tr::now) };
 }
 
 not_null<HistoryService*> GenerateJoinedMessage(
 		not_null<History*> history,
 		TimeId inviteDate,
-		not_null<UserData*> inviter) {
+		not_null<UserData*> inviter,
+		bool viaRequest) {
 	return history->makeServiceMessage(
 		history->owner().nextLocalMessageId(),
 		MessageFlag::LocalHistoryEntry,
 		inviteDate,
-		GenerateJoinedText(history, inviter));
+		GenerateJoinedText(history, inviter, viaRequest));
 }
 
 std::optional<bool> PeerHasThisCall(
diff --git a/Telegram/SourceFiles/history/history_service.h b/Telegram/SourceFiles/history/history_service.h
index 68ea9888a..0109f1f5b 100644
--- a/Telegram/SourceFiles/history/history_service.h
+++ b/Telegram/SourceFiles/history/history_service.h
@@ -175,7 +175,8 @@ private:
 [[nodiscard]] not_null<HistoryService*> GenerateJoinedMessage(
 	not_null<History*> history,
 	TimeId inviteDate,
-	not_null<UserData*> inviter);
+	not_null<UserData*> inviter,
+	bool viaRequest);
 [[nodiscard]] std::optional<bool> PeerHasThisCall(
 	not_null<PeerData*> peer,
 	CallId id);