From d4fbc3037c776592b5088a58591ea0fe714741df Mon Sep 17 00:00:00 2001 From: SaadYousaf Date: Fri, 8 Sep 2023 13:44:03 +0500 Subject: [PATCH] fix: make updates to notification related events --- .../core/djangoapps/notifications/events.py | 19 ++++++++++++++----- .../core/djangoapps/notifications/tasks.py | 8 ++++++-- .../notifications/tests/test_views.py | 2 ++ .../core/djangoapps/notifications/views.py | 6 ++++-- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/openedx/core/djangoapps/notifications/events.py b/openedx/core/djangoapps/notifications/events.py index 64fc72b531..61db83d0e7 100644 --- a/openedx/core/djangoapps/notifications/events.py +++ b/openedx/core/djangoapps/notifications/events.py @@ -62,16 +62,22 @@ def notification_preferences_viewed_event(request, course_id): ) -def notification_generated_event(user_ids, app_name, notification_type, course_key): +def notification_generated_event(user_ids, app_name, notification_type, course_key, content_url, content): """ Emit an event when a notification is generated. """ context = contexts.course_context_from_course_id(course_key) + context['user_id'] = 'None' + recipients_count = len(user_ids) event_data = { - 'recipients_id': user_ids, + 'recipients_id': user_ids[:100], + 'recipients_count': recipients_count, + 'recipients_truncated': recipients_count > 100, 'course_id': str(course_key), 'notification_type': notification_type, 'notification_app': app_name, + 'content_url': content_url, + 'notification_content': content, } with tracker.get_tracker().context(NOTIFICATION_GENERATED, context): tracker.emit( @@ -85,15 +91,17 @@ def notification_generated_event(user_ids, app_name, notification_type, course_k ) -def notification_read_event(user, notification): +def notification_read_event(user, notification, first_read=False): """ Emit an event when a notification app is marked read for a user. """ context = contexts.course_context_from_course_id(notification.course_id) + event_data = notification_event_context(user, notification.course_id, notification) + event_data['first_read'] = first_read with tracker.get_tracker().context(NOTIFICATION_READ, context): tracker.emit( NOTIFICATION_READ, - notification_event_context(user, notification.course_id, notification) + event_data, ) @@ -131,7 +139,7 @@ def notification_preference_update_event(user, course_id, updated_preference): ) -def notification_tray_opened_event(user): +def notification_tray_opened_event(user, unseen_notifications_count): """ Emit an event when a notification tray is opened. """ @@ -139,5 +147,6 @@ def notification_tray_opened_event(user): NOTIFICATION_TRAY_OPENED, { 'user_id': user.id, + 'unseen_notifications_count': unseen_notifications_count, } ) diff --git a/openedx/core/djangoapps/notifications/tasks.py b/openedx/core/djangoapps/notifications/tasks.py index bed059ee12..d2c495ff0c 100644 --- a/openedx/core/djangoapps/notifications/tasks.py +++ b/openedx/core/djangoapps/notifications/tasks.py @@ -116,8 +116,12 @@ def send_notifications(user_ids, course_key: str, app_name, notification_type, c ) ) # send notification to users but use bulk_create - Notification.objects.bulk_create(notifications) - notification_generated_event(user_ids, app_name, notification_type, course_key) + notifications_generated = Notification.objects.bulk_create(notifications) + if notifications_generated: + notification_content = notifications_generated[0].content + notification_generated_event( + user_ids, app_name, notification_type, course_key, content_url, notification_content, + ) def update_user_preference(preference: CourseNotificationPreference, user, course_id): diff --git a/openedx/core/djangoapps/notifications/tests/test_views.py b/openedx/core/djangoapps/notifications/tests/test_views.py index 4a0855e689..d423eb7dfe 100644 --- a/openedx/core/djangoapps/notifications/tests/test_views.py +++ b/openedx/core/djangoapps/notifications/tests/test_views.py @@ -407,6 +407,7 @@ class NotificationListAPIViewTest(APITestCase): event_name, event_data = mock_emit.call_args[0] self.assertEqual(event_name, 'edx.notifications.tray_opened') self.assertEqual(event_data['user_id'], self.user.id) + self.assertEqual(event_data['unseen_notifications_count'], 0) def test_list_notifications_without_authentication(self): """ @@ -652,6 +653,7 @@ class NotificationReadAPIViewTestCase(APITestCase): self.assertEqual(event_data.get('notification_metadata').get('notification_id'), notification_id) self.assertEqual(event_data['notification_app'], 'discussion') self.assertEqual(event_data['notification_type'], 'Type A') + self.assertEqual(event_data['first_read'], True) def test_mark_notification_read_with_other_user_notification_id(self): # Create a PATCH request to mark notification as read for notification_id: 2 through a different user diff --git a/openedx/core/djangoapps/notifications/views.py b/openedx/core/djangoapps/notifications/views.py index 17b1b696f5..1b72da965a 100644 --- a/openedx/core/djangoapps/notifications/views.py +++ b/openedx/core/djangoapps/notifications/views.py @@ -263,7 +263,8 @@ class NotificationListAPIView(generics.ListAPIView): app_name = self.request.query_params.get('app_name') if self.request.query_params.get('tray_opened'): - notification_tray_opened_event(self.request.user) + unseen_count = Notification.objects.filter(user_id=self.request.user, last_seen__isnull=True).count() + notification_tray_opened_event(self.request.user, unseen_count) if app_name: return Notification.objects.filter( @@ -400,9 +401,10 @@ class NotificationReadAPIView(APIView): if notification_id: notification = get_object_or_404(Notification, pk=notification_id, user=request.user) + first_time_read = notification.last_read is None notification.last_read = read_at notification.save() - notification_read_event(request.user, notification) + notification_read_event(request.user, notification, first_time_read) return Response({'message': _('Notification marked read.')}, status=status.HTTP_200_OK) app_name = request.data.get('app_name', '')