From 42dc3e6a39c68d2c39ae66d3b6c8e4d93d0762da Mon Sep 17 00:00:00 2001 From: muhammadadeeltajamul Date: Mon, 16 Dec 2024 13:50:13 +0500 Subject: [PATCH] fix: new discussion post should only be grouped if old notification is not seen --- .../notifications/grouping_notifications.py | 6 ++- .../tests/test_notification_grouping.py | 37 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/openedx/core/djangoapps/notifications/grouping_notifications.py b/openedx/core/djangoapps/notifications/grouping_notifications.py index 3c4688b5ed53..570b2f6bfc98 100644 --- a/openedx/core/djangoapps/notifications/grouping_notifications.py +++ b/openedx/core/djangoapps/notifications/grouping_notifications.py @@ -128,11 +128,15 @@ def get_user_existing_notifications(user_ids, notification_type, group_by_id, co """ Returns user last group able notification """ + notification_type_params = { + 'new_discussion_post': {'last_seen__isnull': True}, + } notifications = Notification.objects.filter( user__in=user_ids, notification_type=notification_type, group_by_id=group_by_id, - course_id=course_id + course_id=course_id, + **notification_type_params.get(notification_type, {}) ) notifications_mapping = {user_id: [] for user_id in user_ids} for notification in notifications: diff --git a/openedx/core/djangoapps/notifications/tests/test_notification_grouping.py b/openedx/core/djangoapps/notifications/tests/test_notification_grouping.py index fea954a0eabb..81c68661e26b 100644 --- a/openedx/core/djangoapps/notifications/tests/test_notification_grouping.py +++ b/openedx/core/djangoapps/notifications/tests/test_notification_grouping.py @@ -2,11 +2,13 @@ Tests for notification grouping module """ +import ddt import unittest from unittest.mock import MagicMock, patch from datetime import datetime from pytz import utc +from common.djangoapps.student.tests.factories import UserFactory from openedx.core.djangoapps.notifications.grouping_notifications import ( BaseNotificationGrouper, NotificationRegistry, @@ -15,6 +17,8 @@ get_user_existing_notifications, NewPostGrouper ) from openedx.core.djangoapps.notifications.models import Notification +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase +from xmodule.modulestore.tests.factories import CourseFactory class TestNotificationRegistry(unittest.TestCase): @@ -102,7 +106,8 @@ def test_group_email_content(self): self.assertEqual(content_context['email_content'], 'new content') -class TestNewPostGrouper(unittest.TestCase): +@ddt.ddt +class TestNewPostGrouper(ModuleStoreTestCase): """ Tests for the NewPostGrouper class """ @@ -142,6 +147,36 @@ def test_new_post_with_same_user(self): self.assertFalse(updated_context.get('grouped', False)) + @ddt.data(datetime(2023, 1, 1, tzinfo=utc), None) + def test_not_grouped_when_notification_is_seen(self, last_seen): + """ + Notification is not grouped if the notification is marked as seen + """ + course = CourseFactory() + user = UserFactory() + notification_params = { + 'app_name': 'discussion', + 'notification_type': 'new_discussion_post', + 'course_id': course.id, + 'group_by_id': course.id, + 'content_url': 'http://example.com', + 'user': user, + 'last_seen': last_seen, + } + Notification.objects.create(content_context={ + 'username': 'User1', + 'post_title': ' Post title', + 'replier_name': 'User 1', + + }, **notification_params) + existing_notifications = get_user_existing_notifications( + [user.id], 'new_discussion_post', course.id, course.id + ) + if last_seen is None: + assert existing_notifications[user.id] is not None + else: + assert existing_notifications[user.id] is None + class TestGroupUserNotifications(unittest.TestCase): """