From 57553abc0a29e008dc8f683439ea70004c254c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 12 Apr 2020 16:45:21 +0200 Subject: [PATCH] plugins/upload: Use embed directly instead of populating input field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- plugins/embed.py | 14 ++++++++------ plugins/upload.py | 22 +++++++++++++++++----- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/plugins/embed.py b/plugins/embed.py index 9895a927..4226c420 100644 --- a/plugins/embed.py +++ b/plugins/embed.py @@ -28,14 +28,13 @@ class Plugin(BasePlugin): help='Embed an image url into the contact\'s client', usage='') - def embed_image_url(self, args): + def embed_image_url(self, url): tab = self.api.current_tab() message = self.core.xmpp.make_message(tab.jid) - message['body'] = args - message['oob']['url'] = args - if isinstance(tab, tabs.MucTab): - message['type'] = 'groupchat' - else: + message['body'] = url + message['oob']['url'] = url + message['type'] = 'groupchat' + if not isinstance(tab, tabs.MucTab): message['type'] = 'chat' tab.add_message( message['body'], @@ -46,3 +45,6 @@ class Plugin(BasePlugin): typ=1, ) message.send() + # TODO: Fix refreshing. The following doesn't work. + tab.refresh() + self.core.tab_win.refresh() diff --git a/plugins/upload.py b/plugins/upload.py index 7e25070e..5e6dfb04 100644 --- a/plugins/upload.py +++ b/plugins/upload.py @@ -16,6 +16,9 @@ This plugin adds a command to the chat tabs. """ + +from typing import Optional + import asyncio import traceback from os.path import expanduser @@ -30,7 +33,11 @@ from poezio import tabs class Plugin(BasePlugin): + dependencies = {'embed'} + def init(self): + self.embed = self.refs['embed'] + if not self.core.xmpp['xep_0363']: raise Exception('slixmpp XEP-0363 plugin failed to load') for _class in (tabs.PrivateTab, tabs.StaticConversationTab, tabs.DynamicConversationTab, tabs.MucTab): @@ -43,18 +50,23 @@ class Plugin(BasePlugin): short='Upload a file', completion=self.completion_filename) - async def async_upload(self, filename): + async def upload(self, filename) -> Optional[str]: try: url = await self.core.xmpp['xep_0363'].upload_file(filename) except UploadServiceNotFound: self.api.information('HTTP Upload service not found.', 'Error') - return + return None except Exception: exception = traceback.format_exc() self.api.information('Failed to upload file: %s' % exception, 'Error') - return - self.core.insert_input_text(url) + return None + return url + + async def send_upload(self, filename): + url = await self.upload(filename) + if url is not None: + self.embed.embed_image_url(url) @command_args_parser.quoted(1) def command_upload(self, args): @@ -63,7 +75,7 @@ class Plugin(BasePlugin): return filename, = args filename = expanduser(filename) - asyncio.ensure_future(self.async_upload(filename)) + asyncio.ensure_future(self.send_upload(filename)) @staticmethod def completion_filename(the_input):