fix: make updates to notification related events

This commit is contained in:
SaadYousaf
2023-09-08 13:44:03 +05:00
committed by Saad Yousaf
parent 175fb0caf8
commit d4fbc3037c
4 changed files with 26 additions and 9 deletions

View File

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

View File

@@ -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):

View File

@@ -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

View File

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