diff --git a/osinaweb/db.sqlite3 b/osinaweb/db.sqlite3 index c6579831..a28c7aed 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 bbf77517..5f8cd1a2 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 af06a981..1c33fa26 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/api/__pycache__/views.cpython-310.pyc b/osinaweb/osichat/api/__pycache__/views.cpython-310.pyc index 431f7df2..e19c7c4f 100644 Binary files a/osinaweb/osichat/api/__pycache__/views.cpython-310.pyc and b/osinaweb/osichat/api/__pycache__/views.cpython-310.pyc differ diff --git a/osinaweb/osichat/migrations/0030_chatnotification_users.py b/osinaweb/osichat/migrations/0030_chatnotification_users.py new file mode 100644 index 00000000..c5f67cab --- /dev/null +++ b/osinaweb/osichat/migrations/0030_chatnotification_users.py @@ -0,0 +1,20 @@ +# Generated by Django 4.2.5 on 2024-10-01 16:25 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('osichat', '0029_chatnotification_session_id'), + ] + + operations = [ + migrations.AddField( + model_name='chatnotification', + name='users', + field=models.ManyToManyField(null=True, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/osinaweb/osichat/migrations/0031_alter_chatnotification_users.py b/osinaweb/osichat/migrations/0031_alter_chatnotification_users.py new file mode 100644 index 00000000..19f65e72 --- /dev/null +++ b/osinaweb/osichat/migrations/0031_alter_chatnotification_users.py @@ -0,0 +1,20 @@ +# Generated by Django 4.2.5 on 2024-10-01 16:25 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('osichat', '0030_chatnotification_users'), + ] + + operations = [ + migrations.AlterField( + model_name='chatnotification', + name='users', + field=models.ManyToManyField(blank=True, null=True, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/osinaweb/osichat/migrations/0032_remove_chatnotification_users_chatnotification_user.py b/osinaweb/osichat/migrations/0032_remove_chatnotification_users_chatnotification_user.py new file mode 100644 index 00000000..d5fb4201 --- /dev/null +++ b/osinaweb/osichat/migrations/0032_remove_chatnotification_users_chatnotification_user.py @@ -0,0 +1,25 @@ +# Generated by Django 4.2.5 on 2024-10-01 16:40 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('osichat', '0031_alter_chatnotification_users'), + ] + + operations = [ + migrations.RemoveField( + model_name='chatnotification', + name='users', + ), + migrations.AddField( + model_name='chatnotification', + name='user', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/osinaweb/osichat/migrations/__pycache__/0030_chatnotification_users.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0030_chatnotification_users.cpython-310.pyc new file mode 100644 index 00000000..382786c5 Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0030_chatnotification_users.cpython-310.pyc differ diff --git a/osinaweb/osichat/migrations/__pycache__/0031_alter_chatnotification_users.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0031_alter_chatnotification_users.cpython-310.pyc new file mode 100644 index 00000000..716a1e12 Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0031_alter_chatnotification_users.cpython-310.pyc differ diff --git a/osinaweb/osichat/migrations/__pycache__/0032_remove_chatnotification_users_chatnotification_user.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0032_remove_chatnotification_users_chatnotification_user.cpython-310.pyc new file mode 100644 index 00000000..431e3de7 Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0032_remove_chatnotification_users_chatnotification_user.cpython-310.pyc differ diff --git a/osinaweb/osichat/models.py b/osinaweb/osichat/models.py index 232add82..b1341a6f 100644 --- a/osinaweb/osichat/models.py +++ b/osinaweb/osichat/models.py @@ -11,7 +11,6 @@ from django.utils.safestring import mark_safe def send_notification(notification): - try: notification_data = { 'title': notification.title, 'body': mark_safe(notification.message), @@ -58,47 +57,15 @@ def send_notification(notification): apns=apns_config ) - - FCMDevice.objects.send_message(message) + if notification.user: + FCMDevice.objects.filter(user=notification.user).send_message(message) + else: + FCMDevice.objects.send_message(message) - except Exception as e: - - error_message = f"Error sending notification: {str(e)}" - - - error_android_config = AndroidConfig( - notification=AndroidNotification( - title="Notification Error", - body=error_message, - sound="default", - ) - ) - error_apns_config = APNSConfig( - payload=APNSPayload( - aps=Aps( - alert=ApsAlert( - title="Notification Error", - body=error_message, - ), - sound="default", - ) - ) - ) - FCMDevice.objects.send_message( - Message( - notification=NotificationFB( - title="Notification Error", - body=error_message, - ), - data={"error": str(e)}, - android=error_android_config, - apns=error_apns_config - ) - ) -# Create your models here. +# Create your models here. class ChatNotification(models.Model): TYPES = ( ('Visitor', 'Visitor'), @@ -111,6 +78,7 @@ class ChatNotification(models.Model): type = models.CharField(max_length=8, choices=TYPES, null=True) type_id = models.IntegerField(null=True) session_id = models.CharField(max_length=200, null=True) + user = models.ForeignKey(User, blank=True, null=True, on_delete=models.CASCADE) def save(self, *args, **kwargs): is_new = not self.pk super().save(*args, **kwargs) @@ -337,17 +305,34 @@ class ChatMessage(models.Model): self.send_message_notification() def send_message_notification(self): type_id = self.room.id - session_id = self.room.chatroomguest.visitor.session_id - title = f"Visitor {self.room.chatroomguest.visitor.ip_address} sent a new message on Ositcom!" - body = f"{self.content}" - notification = ChatNotification.objects.create( - title=title, - message = body, - image = self.room.chatroomguest.visitor.notification_flag_image_url, - type = "Chat", - type_id = type_id, - session_id = session_id - ) + chatroom_guest = getattr(self.room, 'chatroomguest', None) + + if chatroom_guest: + session_id = self.room.chatroomguest.visitor.session_id + notification = ChatNotification.objects.create( + title=f"Visitor {self.room.chatroomguest.visitor.ip_address} sent a new message on Ositcom!", + message = f"{self.content}", + image = self.room.chatroomguest.visitor.notification_flag_image_url, + type = "Chat", + type_id = type_id, + session_id = session_id + ) + + else: + chat_members = ChatMember.objects.filter(room=self.room).exclude(member=self.member) + for chat_member in chat_members: + user = chat_member.member + notification = ChatNotification.objects.create( + title=f"New message from {self.member.first_name} {self.member.last_name}!", + message= f"{self.content}", + type="Chat", + type_id=self.room.id, + user=user + ) + + + + class ChatMessageAttachment(models.Model): diff --git a/osinaweb/osinacore/api/__pycache__/urls.cpython-310.pyc b/osinaweb/osinacore/api/__pycache__/urls.cpython-310.pyc index e4ce38ec..df8b4983 100644 Binary files a/osinaweb/osinacore/api/__pycache__/urls.cpython-310.pyc and b/osinaweb/osinacore/api/__pycache__/urls.cpython-310.pyc differ diff --git a/osinaweb/osinacore/api/__pycache__/views.cpython-310.pyc b/osinaweb/osinacore/api/__pycache__/views.cpython-310.pyc index abeae716..bada07e9 100644 Binary files a/osinaweb/osinacore/api/__pycache__/views.cpython-310.pyc and b/osinaweb/osinacore/api/__pycache__/views.cpython-310.pyc differ diff --git a/osinaweb/osinacore/api/urls.py b/osinaweb/osinacore/api/urls.py index ad939256..05fcd334 100644 --- a/osinaweb/osinacore/api/urls.py +++ b/osinaweb/osinacore/api/urls.py @@ -4,5 +4,6 @@ from . import views urlpatterns = [ path('login/', views.login_user), - path('register-device/', views.register_device) + path('register-device/', views.register_device), + path('update-device/', views.update_device, name='update_device'), ] \ No newline at end of file diff --git a/osinaweb/osinacore/api/views.py b/osinaweb/osinacore/api/views.py index 7d63ec93..84556ddf 100644 --- a/osinaweb/osinacore/api/views.py +++ b/osinaweb/osinacore/api/views.py @@ -49,4 +49,24 @@ def register_device(request): device.save() return successRes(msg="Device registered successfully.") except Exception as e: - return errorRes(str(e)) \ No newline at end of file + return errorRes(str(e)) + + + +@api_view(['POST']) +def update_device(request): + try: + registration_token = request.data.get('registration_token') + token_data = verify(request.headers.get("Authorization")) + user_id = token_data['userid'] + user = User.objects.get(id=user_id) + existing_device = FCMDevice.objects.get(registration_id=registration_token) + existing_device.user = user + existing_device.save() + return successRes(msg='Device updated') + except FCMDevice.DoesNotExist: + return errorRes(msg='Device is not registered') + except TokenError as terr: + return errorRes(msg=str(terr), status=450) + except Exception as e: + return errorRes(str(e))