Skip to content

Commit

Permalink
fix: fix notification generated event, and add notification tray open…
Browse files Browse the repository at this point in the history
…ed event
  • Loading branch information
SaadYousaf authored and saadyousafarbi committed Sep 1, 2023
1 parent c75c706 commit 376135c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
34 changes: 29 additions & 5 deletions openedx/core/djangoapps/notifications/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
)


Expand Down Expand Up @@ -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,
}
)
2 changes: 2 additions & 0 deletions openedx/core/djangoapps/notifications/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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)
19 changes: 9 additions & 10 deletions openedx/core/djangoapps/notifications/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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)

Expand Down
19 changes: 18 additions & 1 deletion openedx/core/djangoapps/notifications/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,23 @@ def test_list_notifications_with_app_name_filter(self):
'<p><strong>test_user</strong> responded to your post <strong>This is a test post.</strong></p>'
)

@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.
Expand Down Expand Up @@ -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')
Expand Down
5 changes: 4 additions & 1 deletion openedx/core/djangoapps/notifications/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 376135c

Please sign in to comment.