include order_id

This commit is contained in:
KoalaSat
2024-06-07 15:47:11 +02:00
parent 3d1b673df8
commit 830846481d
4 changed files with 87 additions and 100 deletions

View File

@ -2,7 +2,7 @@
import uuid import uuid
from decouple import config from decouple import config
from django.contrib.auth.models import User from django.contrib.auth.models import User, Order
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
@ -26,6 +26,7 @@ class Notification(models.Model):
created_at = models.DateTimeField(default=timezone.now) created_at = models.DateTimeField(default=timezone.now)
user = models.ForeignKey(User, on_delete=models.SET_NULL, default=None) user = models.ForeignKey(User, on_delete=models.SET_NULL, default=None)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, default=None)
# notification details # notification details
title = models.CharField(max_length=120, null=False, default=None) title = models.CharField(max_length=120, null=False, default=None)

View File

@ -30,17 +30,19 @@ class Notifications:
return context return context
def send_message(self, robot, title, description): def send_message(self, order, robot, title, description):
"""Save a message for a user and sends it to Telegram""" """Save a message for a user and sends it to Telegram"""
self.save_message(robot, title, description) self.save_message(order, robot, title, description)
self.send_telegram_message(robot.telegram_chat_id, title, description) if robot.telegram_enabled:
self.send_telegram_message(robot.telegram_chat_id, title, description)
def save_message(self, robot, title, description): def save_message(self, order, robot, title, description):
"""Save a message for a user""" """Save a message for a user"""
notification = Notification() notification = Notification()
notification.title = title notification.title = title
notification.description = description notification.description = description
notification.user = robot notification.user = robot
notification.order = order
notification.save() notification.save()
def send_telegram_message(self, chat_id, title, description): def send_telegram_message(self, chat_id, title, description):
@ -65,110 +67,96 @@ class Notifications:
title = f"🔔 Hola {user.username}, te enviaré notificaciones sobre tus órdenes en RoboSats." title = f"🔔 Hola {user.username}, te enviaré notificaciones sobre tus órdenes en RoboSats."
else: else:
title = f"🔔 Hey {user.username}, I will send you notifications about your RoboSats orders." title = f"🔔 Hey {user.username}, I will send you notifications about your RoboSats orders."
self.send_message(user.robot.telegram_chat_id, title) self.send_telegram_message(user.robot.telegram_chat_id, title)
user.robot.telegram_welcomed = True user.robot.telegram_welcomed = True
user.robot.save(update_fields=["telegram_welcomed"]) user.robot.save(update_fields=["telegram_welcomed"])
return return
def order_taken_confirmed(self, order): def order_taken_confirmed(self, order):
if order.maker.robot.telegram_enabled: lang = order.maker.robot.telegram_lang_code
lang = order.maker.robot.telegram_lang_code if lang == "es":
if lang == "es": title = f"✅ Hey {order.maker.username} ¡Tu orden con ID {order.id} ha sido tomada por {order.taker.username}!🥳"
title = f"✅ Hey {order.maker.username} ¡Tu orden con ID {order.id} ha sido tomada por {order.taker.username}!🥳" description = f"Visita http://{self.site}/order/{order.id} para continuar."
description = ( else:
f"Visita http://{self.site}/order/{order.id} para continuar." title = f"✅ Hey {order.maker.username}, your order was taken by {order.taker.username}!🥳"
) description = (
else: f"Visit http://{self.site}/order/{order.id} to proceed with the trade."
title = f"✅ Hey {order.maker.username}, your order was taken by {order.taker.username}!🥳" )
description = f"Visit http://{self.site}/order/{order.id} to proceed with the trade." self.send_message(order, order.maker.robot, title, description)
self.send_message(order.maker.robot.telegram_chat_id, title, description)
if order.taker.robot.telegram_enabled: lang = order.taker.robot.telegram_lang_code
lang = order.taker.robot.telegram_lang_code if lang == "es":
if lang == "es": title = f"✅ Hey {order.taker.username}, acabas de tomar la orden con ID {order.id}."
title = f"✅ Hey {order.taker.username}, acabas de tomar la orden con ID {order.id}." else:
else: title = f"✅ Hey {order.taker.username}, you just took the order with ID {order.id}."
title = f"✅ Hey {order.taker.username}, you just took the order with ID {order.id}." self.send_message(order, order.taker.robot, title)
self.send_message(order.taker.robot.telegram_chat_id, title)
return return
def fiat_exchange_starts(self, order): def fiat_exchange_starts(self, order):
for user in [order.maker, order.taker]: for user in [order.maker, order.taker]:
if user.robot.telegram_enabled: lang = user.robot.telegram_lang_code
lang = user.robot.telegram_lang_code if lang == "es":
if lang == "es": title = f"✅ Hey {user.username}, el depósito de garantía y el recibo del comprador han sido recibidos. Es hora de enviar el dinero fiat."
title = f"✅ Hey {user.username}, el depósito de garantía y el recibo del comprador han sido recibidos. Es hora de enviar el dinero fiat." description = f"Visita http://{self.site}/order/{order.id} para hablar con tu contraparte."
description = f"Visita http://{self.site}/order/{order.id} para hablar con tu contraparte." else:
else: title = f"✅ Hey {user.username}, the escrow and invoice have been submitted. The fiat exchange starts now via the platform chat."
title = f"✅ Hey {user.username}, the escrow and invoice have been submitted. The fiat exchange starts now via the platform chat." description = f"Visit http://{self.site}/order/{order.id} to talk with your counterpart."
description = f"Visit http://{self.site}/order/{order.id} to talk with your counterpart." self.send_message(order, user.robot, title, description)
self.send_message(user.robot.telegram_chat_id, title, description)
return return
def order_expired_untaken(self, order): def order_expired_untaken(self, order):
if order.maker.robot.telegram_enabled: lang = order.maker.robot.telegram_lang_code
lang = order.maker.robot.telegram_lang_code if lang == "es":
if lang == "es": title = f"😪 Hey {order.maker.username}, tu orden con ID {order.id} ha expirado sin ser tomada por ningún robot."
title = f"😪 Hey {order.maker.username}, tu orden con ID {order.id} ha expirado sin ser tomada por ningún robot." description = f"Visita http://{self.site}/order/{order.id} para renovarla."
description = ( else:
f"Visita http://{self.site}/order/{order.id} para renovarla." title = f"😪 Hey {order.maker.username}, your order with ID {order.id} has expired without a taker."
) description = f"Visit http://{self.site}/order/{order.id} to renew it."
else: self.send_message(order, order.maker.robot, title, description)
title = f"😪 Hey {order.maker.username}, your order with ID {order.id} has expired without a taker."
description = f"Visit http://{self.site}/order/{order.id} to renew it."
self.send_message(order.maker.robot.telegram_chat_id, title, description)
return return
def trade_successful(self, order): def trade_successful(self, order):
for user in [order.maker, order.taker]: for user in [order.maker, order.taker]:
if user.robot.telegram_enabled: lang = user.robot.telegram_lang_code
lang = user.robot.telegram_lang_code if lang == "es":
if lang == "es": title = f"🥳 ¡Tu orden con ID {order.id} ha finalizado exitosamente!"
title = ( description = (
f"🥳 ¡Tu orden con ID {order.id} ha finalizado exitosamente!" "⚡ Únete a nosotros en @robosats_es y ayúdanos a mejorar."
) )
description = ( else:
"⚡ Únete a nosotros en @robosats_es y ayúdanos a mejorar." title = f"🥳 Your order with ID {order.id} has finished successfully!"
) description = "⚡ Join us @robosats and help us improve."
else: self.send_message(order, user.robot, title, description)
title = (
f"🥳 Your order with ID {order.id} has finished successfully!"
)
description = "⚡ Join us @robosats and help us improve."
self.send_message(user.robot.telegram_chat_id, title, description)
return return
def public_order_cancelled(self, order): def public_order_cancelled(self, order):
if order.maker.robot.telegram_enabled: lang = order.maker.robot.telegram_lang_code
lang = order.maker.robot.telegram_lang_code if lang == "es":
if lang == "es": title = f"❌ Hey {order.maker.username}, has cancelado tu orden pública con ID {order.id}."
title = f"❌ Hey {order.maker.username}, has cancelado tu orden pública con ID {order.id}." else:
else: title = f"❌ Hey {order.maker.username}, you have cancelled your public order with ID {order.id}."
title = f"❌ Hey {order.maker.username}, you have cancelled your public order with ID {order.id}." self.send_message(order, order.maker.robot, title)
self.send_message(order.maker.robot.telegram_chat_id, title)
return return
def collaborative_cancelled(self, order): def collaborative_cancelled(self, order):
for user in [order.maker, order.taker]: for user in [order.maker, order.taker]:
if user.robot.telegram_enabled: lang = user.robot.telegram_lang_code
lang = user.robot.telegram_lang_code if lang == "es":
if lang == "es": title = f"❌ Hey {user.username}, tu orden con ID {str(order.id)} fue cancelada colaborativamente."
title = f"❌ Hey {user.username}, tu orden con ID {str(order.id)} fue cancelada colaborativamente." else:
else: title = f"❌ Hey {user.username}, your order with ID {str(order.id)} has been collaboratively cancelled."
title = f"❌ Hey {user.username}, your order with ID {str(order.id)} has been collaboratively cancelled." self.send_message(order, user.robot, title)
self.send_message(user.robot.telegram_chat_id, title)
return return
def dispute_opened(self, order): def dispute_opened(self, order):
for user in [order.maker, order.taker]: for user in [order.maker, order.taker]:
if user.robot.telegram_enabled: lang = user.robot.telegram_lang_code
lang = user.robot.telegram_lang_code if lang == "es":
if lang == "es": title = f"⚖️ Hey {user.username}, la orden con ID {str(order.id)} ha entrado en disputa."
title = f"⚖️ Hey {user.username}, la orden con ID {str(order.id)} ha entrado en disputa." else:
else: title = f"⚖️ Hey {user.username}, a dispute has been opened on your order with ID {str(order.id)}."
title = f"⚖️ Hey {user.username}, a dispute has been opened on your order with ID {str(order.id)}." self.send_message(order, user.robot, title)
self.send_message(user.robot.telegram_chat_id, title)
admin_chat_id = config("TELEGRAM_COORDINATOR_CHAT_ID") admin_chat_id = config("TELEGRAM_COORDINATOR_CHAT_ID")
@ -186,18 +174,17 @@ class Notifications:
return return
def order_published(self, order): def order_published(self, order):
if order.maker.robot.telegram_enabled: lang = order.maker.robot.telegram_lang_code
lang = order.maker.robot.telegram_lang_code # In weird cases the order cannot be found (e.g. it is cancelled)
# In weird cases the order cannot be found (e.g. it is cancelled) queryset = Order.objects.filter(maker=order.maker)
queryset = Order.objects.filter(maker=order.maker) if len(queryset) == 0:
if len(queryset) == 0: return
return order = queryset.last()
order = queryset.last() if lang == "es":
if lang == "es": title = f"✅ Hey {order.maker.username}, tu orden con ID {str(order.id)} es pública en el libro de ordenes."
title = f"✅ Hey {order.maker.username}, tu orden con ID {str(order.id)} es pública en el libro de ordenes." else:
else: title = f"✅ Hey {order.maker.username}, your order with ID {str(order.id)} is public in the order book."
title = f"✅ Hey {order.maker.username}, your order with ID {str(order.id)} is public in the order book." self.send_message(order, order.maker.robot, title)
self.send_message(order.maker.robot.telegram_chat_id, title)
return return
def new_chat_message(self, order, chat_message): def new_chat_message(self, order, chat_message):
@ -225,14 +212,12 @@ class Notifications:
notification_reason = f"(You receive this notification because this was the first in-chat message. You will only be notified again if there is a gap bigger than {TIMEGAP} minutes between messages)" notification_reason = f"(You receive this notification because this was the first in-chat message. You will only be notified again if there is a gap bigger than {TIMEGAP} minutes between messages)"
user = chat_message.receiver user = chat_message.receiver
if user.robot.telegram_enabled: title = f"💬 Hey {user.username}, a new chat message in-app was sent to you by {chat_message.sender.username} for order ID {str(order.id)}. {notification_reason}"
title = f"💬 Hey {user.username}, a new chat message in-app was sent to you by {chat_message.sender.username} for order ID {str(order.id)}. {notification_reason}" self.send_message(order, user.robot, title)
self.send_message(user.robot.telegram_chat_id, title)
return return
def coordinator_cancelled(self, order): def coordinator_cancelled(self, order):
if order.maker.robot.telegram_enabled: title = f"🛠️ Your order with ID {order.id} has been cancelled by the coordinator {config('COORDINATOR_ALIAS', cast=str, default='NoAlias')} for the upcoming maintenance stop."
title = f"🛠️ Your order with ID {order.id} has been cancelled by the coordinator {config('COORDINATOR_ALIAS', cast=str, default='NoAlias')} for the upcoming maintenance stop." self.send_message(order, order.maker.robot, title)
self.send_message(order.maker.robot.telegram_chat_id, title)
return return

View File

@ -496,13 +496,13 @@ class NotificationSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Notification model = Notification
fields = ("title", "description") fields = ("title", "description", "order_id")
class ListNotificationSerializer(serializers.ModelSerializer): class ListNotificationSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Notification model = Notification
fields = ("title", "description") fields = ("title", "description", "order_id")
class OrderPublicSerializer(serializers.ModelSerializer): class OrderPublicSerializer(serializers.ModelSerializer):

View File

@ -765,6 +765,7 @@ class NotificationsView(ListAPIView):
data = ListNotificationSerializer(notification).data data = ListNotificationSerializer(notification).data
data["title"] = str(notification.title) data["title"] = str(notification.title)
data["description"] = str(notification.description) data["description"] = str(notification.description)
data["order_id"] = str(notification.order.id)
notification_data.append(data) notification_data.append(data)