-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop' into async
- Loading branch information
Showing
9 changed files
with
205 additions
and
84 deletions.
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
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
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,43 @@ | ||
from typing import Optional, TypedDict | ||
from superdesk.types import User | ||
|
||
|
||
class NotificationPreferences(TypedDict): | ||
email: bool | ||
desktop: bool | ||
|
||
|
||
def get_user_notification_preferences(user: User, notification: Optional[str] = None) -> NotificationPreferences: | ||
user_preferences = user.get("user_preferences") or {} | ||
|
||
def is_enabled(preference: str) -> bool: | ||
return bool(user_preferences.get(preference, {}).get("enabled", False)) | ||
|
||
email_enabled = is_enabled("email:notification") | ||
desktop_enabled = is_enabled("desktop:notification") | ||
|
||
if notification is None: | ||
return NotificationPreferences( | ||
email=email_enabled, | ||
desktop=desktop_enabled, | ||
) | ||
|
||
notification_preferences = user_preferences.get("notifications", {}).get(notification) | ||
if notification_preferences is None: | ||
# BC: Check for the old email:notification:<notification> preference | ||
email_notification_name = f"email:notification:{notification}" | ||
if user_preferences.get(email_notification_name): | ||
return NotificationPreferences( | ||
email=email_enabled and is_enabled(email_notification_name), | ||
desktop=desktop_enabled, | ||
) | ||
else: | ||
return NotificationPreferences( | ||
email=email_enabled, | ||
desktop=desktop_enabled, | ||
) | ||
|
||
return NotificationPreferences( | ||
email=email_enabled and notification_preferences.get("email") is not False, | ||
desktop=desktop_enabled and notification_preferences.get("desktop") is not False, | ||
) |
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 |
---|---|---|
|
@@ -8,22 +8,10 @@ | |
# AUTHORS and LICENSE files distributed with this source code, or | ||
# at https://www.sourcefabric.org/superdesk/license | ||
|
||
from unittest import mock | ||
|
||
from superdesk.activity import ActivityService, ACTIVITY_ERROR, get_recipients | ||
from superdesk.activity import ActivityService, get_recipients | ||
from superdesk.publish import init_app | ||
from superdesk.tests import TestCase | ||
|
||
|
||
def mock_get_resource_service(resource_name): | ||
return MockPreferenceService() | ||
|
||
|
||
class MockPreferenceService: | ||
def email_notification_is_enabled(self, preferences=None): | ||
send_email = preferences.get("email:notification", {}) if isinstance(preferences, dict) else {} | ||
|
||
return send_email and send_email.get("enabled", False) | ||
from superdesk.types import User | ||
|
||
|
||
class ActivityTestCase(TestCase): | ||
|
@@ -44,24 +32,51 @@ def test_is_read(self): | |
self.assertFalse(ActivityService().is_read(activity, "2")) | ||
self.assertFalse(ActivityService().is_read(activity, "3")) | ||
|
||
@mock.patch("superdesk.activity.get_resource_service", mock_get_resource_service) | ||
def test_get_recipients_filters_out_users_not_activated(self): | ||
users = [ | ||
{ | ||
"email": "[email protected]", | ||
"needs_activation": False, | ||
"is_enabled": True, | ||
"is_active": True, | ||
"user_preferences": {"email:notification": {"enabled": True}}, | ||
}, | ||
{ | ||
"email": "[email protected]", | ||
"needs_activation": True, | ||
"is_enabled": True, | ||
"is_active": True, | ||
"user_preferences": {"email:notification": {"enabled": True}}, | ||
}, | ||
User( | ||
{ | ||
"username": "test1", | ||
"email": "[email protected]", | ||
"needs_activation": False, | ||
"is_enabled": True, | ||
"is_active": True, | ||
"user_preferences": {"email:notification": {"enabled": True}}, | ||
} | ||
), | ||
User( | ||
{ | ||
"username": "test2", | ||
"email": "[email protected]", | ||
"needs_activation": True, | ||
"is_enabled": True, | ||
"is_active": True, | ||
"user_preferences": {"email:notification": {"enabled": True}}, | ||
} | ||
), | ||
] | ||
|
||
recipients = get_recipients(user_list=users, activity_name=ACTIVITY_ERROR) | ||
recipients = get_recipients(user_list=users) | ||
self.assertEqual(len(recipients), 1) | ||
self.assertEqual(recipients[0], "[email protected]") | ||
|
||
def test_get_recipients_filters_out_users_with_disabled_notification(self): | ||
users = [ | ||
User( | ||
{ | ||
"username": "test1", | ||
"email": "[email protected]", | ||
"needs_activation": False, | ||
"is_enabled": True, | ||
"is_active": True, | ||
"user_preferences": {"email:notification": {"enabled": False}}, | ||
} | ||
), | ||
] | ||
|
||
recipients = get_recipients(user_list=users, notification_name="test") | ||
assert len(recipients) == 0 | ||
|
||
users[0]["user_preferences"]["email:notification"]["enabled"] = True | ||
recipients = get_recipients(user_list=users, notification_name="test") | ||
assert len(recipients) == 1 |
Oops, something went wrong.