Skip to content
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

Fix: Marked for user desktop notifications [SDBELGA-818] #2645

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions apps/archive/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ def handle_mark_user_notifications(self, updates, original, add_activity=True):
:param original: original item version before update
:param add_activity: flag to decide whether to add notification as activity or not
"""

marked_user = marked_for_user = None
orig_marked_user = original.get("marked_for_user", None)
new_marked_user = updates.get("marked_for_user", None)
Expand Down Expand Up @@ -1275,15 +1276,6 @@ def _send_mark_user_notifications(
:param data: kwargs
"""

# No notification sent if user is not enabled mark for user notification
users_with_enabled_notifications = [
user
for user in user_list
if superdesk.get_resource_service("preferences").mark_for_user_notification_is_enabled(
user_id=user.get("_id")
)
]

if item.get("type") == "text":
link_id = item.get("guid", item.get("_id"))
else:
Expand All @@ -1301,8 +1293,9 @@ def _send_mark_user_notifications(
msg,
resource=resource,
item=item,
user_list=users_with_enabled_notifications,
user_list=user_list,
link=link,
preference_notification_name="mark_for_user",
**data,
)
# send separate notification for markForUser extension
Expand Down
21 changes: 8 additions & 13 deletions apps/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class PreferencesResource(Resource):
)

superdesk.register_default_user_preference(
"mark_for_user:notification",
"email:notification:mark_for_user",
{
"type": "bool",
"enabled": True,
Expand Down Expand Up @@ -412,20 +412,15 @@ def has_missing_privileges(prefs):

doc[_user_preferences_key] = {k: v for k, v in preferences.items() if not has_missing_privileges(v)}

def assignment_notification_is_enabled(self, user_id=None, preferences=None):
def check_preference_email_notification_is_enabled(self, preference_name, user_id=None, preferences=None):
"""
This function checks if email notification is enabled or not based on the preferences.
"""
if user_id:
preferences = self.get_user_preference(user_id)
send_email = preferences.get("assignment:notification", {}) if isinstance(preferences, dict) else {}
return send_email and send_email.get("enabled", False)

def mark_for_user_notification_is_enabled(self, user_id=None, preferences=None):
"""
This function checks if email notification is enabled or not based on the preferences.
This function checks if email notification is enabled or not based on the preference.
"""
send_email = {}
if user_id:
preferences = self.get_user_preference(user_id)
send_email = preferences.get("mark_for_user:notification", {}) if isinstance(preferences, dict) else {}
if preference_name:
send_email = (
preferences.get(f"email:notification:{preference_name}", {}) if isinstance(preferences, dict) else {}
)
return send_email and send_email.get("enabled", False)
14 changes: 11 additions & 3 deletions superdesk/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ def add_activity(
return activity


def notify_and_add_activity(activity_name, msg, resource=None, item=None, user_list=None, **data):
def notify_and_add_activity(
activity_name, msg, resource=None, item=None, user_list=None, preference_notification_name=None, **data
devketanpro marked this conversation as resolved.
Show resolved Hide resolved
):
"""
Adds the activity and notify enabled and active users via email.
"""
Expand All @@ -243,7 +245,7 @@ def notify_and_add_activity(activity_name, msg, resource=None, item=None, user_l
)

if activity_name == ACTIVITY_ERROR or user_list:
recipients = get_recipients(user_list, activity_name)
recipients = get_recipients(user_list, activity_name, preference_notification_name)

if activity_name != ACTIVITY_ERROR:
current_user = getattr(g, "user", None)
Expand All @@ -260,7 +262,7 @@ def notify_and_add_activity(activity_name, msg, resource=None, item=None, user_l
send_activity_emails(activity=activity, recipients=recipients)


def get_recipients(user_list, activity_name):
def get_recipients(user_list, activity_name, preference_notification_name=None):
if not user_list and activity_name == ACTIVITY_ERROR:
user_list = get_resource_service("users").get_users_by_user_type("administrator")

Expand All @@ -273,6 +275,12 @@ def get_recipients(user_list, activity_name):
and get_resource_service("preferences").email_notification_is_enabled(
preferences=user.get("user_preferences", {})
)
and (
not preference_notification_name
or get_resource_service("preferences").check_preference_email_notification_is_enabled(
preference_notification_name, preferences=user.get("user_preferences", {})
)
)
]

return recipients
Expand Down
Loading