diff --git a/api/logics.py b/api/logics.py index dfc5e9a2..e67ffce9 100644 --- a/api/logics.py +++ b/api/logics.py @@ -4,7 +4,7 @@ from api.lightning.node import LNNode from django.db.models import Q from api.models import Order, LNPayment, MarketTick, User, Currency -from api.messages import Telegram +from api.tasks import send_message from decouple import config import math @@ -30,7 +30,6 @@ FIAT_EXCHANGE_DURATION = int(config("FIAT_EXCHANGE_DURATION")) class Logics: - telegram = Telegram() @classmethod def validate_already_maker_or_taker(cls, user): """Validates if a use is already not part of an active order""" @@ -129,7 +128,7 @@ class Logics: order.expires_at = timezone.now() + timedelta( seconds=Order.t_to_expire[Order.Status.TAK]) order.save() - cls.telegram.order_taken(order) + send_message.delay(order.id,'order_taken') return True, None def is_buyer(order, user): @@ -207,13 +206,14 @@ class Logics: elif order.status == Order.Status.PUB: cls.return_bond(order.maker_bond) order.status = Order.Status.EXP - cls.telegram.order_expired_untaken(order) order.save() + send_message.delay(order.id,'order_expired_untaken') return True elif order.status == Order.Status.TAK: cls.cancel_bond(order.taker_bond) cls.kick_taker(order) + send_message.delay(order.id,'taker_expired_b4bond') return True elif order.status == Order.Status.WF2: @@ -520,11 +520,11 @@ class Logics: to prevent DDOS on the LN node and order book. If not strict, maker is returned the bond (more user friendly).""" elif order.status == Order.Status.PUB and order.maker == user: - # Settle the maker bond (Maker loses the bond for cancelling public order) - if cls.return_bond(order.maker_bond - ): # strict: cls.settle_bond(order.maker_bond): + # Return the maker bond (Maker gets returned the bond for cancelling public order) + if cls.return_bond(order.maker_bond): # strict cancellation: cls.settle_bond(order.maker_bond): order.status = Order.Status.UCA order.save() + send_message.delay(order.id,'public_order_cancelled') return True, None # 3) When taker cancels before bond @@ -534,6 +534,7 @@ class Logics: # adds a timeout penalty cls.cancel_bond(order.taker_bond) cls.kick_taker(order) + send_message.delay(order.id,'taker_canceled_b4bond') return True, None # 4) When taker or maker cancel after bond (before escrow) @@ -1000,7 +1001,7 @@ class Logics: order.payout.status = LNPayment.Status.FLIGHT order.payout.save() order.save() - cls.telegram.trade_successful(order) + send_message.delay(order.id,'trade_successful') return True, None else: diff --git a/api/messages.py b/api/messages.py index f69c1d42..bddb8424 100644 --- a/api/messages.py +++ b/api/messages.py @@ -90,11 +90,52 @@ class Telegram(): return lang = user.profile.telegram_lang_code - site = config('HOST_NAME') if lang == 'es': - text = f'¡Tu orden con ID ha finalizado exitosamente!⚡ Unase a @robosats_es y ayudanos a mejorar.' + text = f'¡Tu orden con ID {order.id} ha finalizado exitosamente!⚡ Unase a @robosats_es y ayudanos a mejorar.' else: - text = f'Your order with ID has finished successfully!⚡ Join us @robosats and help us improve.' + text = f'Your order with ID {order.id} has finished successfully!⚡ Join us @robosats and help us improve.' + + self.send_message(user, text) + return + + def public_order_cancelled(self, order): + user = order.maker + if not user.profile.telegram_enabled: + return + + lang = user.profile.telegram_lang_code + if lang == 'es': + text = f'Has cancelado tu orden pública con ID {order.id}.' + else: + text = f'You have cancelled your public order with ID {order.id}.' + + self.send_message(user, text) + return + + def taker_canceled_b4bond(self, order): + user = order.maker + if not user.profile.telegram_enabled: + return + + lang = user.profile.telegram_lang_code + if lang == 'es': + text = f'El tomador ha cancelado antes de bloquear su fianza. Tu orden con ID {order.id} vuelve a ser pública.' + else: + text = f'The taker has canceled before locking the bond. Your order with ID {order.id} is once again public in the order book.' + + self.send_message(user, text) + return + + def taker_expired_b4bond(self, order): + user = order.maker + if not user.profile.telegram_enabled: + return + + lang = user.profile.telegram_lang_code + if lang == 'es': + text = f'El tomador no ha bloqueado la fianza a tiempo. Tu orden con ID {order.id} vuelve a ser pública.' + else: + text = f'The taker has not locked the bond in time. Your order with ID {order.id} is once again public in the order book.' self.send_message(user, text) return \ No newline at end of file diff --git a/api/tasks.py b/api/tasks.py index 038e7190..9c402fdd 100644 --- a/api/tasks.py +++ b/api/tasks.py @@ -152,3 +152,34 @@ def cache_market(): ) return results + +@shared_task(name="send_message", ignore_result=True) +def send_message(order_id, message): + + from api.models import Order + order = Order.objects.get(id=order_id) + if not order.maker.profile.telegram_enabled: + return + + from api.messages import Telegram + telegram = Telegram() + + if message == 'order_taken': + telegram.order_taken(order) + + elif message == 'order_expired_untaken': + telegram.order_expired_untaken(order) + + elif message == 'trade_successful': + telegram.trade_successful(order) + + elif message == 'public_order_cancelled': + telegram.public_order_cancelled(order) + + elif message == 'taker_expired_b4bond': + telegram.taker_expired_b4bond(order) + + elif message == 'taker_canceled_b4bond': + telegram.taker_canceled_b4bond(order) + + return \ No newline at end of file