-
Notifications
You must be signed in to change notification settings - Fork 4k
feat: send mobile braze notifications LEARNER-10298 #36272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jawad-khan
wants to merge
8
commits into
master
Choose a base branch
from
jawad/LEARNER-10298
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+206
−25
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
08caae2
feat: send mobile braze notifications
jawad-khan 1f9b031
fix: fixed pylint issues
jawad-khan 9bcd513
feat: Added push_notification flag and preferences
jawad-khan 1b99696
fix: fixed pylint issues
jawad-khan 6e88b38
fix: Moved braze logic to edx-ace
jawad-khan 52289b0
fix: Un delete admin file
jawad-khan 078e4b5
fix: Added review suggestions
jawad-khan 46d1d69
fix: Added review suggestions
jawad-khan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
openedx/core/djangoapps/notifications/migrations/0007_notification_push.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.2.18 on 2025-03-12 22:30 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('notifications', '0006_notification_group_by_id'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='notification', | ||
name='push', | ||
field=models.BooleanField(default=False), | ||
), | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
10 changes: 10 additions & 0 deletions
10
openedx/core/djangoapps/notifications/push/message_type.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" | ||
Push notifications MessageType | ||
""" | ||
from openedx.core.djangoapps.ace_common.message import BaseMessageType | ||
|
||
|
||
class PushNotificationMessageType(BaseMessageType): | ||
""" | ||
Edx-ace MessageType for Push Notifications | ||
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" Tasks for sending notification to ace push channel """ | ||
from celery.utils.log import get_task_logger | ||
from django.conf import settings | ||
from django.contrib.auth import get_user_model | ||
from edx_ace import ace | ||
from edx_ace.recipient import Recipient | ||
|
||
from .message_type import PushNotificationMessageType | ||
|
||
User = get_user_model() | ||
logger = get_task_logger(__name__) | ||
|
||
|
||
def send_ace_msg_to_push_channel(audience_ids, notification_object, sender_id): | ||
""" | ||
Send mobile notifications using ace to push channels. | ||
""" | ||
if not audience_ids: | ||
return | ||
|
||
# We are releasing this feature gradually. For now, it is only tested with the discussion app. | ||
# We might have a list here in the future. | ||
if notification_object.app_name != 'discussion': | ||
return | ||
|
||
notification_type = notification_object.notification_type | ||
|
||
post_data = { | ||
'notification_type': notification_type, | ||
'course_id': str(notification_object.course_id), | ||
'content_url': notification_object.content_url, | ||
**notification_object.content_context | ||
} | ||
emails = list(User.objects.filter(id__in=audience_ids).values_list('email', flat=True)) | ||
context = {'post_data': post_data} | ||
try: | ||
sender = User.objects.get(id=sender_id) | ||
# We don't need recipient since all recipients are in emails dict. | ||
# But PushNotificationMessageType.personalize method requires us a recipient object. | ||
# I have added sender here in case we might need it in future with the context. | ||
recipient = Recipient(sender.id, sender.email) | ||
except User.DoesNotExist: | ||
recipient = None | ||
|
||
message = PushNotificationMessageType( | ||
app_label="notifications", name="push" | ||
).personalize(recipient, 'en', context) | ||
message.options['emails'] = emails | ||
message.options['braze_campaign'] = notification_type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
message.options['skip_disable_user_policy'] = True | ||
|
||
ace.send(message, limit_to_channels=getattr(settings, 'ACE_PUSH_CHANNELS', [])) | ||
logger.info('Sent mobile notification for %s to ace push channel.', notification_type) |
Empty file.
73 changes: 73 additions & 0 deletions
73
openedx/core/djangoapps/notifications/push/tests/test_tasks.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
Tests for push notifications tasks. | ||
""" | ||
from unittest import mock | ||
|
||
from common.djangoapps.student.tests.factories import UserFactory | ||
from openedx.core.djangoapps.notifications.push.tasks import send_ace_msg_to_push_channel | ||
from openedx.core.djangoapps.notifications.tests.utils import create_notification | ||
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase | ||
from xmodule.modulestore.tests.factories import CourseFactory | ||
|
||
|
||
class SendNotificationsTest(ModuleStoreTestCase): | ||
""" | ||
Tests for send_notifications. | ||
""" | ||
|
||
def setUp(self): | ||
""" | ||
Create a course and users for the course. | ||
""" | ||
|
||
super().setUp() | ||
self.user_1 = UserFactory() | ||
self.user_2 = UserFactory() | ||
self.course_1 = CourseFactory.create( | ||
org='testorg', | ||
number='testcourse', | ||
run='testrun' | ||
) | ||
|
||
self.notification = create_notification( | ||
self.user, self.course_1.id, app_name='discussion', notification_type='new_comment' | ||
) | ||
|
||
@mock.patch('openedx.core.djangoapps.notifications.push.tasks.ace.send') | ||
def test_send_ace_msg_success(self, mock_ace_send): | ||
""" Test send_ace_msg_success """ | ||
send_ace_msg_to_push_channel( | ||
[self.user_1.id, self.user_2.id], | ||
self.notification, | ||
sender_id=self.user_1.id | ||
) | ||
|
||
mock_ace_send.assert_called_once() | ||
message_sent = mock_ace_send.call_args[0][0] | ||
assert message_sent.options['emails'] == [self.user_1.email, self.user_2.email] | ||
assert message_sent.options['braze_campaign'] == 'new_comment' | ||
|
||
@mock.patch('openedx.core.djangoapps.notifications.push.tasks.ace.send') | ||
def test_send_ace_msg_no_sender(self, mock_ace_send): | ||
""" Test when sender is not valid """ | ||
send_ace_msg_to_push_channel( | ||
[self.user_1.id, self.user_2.id], | ||
self.notification, | ||
sender_id=999 | ||
) | ||
|
||
mock_ace_send.assert_called_once() | ||
|
||
@mock.patch('openedx.core.djangoapps.notifications.push.tasks.ace.send') | ||
def test_send_ace_msg_empty_audience(self, mock_ace_send): | ||
""" Test send_ace_msg_success with empty audience """ | ||
send_ace_msg_to_push_channel([], self.notification, sender_id=self.user_1.id) | ||
mock_ace_send.assert_not_called() | ||
|
||
@mock.patch('openedx.core.djangoapps.notifications.push.tasks.ace.send') | ||
def test_send_ace_msg_non_discussion_app(self, mock_ace_send): | ||
""" Test send_ace_msg_success with non-discussion app """ | ||
self.notification.app_name = 'ecommerce' | ||
self.notification.save() | ||
send_ace_msg_to_push_channel([1], self.notification, sender_id=self.user_1.id) | ||
mock_ace_send.assert_not_called() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please explain this with a comment.