fix: fix notification generated event, and add notification tray opened event
This commit is contained in:
@@ -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,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -13,6 +13,7 @@ from openedx_events.learning.signals import (
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
@@ -19,7 +19,6 @@ from openedx.core.djangoapps.notifications.models import (
|
||||
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)
|
||||
|
||||
|
||||
@@ -391,6 +391,23 @@ class NotificationListAPIViewTest(APITestCase):
|
||||
'<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.
|
||||
@@ -621,7 +638,7 @@ class NotificationReadAPIViewTestCase(APITestCase):
|
||||
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')
|
||||
|
||||
@@ -26,7 +26,7 @@ from .events import (
|
||||
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 @@ class NotificationListAPIView(generics.ListAPIView):
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user