From 45abcfc7dcd3a8247b31d4bb0a276b9536db4267 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 28 Dec 2021 01:00:02 +0000 Subject: [PATCH] Post new trades and messages to the channel --- handler/agora.py | 37 +++++++++++++++++++++++++++++++++---- handler/irc.py | 4 ++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/handler/agora.py b/handler/agora.py index 5d71d03..4db264b 100644 --- a/handler/agora.py +++ b/handler/agora.py @@ -24,8 +24,13 @@ class Agora(object): self.log = Logger("agora") self.agora = AgoraDesk(settings.Agora.Token) self.cr = CurrencyRates() + + # Cache for detecting new trades self.last_dash = set() + # Cache for detecting new messages + self.last_messages = {} + def set_irc(self, irc): self.irc = irc @@ -50,33 +55,43 @@ class Agora(object): dash_tmp.append(contact_id) return dash_tmp - def dashboard(self): + def dashboard(self, send_irc=True): """ Get information about our open trades. + Post new trades to IRC and cache trades for the future. :return: human readable list of strings about our trades :rtype: list """ dash = self.agora.dashboard_seller() dash_tmp = [] + current_trades = [] if dash["response"]["data"]["contact_count"] > 0: for contact in dash["response"]["data"]["contact_list"]: contact_id = contact["data"]["contact_id"] + current_trades.append(contact_id) buyer = contact["data"]["buyer"]["username"] amount = contact["data"]["amount"] amount_xmr = contact["data"]["amount_xmr"] currency = contact["data"]["currency"] if not contact["data"]["is_selling"]: continue - if contact_id not in self.last_dash: - self.irc.client.msg(self.irc.client.channel, f"AUTO {contact_id}: {buyer} {amount}{currency} {amount_xmr}XMR") + if send_irc: + if contact_id not in self.last_dash: + self.irc.client.msg(self.irc.client.channel, f"AUTO {contact_id}: {buyer} {amount}{currency} {amount_xmr}XMR") dash_tmp.append(f"{contact_id}: {buyer} {amount}{currency} {amount_xmr}XMR") self.last_dash.add(contact_id) + + # Purge old trades from cache + for trade in self.last_dash: + if trade not in current_trades: + self.last_dash.remove(trade) return dash_tmp - def get_messages(self, contact_id): + def get_messages(self, contact_id, send_irc=True): """ Get messages for a certain trade ID. + Post new messages to IRC and cache messages for the future. :param contact_id: trade/contact ID :return: list of messages :rtype: list @@ -85,6 +100,15 @@ class Agora(object): messages_tmp = [] for message in messages["response"]["data"]["message_list"]: messages_tmp.append(f"({message['sender']['username']}): {message['msg']}") + if send_irc: + if contact_id in self.last_messages: + if not len(self.last_messages[contact_id]) == len(messages_tmp): + difference = set(messages_tmp) ^ self.last_messages[contact_id] + for x in difference: + self.irc.client.msg(self.irc.client.channel, x) + else: + for x in messages_tmp: + self.irc.client.msg(self.irc.client.channel, x) return messages_tmp def get_all_messages(self): @@ -99,6 +123,11 @@ class Agora(object): messages = self.get_messages(contact_id) messages_tmp[contact_id] = messages + # Purge old trades from cache + for trade in self.last_messages: + if trade not in messages_tmp: + del self.last_messages[trade] + return messages_tmp def get_ads(self): diff --git a/handler/irc.py b/handler/irc.py index d753f73..7a3485d 100644 --- a/handler/irc.py +++ b/handler/irc.py @@ -58,7 +58,7 @@ class IRCBot(irc.IRCClient): if cmd == "trades" and host in self.admins: # Get details of open trades and post on IRC - trades = self.agora.dashboard() + trades = self.agora.dashboard(send_irc=False) for trade_id in trades: self.msg(channel, trade_id) @@ -81,7 +81,7 @@ class IRCBot(irc.IRCClient): elif cmd == "messages" and host in self.admins and len(spl) == 2: # Get all messages for a given trade - messages = self.agora.get_messages(spl[1]) + messages = self.agora.get_messages(spl[1], send_irc=False) for message in messages: self.msg(channel, f"{spl[1]}: {message}")