From aed43f34dd0f343fce7fb97431dd34a605efe2b4 Mon Sep 17 00:00:00 2001 From: Schimon Jehudah Date: Mon, 18 Mar 2024 18:49:52 +0000 Subject: [PATCH] Add a handler to handle pending tasks. --- slixfeed/action.py | 39 +++++++++++++++++++--------------- slixfeed/version.py | 4 ++-- slixfeed/xmpp/client.py | 10 ++++++++- slixfeed/xmpp/component.py | 9 +++++++- slixfeed/xmpp/connect.py | 2 -- slixfeed/xmpp/process.py | 43 ++++++++++++++++++++++++++++++++++++-- 6 files changed, 82 insertions(+), 25 deletions(-) diff --git a/slixfeed/action.py b/slixfeed/action.py index a10bc7b..1e21092 100644 --- a/slixfeed/action.py +++ b/slixfeed/action.py @@ -153,25 +153,30 @@ async def xmpp_send_status(self, jid): status_mode = 'xa' status_text = '📪️ Send "Start" to receive updates' else: - feeds = sqlite.get_number_of_items(db_file, 'feeds') - # print(await current_time(), jid, "has", feeds, "feeds") - if not feeds: - status_mode = 'available' - status_text = '📪️ Send a URL from a blog or a news website' + jid_task = self.pending_tasks[jid] + if len(jid_task): + status_mode = 'dnd' + status_text = jid_task[list(jid_task.keys())[0]] else: - unread = sqlite.get_number_of_entries_unread(db_file) - if unread: - status_mode = 'chat' - status_text = '📬️ There are {} news items'.format(str(unread)) - # status_text = ( - # "📰 News items: {}" - # ).format(str(unread)) - # status_text = ( - # "📰 You have {} news items" - # ).format(str(unread)) - else: + feeds = sqlite.get_number_of_items(db_file, 'feeds') + # print(await current_time(), jid, "has", feeds, "feeds") + if not feeds: status_mode = 'available' - status_text = '📭️ No news' + status_text = '📪️ Send a URL from a blog or a news website' + else: + unread = sqlite.get_number_of_entries_unread(db_file) + if unread: + status_mode = 'chat' + status_text = '📬️ There are {} news items'.format(str(unread)) + # status_text = ( + # "📰 News items: {}" + # ).format(str(unread)) + # status_text = ( + # "📰 You have {} news items" + # ).format(str(unread)) + else: + status_mode = 'available' + status_text = '📭️ No news' # breakpoint() # print(await current_time(), status_text, "for", jid) diff --git a/slixfeed/version.py b/slixfeed/version.py index d0bc7fe..1e8ec43 100644 --- a/slixfeed/version.py +++ b/slixfeed/version.py @@ -1,2 +1,2 @@ -__version__ = '0.1.45' -__version_info__ = (0, 1, 45) +__version__ = '0.1.46' +__version_info__ = (0, 1, 46) diff --git a/slixfeed/xmpp/client.py b/slixfeed/xmpp/client.py index 2e04b1e..c908959 100644 --- a/slixfeed/xmpp/client.py +++ b/slixfeed/xmpp/client.py @@ -120,6 +120,9 @@ class Slixfeed(slixmpp.ClientXMPP): # Handler for tasks self.task_manager = {} + # Handler for task messages + self.pending_tasks = {} + # Handler for ping self.task_ping_instance = {} @@ -349,6 +352,11 @@ class Slixfeed(slixmpp.ClientXMPP): # message_body = 'Share online status to receive updates.' # XmppMessage.send_headline(self, jid_bare, message_subject, # message_body, 'chat') + + if jid_bare not in self.pending_tasks: + self.pending_tasks[jid_bare] = {} + # if jid_full not in self.pending_tasks: + # self.pending_tasks[jid_full] = {} await process.message(self, message) # chat_type = message["type"] # message_body = message["body"] @@ -2749,7 +2757,7 @@ class Slixfeed(slixmpp.ClientXMPP): if jid_bare not in self.settings: Config.add_settings_jid(self.settings, jid_bare, db_file) form = self['xep_0004'].make_form('form', 'Settings') - form['instructions'] = 'Editing settings' + form['instructions'] = 'Editing settings of {}'.format(jid_bare) value = Config.get_setting_value(self.settings, jid_bare, 'enabled') value = str(value) value = int(value) diff --git a/slixfeed/xmpp/component.py b/slixfeed/xmpp/component.py index 1327ebb..40ffa4e 100644 --- a/slixfeed/xmpp/component.py +++ b/slixfeed/xmpp/component.py @@ -111,6 +111,9 @@ class SlixfeedComponent(slixmpp.ComponentXMPP): # Handler for tasks self.task_manager = {} + # Handler for task messages + self.pending_tasks = {} + # Handler for ping self.task_ping_instance = {} @@ -308,6 +311,10 @@ class SlixfeedComponent(slixmpp.ComponentXMPP): # self.send_raw(str(presence_probe)) # presence_probe.send() + if jid_bare not in self.pending_tasks: + self.pending_tasks[jid_bare] = {} + # if jid_full not in self.pending_tasks: + # self.pending_tasks[jid_full] = {} await process.message(self, message) # chat_type = message["type"] # message_body = message["body"] @@ -2709,7 +2716,7 @@ class SlixfeedComponent(slixmpp.ComponentXMPP): if jid_bare not in self.settings: Config.add_settings_jid(self.settings, jid_bare, db_file) form = self['xep_0004'].make_form('form', 'Settings') - form['instructions'] = 'Editing settings' + form['instructions'] = 'Editing settings of {}'.format(jid_bare) value = Config.get_setting_value(self.settings, jid_bare, 'enabled') value = str(value) value = int(value) diff --git a/slixfeed/xmpp/connect.py b/slixfeed/xmpp/connect.py index d85bad1..1d90f48 100644 --- a/slixfeed/xmpp/connect.py +++ b/slixfeed/xmpp/connect.py @@ -14,7 +14,6 @@ TODO """ import asyncio -from slixfeed.config import get_value from slixfeed.dt import current_time from slixmpp.exceptions import IqTimeout, IqError from time import sleep @@ -36,7 +35,6 @@ class XmppConnect: Returns ------- None. - """ jid_from = str(self.boundjid) if self.is_component else None if not jid: diff --git a/slixfeed/xmpp/process.py b/slixfeed/xmpp/process.py index 7a88f79..48a5a7a 100644 --- a/slixfeed/xmpp/process.py +++ b/slixfeed/xmpp/process.py @@ -46,6 +46,8 @@ from slixfeed.xmpp.upload import XmppUpload from slixfeed.xmpp.utility import get_chat_type, is_moderator, is_operator import time +from random import randrange + try: import tomllib except: @@ -104,6 +106,9 @@ async def message(self, message): task.clean_tasks_xmpp(self, jid_bare, key_list) status_type = 'dnd' status_message = '📥️ Procesing request to import feeds...' + # pending_tasks_num = len(self.pending_tasks[jid_bare]) + pending_tasks_num = randrange(10000, 99999) + self.pending_tasks[jid_bare][pending_tasks_num] = status_message XmppPresence.send(self, jid_bare, status_message, status_type=status_type) db_file = config.get_pathname_to_database(jid_file) @@ -112,6 +117,7 @@ async def message(self, message): response = 'Successfully imported {} feeds.'.format(count) else: response = 'OPML file was not imported.' + del self.pending_tasks[jid_bare][pending_tasks_num] key_list = ['status'] await task.start_tasks_xmpp(self, jid_bare, key_list) XmppMessage.send_reply(self, message, response) @@ -280,6 +286,13 @@ async def message(self, message): 'Send "help" for further instructions.\n' .format(self.alias)) XmppMessage.send_reply(self, message, response) + + title = message_lowercase + summary = message_text + # from random import randrange + node = str(randrange(10000, 99999)) + iq = XmppPubsub.create(self, title, summary, node) + await XmppIQ.send(self, iq) # case _ if message_lowercase.startswith('activate'): # if message['type'] == 'groupchat': @@ -513,6 +526,9 @@ async def message(self, message): status_message = ('📤️ Procesing request to ' 'export feeds into {}...' .format(ext.upper())) + # pending_tasks_num = len(self.pending_tasks[jid_bare]) + pending_tasks_num = randrange(10000, 99999) + self.pending_tasks[jid_bare][pending_tasks_num] = status_message XmppPresence.send(self, jid_bare, status_message, status_type=status_type) filename = await action.export_feeds(self, jid_bare, @@ -524,6 +540,7 @@ async def message(self, message): # XmppMessage.send_oob_reply_message(message, url, response) chat_type = await get_chat_type(self, jid_bare) XmppMessage.send_oob(self, jid_bare, url, chat_type) + del self.pending_tasks[jid_bare][pending_tasks_num] key_list = ['status'] await task.start_tasks_xmpp(self, jid_bare, key_list) else: @@ -559,6 +576,9 @@ async def message(self, message): status_type = 'dnd' status_message = ('📃️ Procesing request to produce {} ' 'document...'.format(ext.upper())) + # pending_tasks_num = len(self.pending_tasks[jid_bare]) + pending_tasks_num = randrange(10000, 99999) + self.pending_tasks[jid_bare][pending_tasks_num] = status_message XmppPresence.send(self, jid_bare, status_message, status_type=status_type) db_file = config.get_pathname_to_database(jid_file) @@ -612,8 +632,6 @@ async def message(self, message): response = ('> {}\n' 'Failed to fetch URL. Reason: {}' .format(url, code)) - key_list = ['status'] - await task.start_tasks_xmpp(self, jid_bare, key_list) else: response = ('No action has been taken.' '\n' @@ -623,6 +641,9 @@ async def message(self, message): response = ('Unsupported filetype.\n' 'Try: epub, html, md (markdown), ' 'pdf, or txt (text)') + del self.pending_tasks[jid_bare][pending_tasks_num] + key_list = ['status'] + await task.start_tasks_xmpp(self, jid_bare, key_list) if response: logging.warning('Error for URL {}: {}'.format(url, error)) XmppMessage.send_reply(self, message, response) @@ -633,6 +654,9 @@ async def message(self, message): task.clean_tasks_xmpp(self, jid_bare, key_list) status_type = 'dnd' status_message = '📥️ Procesing request to import feeds...' + # pending_tasks_num = len(self.pending_tasks[jid_bare]) + pending_tasks_num = randrange(10000, 99999) + self.pending_tasks[jid_bare][pending_tasks_num] = status_message XmppPresence.send(self, jid_bare, status_message, status_type=status_type) db_file = config.get_pathname_to_database(jid_file) @@ -642,6 +666,7 @@ async def message(self, message): .format(count)) else: response = 'OPML file was not imported.' + del self.pending_tasks[jid_bare][pending_tasks_num] key_list = ['status'] await task.start_tasks_xmpp(self, jid_bare, key_list) XmppMessage.send_reply(self, message, response) @@ -652,6 +677,10 @@ async def message(self, message): status_type = 'dnd' status_message = ('📫️ Processing request to fetch data from {}' .format(url)) + # pending_tasks_num = len(self.pending_tasks[jid_bare]) + pending_tasks_num = randrange(10000, 99999) + self.pending_tasks[jid_bare][pending_tasks_num] = status_message + print(self.pending_tasks) XmppPresence.send(self, jid_bare, status_message, status_type=status_type) if url.startswith('feed:'): @@ -687,6 +716,9 @@ async def message(self, message): 'added to subscription list.' .format(result['link'], result['name'])) # task.clean_tasks_xmpp(self, jid_bare, ['status']) + print(self.pending_tasks) + del self.pending_tasks[jid_bare][pending_tasks_num] + print(self.pending_tasks) key_list = ['status'] await task.start_tasks_xmpp(self, jid_bare, key_list) # except: @@ -897,6 +929,8 @@ async def message(self, message): status_type = 'dnd' status_message = ('📫️ Processing request to fetch data ' 'from {}'.format(url)) + + self.pending_tasks[jid_bare][pending_tasks_num] = status_message XmppPresence.send(self, jid_bare, status_message, status_type=status_type) if url.startswith('feed:'): @@ -927,6 +961,7 @@ async def message(self, message): '\n' 'Missing URL.') XmppMessage.send_reply(self, message, response) + del self.pending_tasks[jid_bare][pending_tasks_num] key_list = ['status'] await task.start_tasks_xmpp(self, jid_bare, key_list) case _ if message_lowercase.startswith('recent '): @@ -1003,6 +1038,9 @@ async def message(self, message): task.clean_tasks_xmpp(self, jid_bare, key_list) status_type = 'dnd' status_message = '📫️ Marking entries as read...' + # pending_tasks_num = len(self.pending_tasks[jid_bare]) + pending_tasks_num = randrange(10000, 99999) + self.pending_tasks[jid_bare][pending_tasks_num] = status_message XmppPresence.send(self, jid_bare, status_message, status_type=status_type) db_file = config.get_pathname_to_database(jid_file) @@ -1044,6 +1082,7 @@ async def message(self, message): await sqlite.mark_all_as_read(db_file) response = 'All entries have been marked as read.' XmppMessage.send_reply(self, message, response) + del self.pending_tasks[jid_bare][pending_tasks_num] key_list = ['status'] await task.start_tasks_xmpp(self, jid_bare, key_list) case _ if message_lowercase.startswith('search '):