emile 7 months ago
parent 8eb25d524b
commit 68124b85de

Binary file not shown.

@ -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),
),
]

@ -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),
),
]

@ -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),
),
]

@ -11,7 +11,6 @@ from django.utils.safestring import mark_safe
def send_notification(notification): def send_notification(notification):
try:
notification_data = { notification_data = {
'title': notification.title, 'title': notification.title,
'body': mark_safe(notification.message), 'body': mark_safe(notification.message),
@ -58,47 +57,15 @@ def send_notification(notification):
apns=apns_config apns=apns_config
) )
if notification.user:
FCMDevice.objects.filter(user=notification.user).send_message(message)
else:
FCMDevice.objects.send_message(message) 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): class ChatNotification(models.Model):
TYPES = ( TYPES = (
('Visitor', 'Visitor'), ('Visitor', 'Visitor'),
@ -111,6 +78,7 @@ class ChatNotification(models.Model):
type = models.CharField(max_length=8, choices=TYPES, null=True) type = models.CharField(max_length=8, choices=TYPES, null=True)
type_id = models.IntegerField(null=True) type_id = models.IntegerField(null=True)
session_id = models.CharField(max_length=200, 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): def save(self, *args, **kwargs):
is_new = not self.pk is_new = not self.pk
super().save(*args, **kwargs) super().save(*args, **kwargs)
@ -337,18 +305,35 @@ class ChatMessage(models.Model):
self.send_message_notification() self.send_message_notification()
def send_message_notification(self): def send_message_notification(self):
type_id = self.room.id type_id = self.room.id
chatroom_guest = getattr(self.room, 'chatroomguest', None)
if chatroom_guest:
session_id = self.room.chatroomguest.visitor.session_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( notification = ChatNotification.objects.create(
title=title, title=f"Visitor {self.room.chatroomguest.visitor.ip_address} sent a new message on Ositcom!",
message = body, message = f"{self.content}",
image = self.room.chatroomguest.visitor.notification_flag_image_url, image = self.room.chatroomguest.visitor.notification_flag_image_url,
type = "Chat", type = "Chat",
type_id = type_id, type_id = type_id,
session_id = session_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): class ChatMessageAttachment(models.Model):
message = models.OneToOneField(ChatMessage, on_delete=models.CASCADE) message = models.OneToOneField(ChatMessage, on_delete=models.CASCADE)

@ -4,5 +4,6 @@ from . import views
urlpatterns = [ urlpatterns = [
path('login/', views.login_user), 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'),
] ]

@ -50,3 +50,23 @@ def register_device(request):
return successRes(msg="Device registered successfully.") return successRes(msg="Device registered successfully.")
except Exception as e: except Exception as e:
return errorRes(str(e)) 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))

Loading…
Cancel
Save