fix: notification audience was not filtering users with no preference (#33625)

This commit is contained in:
Muhammad Adeel Tajamul
2023-11-01 09:32:35 +05:00
committed by GitHub
parent 2f49af300c
commit 44b62cafe7

View File

@@ -101,7 +101,13 @@ def send_notifications(user_ids, course_key: str, app_name, notification_type, c
sender_id = context.pop('sender_id', None) sender_id = context.pop('sender_id', None)
default_web_config = get_default_values_of_preference(app_name, notification_type).get('web', False) default_web_config = get_default_values_of_preference(app_name, notification_type).get('web', False)
generated_notification_audience = [] generated_notification_audience = []
for batch_user_ids in get_list_in_batches(user_ids, batch_size): for batch_user_ids in get_list_in_batches(user_ids, batch_size):
if ENABLE_NOTIFICATIONS_FILTERS.is_enabled(course_key):
logger.info(f'Sending notifications to {len(batch_user_ids)} users in {course_key}')
batch_user_ids = NotificationFilter().apply_filters(batch_user_ids, course_key, notification_type)
logger.info(f'After applying filters, sending notifications to {len(batch_user_ids)} users in {course_key}')
# check if what is preferences of user and make decision to send notification or not # check if what is preferences of user and make decision to send notification or not
preferences = CourseNotificationPreference.objects.filter( preferences = CourseNotificationPreference.objects.filter(
user_id__in=batch_user_ids, user_id__in=batch_user_ids,
@@ -115,21 +121,15 @@ def send_notifications(user_ids, course_key: str, app_name, notification_type, c
if not preferences: if not preferences:
continue continue
notifications = []
for preference in preferences: for preference in preferences:
preference = update_user_preference(preference, preference.user_id, course_key) user_id = preference.user_id
if not ( preference = update_user_preference(preference, user_id, course_key)
if (
preference and preference and
preference.get_web_config(app_name, notification_type) and preference.get_web_config(app_name, notification_type) and
preference.get_app_config(app_name).get('enabled', False) preference.get_app_config(app_name).get('enabled', False)
): ):
batch_user_ids.remove(preference.user_id)
if ENABLE_NOTIFICATIONS_FILTERS.is_enabled(course_key):
logger.info(f'Sending notifications to {len(batch_user_ids)} users.')
batch_user_ids = NotificationFilter().apply_filters(batch_user_ids, course_key, notification_type)
logger.info(f'After applying filters, sending notifications to {len(batch_user_ids)} users.')
notifications = []
for user_id in batch_user_ids:
notifications.append( notifications.append(
Notification( Notification(
user_id=user_id, user_id=user_id,
@@ -149,6 +149,8 @@ def send_notifications(user_ids, course_key: str, app_name, notification_type, c
notification_content = notification_objects[0].content notification_content = notification_objects[0].content
if notifications_generated: if notifications_generated:
logger.info(f'Temp: Notifications generated for {len(generated_notification_audience)} out of '
f'{len(user_ids)} users - {app_name} - {notification_type} - {course_key}.')
notification_generated_event( notification_generated_event(
generated_notification_audience, app_name, notification_type, course_key, content_url, generated_notification_audience, app_name, notification_type, course_key, content_url,
notification_content, sender_id=sender_id notification_content, sender_id=sender_id