From c40390dd5c3fcce8fd3289d2d2acde70aa792c0f Mon Sep 17 00:00:00 2001 From: Alexander PapaTutuWawa Date: Thu, 17 Sep 2020 15:47:55 +0200 Subject: [PATCH] fix: Make subscribing and unsubscribing work --- janine/janine.py | 78 +++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/janine/janine.py b/janine/janine.py index 4736c3c..6859a27 100644 --- a/janine/janine.py +++ b/janine/janine.py @@ -77,6 +77,8 @@ class WarningBot: # Configuration stuff self._data_dir = '' + self._client_store = '' + self._warning_store = '' self._load_config() @@ -88,27 +90,6 @@ class WarningBot: self._jid, aioxmpp.make_security_layer(self._password)) - # First we check if the entered Landkreis is valid - self._channels = [] - channels = {} - channel_file = os.path.join(self._data_dir, 'channels.json') - if not os.path.exists(channel_file): - log.debug('Requesting search channels') - req = requests.get(MiscDataSources.channels()) - channels = json.loads(req.text) - self._channels = list(map(lambda key: channels[key].get('NAME', ''), - channels.keys())) - - try: - with open(channel_file, 'w') as f: - f.write(json.dumps(self._channels)) - except Exception as err: - log.error('Failed to cache channel data:') - log.error(str(err)) - else: - with open(channel_file, 'r') as f: - self._channels = json.loads(f.read()) - async with self._client.connected() as stream: logging.info('Client connected to server') @@ -145,6 +126,9 @@ class WarningBot: await periodic def __is_message_valid(self, msg): + ''' + Returns True on messages we want to handle. False otherwise. + ''' if msg.type_ != aioxmpp.MessageType.CHAT: return False @@ -158,6 +142,9 @@ class WarningBot: return True def __make_msg(self, to, body): + ''' + Wrapper for creating a message object to enqueue or send. + ''' msg = aioxmpp.Message( type_=aioxmpp.MessageType.CHAT, to=to) @@ -191,13 +178,15 @@ class WarningBot: if not landkreis in self._warn_clients.keys(): self._warn_clients[landkreis] = [] - self._warn_clients[landkreis].append(str(msg.from_)) + self._warn_clients[landkreis].append(str(msg.from_.bare())) - # TODO: Flush self._warn_clients to disk self._client.enqueue(self.__make_msg( to=msg.from_, body=f'Du erhälst nun Nachrichten zu {landkreis} von mir')) + with open(self._client_store, 'w') as cf: + cf.write(json.dumps(self._warn_clients)) + # Send all known warnings for the landkreis to the user for warning in self._warnings0: if landkreis in warning.landkreise: @@ -215,19 +204,22 @@ class WarningBot: return landkreis = ' '.join(cmd_parts[1:]) - if not landreis in self._warn_clients.keys(): + if not landkreis in self._warn_clients: self._client.enqueue(self.__make_msg( to=msg.from_, body=f'Du hast {landkreis} nicht abonniert')) return - if str(msg.from_) in self._warn_clients[landkreis]: - filter_ = lambda x: x != str(msg.from_) + if str(msg.from_.bare()) in self._warn_clients[landkreis]: + filter_ = lambda x: x != str(msg.from_.bare()) self._warn_clients[landkreis] = list(filter(filter_, self._warn_clients[landkreis])) self._client.enqueue(self.__make_msg( to=msg.from_, body=f'Du erhälst keine Nachrichten zu {landkreis} mehr von mir')) + + if len(self._warn_clients[landkreis]) == 0: + del self._warn_clients[landkreis] else: self._client.enqueue(self.__make_msg( to=msg.from_, @@ -237,6 +229,10 @@ class WarningBot: self._client.enqueue(self.__make_msg( to=msg.from_, body=body)) + else: + self._client.enqueue(self.__make_msg( + to=msg.from_, + body='Diesen Befehl kenne ich nicht... Mit "help" kannst du alle Befehle sehen, die ich kenne.')) async def _periodic_requests(self): ''' @@ -322,11 +318,33 @@ class WarningBot: self._recipients = config['General']['Recipients'].split(',') self._refresh_timeout = int(config['General']['Timeout']) - # Warning data - client_file = os.path.join(self._data_dir, 'clients.json') - if os.path.exists(client_file): - with open(client_file, 'r') as cf: + # Persistent data + # Subscribed clients + self._client_store = os.path.join(self._data_dir, 'clients.json') + if os.path.exists(self._client_store): + with open(self._client_store, 'r') as cf: self._warn_clients = json.loads(cf.read()) + + # Landkreise + self._channels = [] + channels = {} + channel_file = os.path.join(self._data_dir, 'channels.json') + if not os.path.exists(channel_file): + log.debug('Requesting search channels') + req = requests.get(MiscDataSources.channels()) + channels = json.loads(req.text) + self._channels = list(map(lambda key: channels[key].get('NAME', ''), + channels.keys())) + + try: + with open(channel_file, 'w') as f: + f.write(json.dumps(self._channels)) + except Exception as err: + log.error('Failed to cache channel data:') + log.error(str(err)) + else: + with open(channel_file, 'r') as f: + self._channels = json.loads(f.read()) # Bot Config self._jid = aioxmpp.JID.fromstr(config['Bot']['JID'])