Asynchronous TG messages. Add taker_cancelled and taker_expired messages.

This commit is contained in:
Reckless_Satoshi 2022-02-23 08:15:48 -08:00
parent a9575c2338
commit 30e35f8973
No known key found for this signature in database
GPG Key ID: 9C4585B561315571
3 changed files with 84 additions and 11 deletions

View File

@ -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:

View File

@ -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

View File

@ -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