diff --git a/osinaweb/db.sqlite3 b/osinaweb/db.sqlite3 index 506d9c6c..766342e0 100644 Binary files a/osinaweb/db.sqlite3 and b/osinaweb/db.sqlite3 differ diff --git a/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc b/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc index 3506914b..2e59f5e2 100644 Binary files a/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc and b/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc differ diff --git a/osinaweb/osichat/__pycache__/models.cpython-310.pyc b/osinaweb/osichat/__pycache__/models.cpython-310.pyc index 668ae231..a9783a52 100644 Binary files a/osinaweb/osichat/__pycache__/models.cpython-310.pyc and b/osinaweb/osichat/__pycache__/models.cpython-310.pyc differ diff --git a/osinaweb/osichat/migrations/0027_chatnotification_type.py b/osinaweb/osichat/migrations/0027_chatnotification_type.py new file mode 100644 index 00000000..36c0817f --- /dev/null +++ b/osinaweb/osichat/migrations/0027_chatnotification_type.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.5 on 2024-08-17 11:51 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('osichat', '0026_visitor_region'), + ] + + operations = [ + migrations.AddField( + model_name='chatnotification', + name='type', + field=models.CharField(choices=[('Visitor', 'Visitor'), ('Chat', 'Chat')], max_length=8, null=True), + ), + ] diff --git a/osinaweb/osichat/migrations/__pycache__/0027_chatnotification_type.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0027_chatnotification_type.cpython-310.pyc new file mode 100644 index 00000000..366713d4 Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0027_chatnotification_type.cpython-310.pyc differ diff --git a/osinaweb/osichat/models.py b/osinaweb/osichat/models.py index f0627eec..d1d01a38 100644 --- a/osinaweb/osichat/models.py +++ b/osinaweb/osichat/models.py @@ -9,57 +9,74 @@ from firebase_admin.messaging import Message,AndroidConfig,APNSConfig,APNSPayloa from django.utils.safestring import mark_safe + def send_notification(notification): notification_data = { 'title': notification.title, - 'body': mark_safe(notification.message), + 'body': mark_safe(notification.message), } + sound = 'default' + if notification.type == 'Visitor': + sound = 'https://osina.ositcom.com/static/notifications/new-visitor.mp3' + android_config = AndroidConfig( notification=AndroidNotification( title=notification_data['title'], body=notification_data['body'], - sound='default' - ) + sound=sound + ) ) apns_config = APNSConfig( - payload= APNSPayload( + payload=APNSPayload( aps=Aps( alert=ApsAlert( title=notification_data['title'], body=notification_data['body'], ), - sound='default', + sound=sound, ) ) ) if notification.image: FCMDevice.objects.send_message( - Message(notification=NotificationFB( - title=notification_data['title'], - body=notification_data['body'], - image= notification.image, - ), data={"image": notification.image},android=android_config,apns=apns_config) + Message( + notification=NotificationFB( + title=notification_data['title'], + body=notification_data['body'], + image=notification.image, + ), + data={"image": notification.image}, + android=android_config, + apns=apns_config ) + ) else: FCMDevice.objects.send_message( - Message(notification=NotificationFB( - title=notification_data['title'], - body=notification_data['body'], - ), - android=android_config, - apns=apns_config) + Message( + notification=NotificationFB( + title=notification_data['title'], + body=notification_data['body'], + ), + android=android_config, + apns=apns_config + ) ) # Create your models here. class ChatNotification(models.Model): + TYPES = ( + ('Visitor', 'Visitor'), + ('Chat', 'Chat') + ) title = models.CharField(max_length=255) message = models.TextField() image = models.TextField(blank=True,null=True) created_at = models.DateTimeField(auto_now_add=True) + type = models.CharField(max_length=8, choices=TYPES, null=True) def save(self, *args, **kwargs): is_new = not self.pk super().save(*args, **kwargs) @@ -119,9 +136,18 @@ class Visitor(models.Model): self.send_visitor_notification() def send_visitor_notification(self): + first_log = self.visitorlog_set.order_by('visit_date').first() + body = "" + if first_log: + if first_log.title: + body = f"New visitor navigated to {first_log.title}" + elif first_log.url: + body = f"New visitor navigated to {first_log.url}" notification = ChatNotification.objects.create( title="New visitor on Ositcom!", - image = self.notification_flag_image_url + body = body, + image = self.notification_flag_image_url, + type = "Visitor" ) @@ -148,7 +174,6 @@ class VisitorLog(models.Model): return f"{int(minutes):02}:{int(seconds):02}" def save(self, *args, **kwargs): - is_new = not self.pk if self.left_date: action = 'end_log' else: @@ -161,14 +186,9 @@ class VisitorLog(models.Model): 'action': action } async_to_sync(channel_layer.group_send)("osichat", event) - if is_new: - self.send_visitorlog_notification() - def send_visitorlog_notification(self): - notification = ChatNotification.objects.create( - title=f"Visitor navigated to: {self.title}.", - image=self.visitor.notification_flag_image_url - ) + + class ChatRoom(models.Model):