From 2bbf17b672c4bbe87b85959b8fe4412ae4c0b1df Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Thu, 30 Nov 2017 21:45:36 +0400
Subject: [PATCH] Fix t.me/share links.

Regression was introduced in ffc20e4492.

Fixes #4099.
---
 .../SourceFiles/history/history_widget.cpp    |  7 ++---
 Telegram/SourceFiles/mainwidget.cpp           | 29 ++++++++++++-------
 Telegram/SourceFiles/mainwidget.h             |  5 +++-
 .../SourceFiles/window/window_peer_menu.cpp   |  1 +
 4 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp
index 15ed87071..c8e5f9260 100644
--- a/Telegram/SourceFiles/history/history_widget.cpp
+++ b/Telegram/SourceFiles/history/history_widget.cpp
@@ -331,7 +331,7 @@ void HistoryHider::forward() {
 		} else if (_sendPath) {
 			parent()->onSendPaths(_offered->id);
 		} else if (!_shareUrl.isEmpty()) {
-			parent()->onShareUrl(_offered->id, _shareUrl, _shareText);
+			parent()->shareUrl(_offered, _shareUrl, _shareText);
 		} else if (!_botAndQuery.isEmpty()) {
 			parent()->onInlineSwitchChosen(_offered->id, _botAndQuery);
 		} else {
@@ -402,9 +402,8 @@ bool HistoryHider::offerPeer(PeerId peer) {
 		}
 		return false;
 	} else if (!_shareUrl.isEmpty()) {
-		auto toId = _offered->id;
-		_offered = nullptr;
-		if (parent()->onShareUrl(toId, _shareUrl, _shareText)) {
+		auto offered = base::take(_offered);
+		if (parent()->shareUrl(offered, _shareUrl, _shareText)) {
 			startHide();
 		}
 		return false;
diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp
index 26ee595ea..1b33bef28 100644
--- a/Telegram/SourceFiles/mainwidget.cpp
+++ b/Telegram/SourceFiles/mainwidget.cpp
@@ -662,19 +662,28 @@ bool MainWidget::setForwardDraft(PeerId peerId, const SelectedItemSet &items) {
 	return true;
 }
 
-bool MainWidget::onShareUrl(const PeerId &peer, const QString &url, const QString &text) {
-	PeerData *p = App::peer(peer);
-	if (!peer || p->canWrite()) {
+bool MainWidget::shareUrl(
+		not_null<PeerData*> peer,
+		const QString &url,
+		const QString &text) {
+	if (!peer->canWrite()) {
 		Ui::show(Box<InformBox>(lang(lng_share_cant)));
 		return false;
 	}
-	History *h = App::history(peer);
-	TextWithTags textWithTags = { url + '\n' + text, TextWithTags::Tags() };
-	MessageCursor cursor = { url.size() + 1, url.size() + 1 + text.size(), QFIXED_MAX };
-	h->setLocalDraft(std::make_unique<Data::Draft>(textWithTags, 0, cursor, false));
-	h->clearEditDraft();
-	bool opened = _history->peer() && (_history->peer()->id == peer);
-	if (opened) {
+	TextWithTags textWithTags = {
+		url + '\n' + text,
+		TextWithTags::Tags()
+	};
+	MessageCursor cursor = {
+		url.size() + 1,
+		url.size() + 1 + text.size(),
+		QFIXED_MAX
+	};
+	auto history = App::history(peer->id);
+	history->setLocalDraft(
+		std::make_unique<Data::Draft>(textWithTags, 0, cursor, false));
+	history->clearEditDraft();
+	if (_history->peer() == peer) {
 		_history->applyDraft();
 	} else {
 		Ui::showPeerHistory(peer, ShowAtUnreadMsgId);
diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h
index d9f642553..090a0a306 100644
--- a/Telegram/SourceFiles/mainwidget.h
+++ b/Telegram/SourceFiles/mainwidget.h
@@ -190,7 +190,10 @@ public:
 	void noHider(HistoryHider *destroyed);
 	bool setForwardDraft(PeerId peer, ForwardWhatMessages what);
 	bool setForwardDraft(PeerId peer, const SelectedItemSet &items);
-	bool onShareUrl(const PeerId &peer, const QString &url, const QString &text);
+	bool shareUrl(
+		not_null<PeerData*> peer,
+		const QString &url,
+		const QString &text);
 	bool onInlineSwitchChosen(const PeerId &peer, const QString &botAndQuery);
 	void onShareContact(const PeerId &peer, UserData *contact);
 	bool onSendPaths(const PeerId &peer);
diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp
index 3b1269994..8e84f2fb3 100644
--- a/Telegram/SourceFiles/window/window_peer_menu.cpp
+++ b/Telegram/SourceFiles/window/window_peer_menu.cpp
@@ -31,6 +31,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
 #include "auth_session.h"
 #include "apiwrap.h"
 #include "mainwidget.h"
+#include "mainwindow.h"
 #include "observer_peer.h"
 #include "styles/style_boxes.h"
 #include "window/window_controller.h"