From 376135ca7366e98fd87808edd6bfec0874756ad2 Mon Sep 17 00:00:00 2001 From: SaadYousaf Date: Fri, 18 Aug 2023 13:45:07 +0500 Subject: [PATCH] fix: fix notification generated event, and add notification tray opened event --- .../core/djangoapps/notifications/events.py | 34 ++++++++++++++++--- .../core/djangoapps/notifications/handlers.py | 2 ++ .../core/djangoapps/notifications/tasks.py | 19 +++++------ .../notifications/tests/test_views.py | 19 ++++++++++- .../core/djangoapps/notifications/views.py | 5 ++- 5 files changed, 62 insertions(+), 17 deletions(-) diff --git a/openedx/core/djangoapps/notifications/events.py b/openedx/core/djangoapps/notifications/events.py index c86801a4a8eb..8c9e45ee001a 100644 --- a/openedx/core/djangoapps/notifications/events.py +++ b/openedx/core/djangoapps/notifications/events.py @@ -2,13 +2,14 @@ from eventtracking import tracker -from common.djangoapps.track import contexts +from common.djangoapps.track import contexts, segment NOTIFICATION_PREFERENCES_VIEWED = 'edx.notifications.preferences.viewed' NOTIFICATION_GENERATED = 'edx.notifications.generated' -NOTIFICATION_READ = 'edx.notification.read' +NOTIFICATION_READ = 'edx.notifications.read' NOTIFICATION_APP_ALL_READ = 'edx.notifications.app_all_read' NOTIFICATION_PREFERENCES_UPDATED = 'edx.notifications.preferences.updated' +NOTIFICATION_TRAY_OPENED = 'edx.notifications.tray_opened' def get_user_forums_roles(user, course_id): @@ -61,15 +62,26 @@ def notification_preferences_viewed_event(request, course_id): ) -def notification_generated_event(user, notification): +def notification_generated_event(notification_data): """ Emit an event when a notification is generated. """ - context = contexts.course_context_from_course_id(notification.course_id) + context = contexts.course_context_from_course_id(notification_data.get('course_key', '')) + event_data = { + 'recipients_id': notification_data.get('user_ids', []), + 'course_id': notification_data.get('course_key', ''), + 'notification_type': notification_data.get('notification_type', ''), + 'notification_app': notification_data.get('notification_app', ''), + } with tracker.get_tracker().context(NOTIFICATION_GENERATED, context): tracker.emit( NOTIFICATION_GENERATED, - notification_event_context(user, notification.course_id, notification) + event_data, + ) + segment.track( + 'None', + NOTIFICATION_GENERATED, + event_data, ) @@ -117,3 +129,15 @@ def notification_preference_update_event(user, course_id, updated_preference): 'value': updated_preference.get('value', ''), } ) + + +def notification_tray_opened_event(user): + """ + Emit an event when a notification tray is opened. + """ + tracker.emit( + NOTIFICATION_TRAY_OPENED, + { + 'user_id': user.id, + } + ) diff --git a/openedx/core/djangoapps/notifications/handlers.py b/openedx/core/djangoapps/notifications/handlers.py index 7d37bbe7d6fe..91b76f941dec 100644 --- a/openedx/core/djangoapps/notifications/handlers.py +++ b/openedx/core/djangoapps/notifications/handlers.py @@ -13,6 +13,7 @@ ) from openedx.core.djangoapps.notifications.config.waffle import ENABLE_NOTIFICATIONS +from openedx.core.djangoapps.notifications.events import notification_generated_event from openedx.core.djangoapps.notifications.models import CourseNotificationPreference log = logging.getLogger(__name__) @@ -58,3 +59,4 @@ def generate_user_notifications(signal, sender, notification_data, metadata, **k notification_data = notification_data.__dict__ notification_data['course_key'] = str(notification_data['course_key']) send_notifications.delay(**notification_data) + notification_generated_event(notification_data) diff --git a/openedx/core/djangoapps/notifications/tasks.py b/openedx/core/djangoapps/notifications/tasks.py index ca5bdabbdd7d..9a4f502b8f11 100644 --- a/openedx/core/djangoapps/notifications/tasks.py +++ b/openedx/core/djangoapps/notifications/tasks.py @@ -19,7 +19,6 @@ Notification, get_course_notification_preference_config_version ) -from openedx.core.djangoapps.notifications.events import notification_generated_event logger = get_task_logger(__name__) @@ -105,16 +104,16 @@ def send_notifications(user_ids, course_key: str, app_name, notification_type, c preference.get_web_config(app_name, notification_type) and preference.get_app_config(app_name).get('enabled', False) ): - notification = Notification( - user_id=preference.user_id, - app_name=app_name, - notification_type=notification_type, - content_context=context, - content_url=content_url, - course_id=course_key, + notifications.append( + Notification( + user_id=preference.user_id, + app_name=app_name, + notification_type=notification_type, + content_context=context, + content_url=content_url, + course_id=course_key, + ) ) - notifications.append(notification) - notification_generated_event(preference.user, notification) # send notification to users but use bulk_create Notification.objects.bulk_create(notifications) diff --git a/openedx/core/djangoapps/notifications/tests/test_views.py b/openedx/core/djangoapps/notifications/tests/test_views.py index 1685ec385b7e..1cc0cab1a116 100644 --- a/openedx/core/djangoapps/notifications/tests/test_views.py +++ b/openedx/core/djangoapps/notifications/tests/test_views.py @@ -391,6 +391,23 @@ def test_list_notifications_with_app_name_filter(self): '

test_user responded to your post This is a test post.

' ) + @mock.patch("eventtracking.tracker.emit") + def test_list_notifications_with_tray_opened_param(self, mock_emit): + """ + Test event emission with tray_opened param is provided. + """ + self.client.login(username=self.user.username, password='test') + + # Make a request to the view with the tray_opened query parameter set to True. + response = self.client.get(self.url + "?tray_opened=True") + + # Assert that the response is successful. + self.assertEqual(response.status_code, 200) + + 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) + def test_list_notifications_without_authentication(self): """ Test that the view returns 403 if the user is not authenticated. @@ -621,7 +638,7 @@ def test_mark_notification_read_with_notification_id(self, mock_emit): notifications = Notification.objects.filter(user=self.user, id=notification_id, last_read__isnull=False) self.assertEqual(notifications.count(), 1) event_name, event_data = mock_emit.call_args[0] - self.assertEqual(event_name, 'edx.notification.read') + self.assertEqual(event_name, 'edx.notifications.read') 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') diff --git a/openedx/core/djangoapps/notifications/views.py b/openedx/core/djangoapps/notifications/views.py index d8e579635f86..8614bb49f66b 100644 --- a/openedx/core/djangoapps/notifications/views.py +++ b/openedx/core/djangoapps/notifications/views.py @@ -26,7 +26,7 @@ notification_preference_update_event, notification_preferences_viewed_event, notification_read_event, - notifications_app_all_read_event, + notifications_app_all_read_event, notification_tray_opened_event, ) from .models import Notification from .serializers import ( @@ -262,6 +262,9 @@ def get_queryset(self): expiry_date = datetime.now(UTC) - timedelta(days=settings.NOTIFICATIONS_EXPIRY) app_name = self.request.query_params.get('app_name') + if self.request.query_params.get('tray_opened'): + notification_tray_opened_event(self.request.user) + if app_name: return Notification.objects.filter( user=self.request.user,