forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 14
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
feat: [AXM-252] add settings for edx-ace push notifications #2541
Merged
monteri
merged 12 commits into
mob-develop
from
Ivan_Niedielnitsev/AXM-252/feature/Prepare-edx-platform-to-use-push-notifications-with-edx-ace
Apr 29, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
09dd70e
feat: [AXM-252] create policy for push notifications
NiedielnitsevIvan 235860e
feat: [AXM-252] add API for store device token
NiedielnitsevIvan 991f014
feat: [AXM-252] add settings for edx-ace push notifications
NiedielnitsevIvan dba1fb1
chore: [AXM-252] add edx-ace and django-push-notification to dev requ…
NiedielnitsevIvan 2915b72
chore: [AXM-252] update edx-ace version
NiedielnitsevIvan 4eb840d
fix: [AXM-252] add create token edndpoint to urls
NiedielnitsevIvan b9708fa
chore: [AXM-252] update django push notifications version
NiedielnitsevIvan 60591a5
style: [AXM-252] fix code style issues after review
NiedielnitsevIvan 294845a
chore: [AXM-252] bump edx-ace version
NiedielnitsevIvan 6e5f4fb
refactor: [AXM-252] some push notif policy refactoring
NiedielnitsevIvan d21cd4a
chore: [AXM-252] change edx-ace branch to mob-develop
NiedielnitsevIvan 81ebaa4
chore: [AXM-252] recompile requirements after rebase
NiedielnitsevIvan 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 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 @@ | ||
from django.urls import path | ||
from .views import GCMDeviceViewSet | ||
|
||
|
||
CREATE_GCM_DEVICE = GCMDeviceViewSet.as_view({'post': 'create'}) | ||
|
||
|
||
urlpatterns = [ | ||
path('create-token/', CREATE_GCM_DEVICE, name='gcmdevice-list'), | ||
] |
This file contains 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,50 @@ | ||
from django.conf import settings | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
|
||
from edx_ace.push_notifications.views import GCMDeviceViewSet as GCMDeviceViewSetBase | ||
|
||
from ..decorators import mobile_view | ||
|
||
|
||
@mobile_view(is_user=True) | ||
class GCMDeviceViewSet(GCMDeviceViewSetBase): | ||
""" | ||
**Use Case** | ||
This endpoint allows clients to register a device for push notifications. | ||
|
||
If the device is already registered, the existing registration will be updated. | ||
If setting PUSH_NOTIFICATIONS_SETTINGS is not configured, the endpoint will return a 501 error. | ||
|
||
**Example Request** | ||
POST /api/mobile/{version}/notifications/create-token/ | ||
**POST Parameters** | ||
The body of the POST request can include the following parameters. | ||
* name (optional) - A name of the device. | ||
* registration_id (required) - The device token of the device. | ||
* device_id (optional) - ANDROID_ID / TelephonyManager.getDeviceId() (always as hex) | ||
* active (optional) - Whether the device is active, default is True. | ||
If False, the device will not receive notifications. | ||
* cloud_message_type (required) - You should choose FCM or GCM. Currently, only FCM is supported. | ||
* application_id (optional) - Opaque application identity, should be filled in for multiple | ||
key/certificate access. | ||
**Example Response** | ||
```json | ||
{ | ||
"id": 1, | ||
"name": "My Device", | ||
"registration_id": "fj3j4", | ||
"device_id": 1234, | ||
"active": true, | ||
"date_created": "2024-04-18T07:39:37.132787Z", | ||
"cloud_message_type": "FCM", | ||
"application_id": "my_app_id" | ||
} | ||
``` | ||
""" | ||
|
||
def create(self, request, *args, **kwargs): | ||
if not getattr(settings, 'PUSH_NOTIFICATIONS_SETTINGS', None): | ||
return Response('Push notifications are not configured.', status.HTTP_501_NOT_IMPLEMENTED) | ||
|
||
return super().create(request, *args, **kwargs) |
This file contains 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 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 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 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,20 @@ | ||
""" | ||
Utility functions for edx-ace. | ||
""" | ||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def setup_firebase_app(firebase_credentials, app_name='fcm-app'): | ||
""" | ||
Returns a Firebase app instance if the Firebase credentials are provided. | ||
""" | ||
try: | ||
import firebase_admin # pylint: disable=import-outside-toplevel | ||
except ImportError: | ||
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. Maybe log here with text: "Could not import firebase_admin package"? 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. Logs added. |
||
log.error('Could not import firebase_admin package.') | ||
return | ||
if firebase_credentials: | ||
certificate = firebase_admin.credentials.Certificate(firebase_credentials) | ||
return firebase_admin.initialize_app(certificate, name=app_name) |
This file contains 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,41 @@ | ||
"""Policies for the notifications app.""" | ||
|
||
from edx_ace.channel import ChannelType | ||
from edx_ace.policy import Policy, PolicyResult | ||
from opaque_keys.edx.keys import CourseKey | ||
|
||
from .models import CourseNotificationPreference | ||
|
||
|
||
class CoursePushNotificationOptout(Policy): | ||
""" | ||
Course Push Notification optOut Policy. | ||
""" | ||
|
||
def check(self, message): | ||
""" | ||
Check if the user has opted out of push notifications for the given course. | ||
:param message: | ||
:return: | ||
""" | ||
course_ids = message.context.get('course_ids', []) | ||
app_label = message.context.get('app_label') | ||
|
||
if not (app_label or message.context.get('send_push_notification', False)): | ||
return PolicyResult(deny={ChannelType.PUSH}) | ||
|
||
course_keys = [CourseKey.from_string(course_id) for course_id in course_ids] | ||
for course_key in course_keys: | ||
course_notification_preference = CourseNotificationPreference.get_user_course_preference( | ||
message.recipient.lms_user_id, | ||
course_key | ||
) | ||
push_notification_preference = course_notification_preference.get_notification_type_config( | ||
app_label, | ||
notification_type='push', | ||
).get('push', False) | ||
|
||
if not push_notification_preference: | ||
return PolicyResult(deny={ChannelType.PUSH}) | ||
|
||
return PolicyResult(deny=frozenset()) |
This file contains 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.
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.
👍