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)
default_web_config = get_default_values_of_preference(app_name, notification_type).get('web', False)
generated_notification_audience = []
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
preferences = CourseNotificationPreference.objects.filter(
user_id__in=batch_user_ids,
@@ -115,32 +121,26 @@ def send_notifications(user_ids, course_key: str, app_name, notification_type, c
if not preferences:
continue
notifications = []
for preference in preferences:
preference = update_user_preference(preference, preference.user_id, course_key)
if not (
user_id = preference.user_id
preference = update_user_preference(preference, user_id, course_key)
if (
preference and
preference.get_web_config(app_name, notification_type) and
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(
Notification(
user_id=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=user_id,
app_name=app_name,
notification_type=notification_type,
content_context=context,
content_url=content_url,
course_id=course_key,
)
)
)
generated_notification_audience.append(user_id)
generated_notification_audience.append(user_id)
# send notification to users but use bulk_create
notification_objects = Notification.objects.bulk_create(notifications)
@@ -149,6 +149,8 @@ def send_notifications(user_ids, course_key: str, app_name, notification_type, c
notification_content = notification_objects[0].content
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(
generated_notification_audience, app_name, notification_type, course_key, content_url,
notification_content, sender_id=sender_id