From 3165a7268b41799b0adce39d626cdf8f2f1ae01c Mon Sep 17 00:00:00 2001
From: "V.G. Bulavintsev" <golem.md@gmail.com>
Date: Fri, 4 Jun 2021 13:26:42 +0200
Subject: [PATCH] Fix adding torrent to personal channel after creating torrent

---
 .../dialogs/createtorrentdialog.py            | 21 ++-------
 src/tribler-gui/tribler_gui/tribler_window.py | 47 +++++++++++++------
 2 files changed, 35 insertions(+), 33 deletions(-)

diff --git a/src/tribler-gui/tribler_gui/dialogs/createtorrentdialog.py b/src/tribler-gui/tribler_gui/dialogs/createtorrentdialog.py
index 2ecc3138314..335e1d78f27 100644
--- a/src/tribler-gui/tribler_gui/dialogs/createtorrentdialog.py
+++ b/src/tribler-gui/tribler_gui/dialogs/createtorrentdialog.py
@@ -4,8 +4,6 @@
 from PyQt5.QtCore import QDir, pyqtSignal
 from PyQt5.QtWidgets import QAction, QFileDialog, QSizePolicy, QTreeWidgetItem
 
-from tribler_core.utilities.unicode import ensure_unicode
-
 from tribler_gui.defs import BUTTON_TYPE_NORMAL
 from tribler_gui.dialogs.confirmationdialog import ConfirmationDialog
 from tribler_gui.dialogs.dialogcontainer import DialogContainer
@@ -22,6 +20,7 @@ def __init__(self, parent):
 class CreateTorrentDialog(DialogContainer):
 
     create_torrent_notification = pyqtSignal(dict)
+    add_to_channel_selected = pyqtSignal(str)
 
     def __init__(self, parent):
         DialogContainer.__init__(self, parent)
@@ -127,23 +126,9 @@ def on_torrent_created(self, result):
         self.dialog_widget.edit_channel_create_torrent_progress_label.setText(tr("Created torrent"))
         if 'torrent' in result:
             self.create_torrent_notification.emit({"msg": tr("Torrent successfully created")})
-            if self.dialog_widget.add_to_channel_checkbox.isChecked():
-                self.add_torrent_to_channel(result['torrent'])
             self.close_dialog()
-
-    def add_torrent_to_channel(self, torrent):
-        data = {"torrent": torrent}
-        if self.name:
-            data.update({"title": ensure_unicode(self.name, 'utf8')})
-        self.rest_request2 = TriblerNetworkRequest(
-            "mychannel/torrents", self.on_torrent_to_channel_added, data=data, method='PUT'
-        )
-
-    def on_torrent_to_channel_added(self, result):
-        if not result:
-            return
-        if 'added' in result:
-            self.create_torrent_notification.emit({"msg": tr("Torrent successfully added to the channel")})
+            if self.dialog_widget.add_to_channel_checkbox.isChecked():
+                self.add_to_channel_selected.emit(result['torrent'])
 
     def on_select_save_directory(self, checked):
         chosen_dir = QFileDialog.getExistingDirectory(
diff --git a/src/tribler-gui/tribler_gui/tribler_window.py b/src/tribler-gui/tribler_gui/tribler_window.py
index ceb4564bb86..b36bdbe52e6 100644
--- a/src/tribler-gui/tribler_gui/tribler_window.py
+++ b/src/tribler-gui/tribler_gui/tribler_window.py
@@ -559,24 +559,40 @@ def perform_start_download_request(
         self.update_recent_download_locations(destination)
 
         if add_to_channel:
+            self.show_add_torrent_to_channel_dialog_from_uri(uri)
+
+    def show_add_torrent_to_channel_dialog_from_uri(self, uri):
+        def on_add_button_pressed(channel_id):
+            post_data = {}
+            if uri.startswith("file:"):
+                with open(uri_to_path(uri), "rb") as torrent_file:
+                    post_data['torrent'] = b64encode(torrent_file.read()).decode('utf8')
+            elif uri.startswith("magnet:"):
+                post_data['uri'] = uri
+
+            if post_data:
+                TriblerNetworkRequest(
+                    f"channels/mychannel/{channel_id}/torrents",
+                    lambda _: self.tray_show_message(tr("Channel update"), tr("Torrent(s) added to your channel")),
+                    method='PUT',
+                    data=post_data,
+                )
 
-            def on_add_button_pressed(channel_id):
-                post_data = {}
-                if uri.startswith("file:"):
-                    with open(uri_to_path(uri), "rb") as torrent_file:
-                        post_data['torrent'] = b64encode(torrent_file.read()).decode('utf8')
-                elif uri.startswith("magnet:"):
-                    post_data['uri'] = uri
+        self.window().add_to_channel_dialog.show_dialog(on_add_button_pressed, confirm_button_text="Add torrent")
 
-                if post_data:
-                    TriblerNetworkRequest(
-                        f"channels/mychannel/{channel_id}/torrents",
-                        lambda _: self.tray_show_message(tr("Channel update"), tr("Torrent(s) added to your channel")),
-                        method='PUT',
-                        data=post_data,
-                    )
+    def show_add_torrent_to_channel_dialog_from_torrent_data(self, torrent_data):
+        def on_add_button_pressed(channel_id):
+            post_data = {'torrent': torrent_data}
+
+            if post_data:
+                TriblerNetworkRequest(
+                    f"channels/mychannel/{channel_id}/torrents",
+                    lambda _: self.tray_show_message(tr("Channel update"), tr("Torrent(s) added to your channel")),
+                    method='PUT',
+                    data=post_data,
+                )
 
-            self.window().add_to_channel_dialog.show_dialog(on_add_button_pressed, confirm_button_text="Add torrent")
+        self.window().add_to_channel_dialog.show_dialog(on_add_button_pressed, confirm_button_text="Add torrent")
 
     def on_new_version_available(self, version):
         if version == str(self.gui_settings.value('last_reported_version')):
@@ -740,6 +756,7 @@ def on_create_torrent(self, checked):
 
         self.create_dialog = CreateTorrentDialog(self)
         connect(self.create_dialog.create_torrent_notification, self.on_create_torrent_updates)
+        connect(self.create_dialog.add_to_channel_selected, self.show_add_torrent_to_channel_dialog_from_torrent_data)
         self.create_dialog.show()
 
     def on_create_torrent_updates(self, update_dict):