From 0744f43a0ebd3245458decb1e1d8e67bc2a47c57 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Thu, 4 Apr 2019 14:02:22 +0400
Subject: [PATCH] Create private channel post links.

---
 Telegram/Resources/langs/lang.strings         |  1 +
 .../history/history_inner_widget.cpp          | 10 ++++----
 Telegram/SourceFiles/history/history_item.cpp | 18 +++++++-------
 .../SourceFiles/history/history_message.cpp   |  5 ++--
 .../SourceFiles/history/history_widget.cpp    |  8 -------
 Telegram/SourceFiles/history/history_widget.h |  1 -
 .../view/history_view_context_menu.cpp        | 24 ++++++++++++-------
 .../history/view/history_view_context_menu.h  |  1 +
 8 files changed, 33 insertions(+), 35 deletions(-)

diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings
index 04b995c43..4666a3b16 100644
--- a/Telegram/Resources/langs/lang.strings
+++ b/Telegram/Resources/langs/lang.strings
@@ -1032,6 +1032,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 "lng_group_invite_no_room" = "Unable to join this group because there are too many members in it already.";
 
 "lng_channel_public_link_copied" = "Link copied to clipboard.";
+"lng_context_about_private_link" = "This link will only work for members of this chat.";
 
 "lng_forwarded" = "Forwarded from {user}";
 "lng_forwarded_date" = "Original: {date}";
diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp
index 46e99728a..14741d1ee 100644
--- a/Telegram/SourceFiles/history/history_inner_widget.cpp
+++ b/Telegram/SourceFiles/history/history_inner_widget.cpp
@@ -1167,7 +1167,7 @@ std::unique_ptr<QMimeData> HistoryInner::prepareDrag() {
 		//}
 	}
 	if (auto mimeData = MimeDataFromTextWithEntities(sel)) {
-		updateDragSelection(0, 0, false);
+		updateDragSelection(nullptr, nullptr, false);
 		_widget->noSelectingScroll();
 
 		if (!urls.isEmpty()) mimeData->setUrls(urls);
@@ -1580,7 +1580,7 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) {
 		}
 		if (item && item->hasDirectLink() && isUponSelected != 2 && isUponSelected != -2) {
 			_menu->addAction(lang(item->history()->peer->isMegagroup() ? lng_context_copy_link : lng_context_copy_post_link), [=] {
-				_widget->copyPostLink(itemId);
+				HistoryView::CopyPostLink(itemId);
 			});
 		}
 		if (isUponSelected > 1) {
@@ -1716,7 +1716,7 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) {
 				});
 		} else if (item && item->hasDirectLink() && isUponSelected != 2 && isUponSelected != -2) {
 			_menu->addAction(lang(item->history()->peer->isMegagroup() ? lng_context_copy_link : lng_context_copy_post_link), [=] {
-				_widget->copyPostLink(itemId);
+				HistoryView::CopyPostLink(itemId);
 			});
 		}
 		if (isUponSelected > 1) {
@@ -2256,7 +2256,7 @@ bool HistoryInner::focusNextPrevChild(bool next) {
 
 void HistoryInner::adjustCurrent(int32 y) const {
 	int32 htop = historyTop(), hdrawtop = historyDrawTop(), mtop = migratedTop();
-	_curHistory = 0;
+	_curHistory = nullptr;
 	if (mtop >= 0) {
 		adjustCurrent(y - mtop, _migrated);
 	}
@@ -2682,7 +2682,7 @@ void HistoryInner::mouseActionUpdate() {
 	if (_mouseAction == MouseAction::Selecting) {
 		_widget->checkSelectingScroll(mousePos);
 	} else {
-		updateDragSelection(0, 0, false);
+		updateDragSelection(nullptr, nullptr, false);
 		_widget->noSelectingScroll();
 	}
 
diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp
index a3f2f2eac..dda1d51c4 100644
--- a/Telegram/SourceFiles/history/history_item.cpp
+++ b/Telegram/SourceFiles/history/history_item.cpp
@@ -549,21 +549,19 @@ bool HistoryItem::suggestDeleteAllReport() const {
 }
 
 bool HistoryItem::hasDirectLink() const {
-	if (!IsServerMsgId(id)) {
-		return false;
-	}
-	if (auto channel = _history->peer->asChannel()) {
-		return channel->isPublic();
-	}
-	return false;
+	return IsServerMsgId(id) && _history->peer->isChannel();
 }
 
 QString HistoryItem::directLink() const {
 	if (hasDirectLink()) {
-		auto channel = _history->peer->asChannel();
+		const auto channel = _history->peer->asChannel();
 		Assert(channel != nullptr);
-		auto query = channel->username + '/' + QString::number(id);
-		if (!channel->isMegagroup()) {
+
+		const auto base = channel->isPublic()
+			? channel->username
+			: "c/" + QString::number(channel->bareId());
+		const auto query = base + '/' + QString::number(id);
+		if (channel->isPublic() && !channel->isMegagroup()) {
 			if (const auto media = this->media()) {
 				if (const auto document = media->document()) {
 					if (document->isVideoMessage()) {
diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp
index fb8c1cbee..0e2a1bf3e 100644
--- a/Telegram/SourceFiles/history/history_message.cpp
+++ b/Telegram/SourceFiles/history/history_message.cpp
@@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "history/history_location_manager.h"
 #include "history/history_service.h"
 #include "history/view/history_view_service_message.h"
+#include "history/view/history_view_context_menu.h" // For CopyPostLink().
 #include "auth_session.h"
 #include "boxes/share_box.h"
 #include "boxes/confirm_box.h"
@@ -104,9 +105,7 @@ void FastShareMessage(not_null<HistoryItem*> item) {
 	auto copyCallback = [data]() {
 		if (auto item = App::histItemById(data->msgIds[0])) {
 			if (item->hasDirectLink()) {
-				QApplication::clipboard()->setText(item->directLink());
-
-				Ui::Toast::Show(lang(lng_channel_public_link_copied));
+				HistoryView::CopyPostLink(item->fullId());
 			} else if (const auto bot = item->getMessageBot()) {
 				if (const auto media = item->media()) {
 					if (const auto game = media->game()) {
diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp
index ab23314bf..376f27bd0 100644
--- a/Telegram/SourceFiles/history/history_widget.cpp
+++ b/Telegram/SourceFiles/history/history_widget.cpp
@@ -5781,14 +5781,6 @@ void HistoryWidget::onPinnedHide() {
 	}
 }
 
-void HistoryWidget::copyPostLink(FullMsgId itemId) {
-	if (const auto item = App::histItemById(itemId)) {
-		if (item->hasDirectLink()) {
-			QApplication::clipboard()->setText(item->directLink());
-		}
-	}
-}
-
 bool HistoryWidget::lastForceReplyReplied(const FullMsgId &replyTo) const {
 	if (replyTo.channel != _channel) {
 		return false;
diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h
index 8fd8cb7cc..b689b425d 100644
--- a/Telegram/SourceFiles/history/history_widget.h
+++ b/Telegram/SourceFiles/history/history_widget.h
@@ -201,7 +201,6 @@ public:
 	void editMessage(not_null<HistoryItem*> item);
 	void pinMessage(FullMsgId itemId);
 	void unpinMessage(FullMsgId itemId);
-	void copyPostLink(FullMsgId itemId);
 
 	MsgId replyToId() const;
 	void messageDataReceived(ChannelData *channel, MsgId msgId);
diff --git a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp
index a3165c395..bbb597d85 100644
--- a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp
+++ b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp
@@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "history/media/history_media_web_page.h"
 #include "ui/widgets/popup_menu.h"
 #include "ui/image/image.h"
+#include "ui/toast/toast.h"
 #include "chat_helpers/message_field.h"
 #include "boxes/confirm_box.h"
 #include "boxes/sticker_set_box.h"
@@ -185,14 +186,6 @@ void AddDocumentActions(
 	AddSaveDocumentAction(menu, contextId, document);
 }
 
-void CopyPostLink(FullMsgId itemId) {
-	if (const auto item = App::histItemById(itemId)) {
-		if (item->hasDirectLink()) {
-			QApplication::clipboard()->setText(item->directLink());
-		}
-	}
-}
-
 void AddPostLinkAction(
 		not_null<Ui::PopupMenu*> menu,
 		const ContextMenuRequest &request) {
@@ -512,6 +505,21 @@ base::unique_qptr<Ui::PopupMenu> FillContextMenu(
 	return result;
 }
 
+void CopyPostLink(FullMsgId itemId) {
+	if (const auto item = App::histItemById(itemId)) {
+		if (item->hasDirectLink()) {
+			QApplication::clipboard()->setText(item->directLink());
+
+			const auto channel = item->history()->peer->asChannel();
+			Assert(channel != nullptr);
+
+			Ui::Toast::Show(lang(channel->isPublic()
+				? lng_channel_public_link_copied
+				: lng_context_about_private_link));
+		}
+	}
+}
+
 void StopPoll(FullMsgId itemId) {
 	Ui::show(Box<ConfirmBox>(
 		lang(lng_polls_stop_warning),
diff --git a/Telegram/SourceFiles/history/view/history_view_context_menu.h b/Telegram/SourceFiles/history/view/history_view_context_menu.h
index 1d5fce842..47ae8fbd9 100644
--- a/Telegram/SourceFiles/history/view/history_view_context_menu.h
+++ b/Telegram/SourceFiles/history/view/history_view_context_menu.h
@@ -35,6 +35,7 @@ base::unique_qptr<Ui::PopupMenu> FillContextMenu(
 	not_null<ListWidget*> list,
 	const ContextMenuRequest &request);
 
+void CopyPostLink(FullMsgId itemId);
 void StopPoll(FullMsgId itemId);
 
 } // namespace