diff --git a/invenio_users_resources/notifications.py b/invenio_users_resources/notifications.py index 7e048a2..d14054c 100644 --- a/invenio_users_resources/notifications.py +++ b/invenio_users_resources/notifications.py @@ -9,19 +9,52 @@ """User specific resources for notifications.""" -from invenio_records_resources.notifications import RecipientFilter +from invenio_accounts.models import User +from invenio_notifications.models import Recipient +from invenio_notifications.services.filters import RecipientFilter class UserPreferencesRecipientFilter(RecipientFilter): """Recipient filter for notifications being enabled at all.""" - def run(self, recipients, **kwargs): + @classmethod + def __call___(cls, notification, recipients): """Filter recipients.""" - return [ + recipients = [ r for r in recipients - if r.get("user", {}) + if r.get("data", {}) .get("preferences", {}) .get("notifications", {}) .get("enabled", False) ] + return recipients + + +class UserRecipient: + def __init__(self, key): + """Ctor.""" + self.key = key + + def __call__(self, notification, recipients): + """Update required recipient information and add backend id.""" + user = notification[self.key] + if isinstance(user, User): + if user.preferences["notifications"]["enabled"]: + recipients[user.id] = Recipient(data=user.dump()) + else: + recipients[user.get("id")] = Recipient(data=user) + return recipients + + +class UserEmailBackend: + def __call__(self, notification, recipient): + """Update required recipient information and add backend id.""" + return "email" + # user = recipient.data + # rec.backends.append( + # { + # "backend": "email", + # "to": f"{user.profile.full_name} <{user.email}>", + # } + # ) diff --git a/tests/test_notifications.py b/tests/test_notifications.py new file mode 100644 index 0000000..5a06789 --- /dev/null +++ b/tests/test_notifications.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2023 Graz University of Technology. +# +# Invenio-Users-Resources is free software; you can redistribute it and/or +# modify it under the terms of the MIT License; see LICENSE file for more +# details. + +"""Notification related tests.""" + +from invenio_users_resources.notifications import ( + UserPreferencesRecipientFilter, +) +from invenio_users_resources.records.api import UserAggregate + + +# def test_user_recipient_filter(user_pub): +# """Test user recipient filter for notifications.""" +# preferences_filter = UserPreferencesRecipientFilter + +# u = UserAggregate.from_user(user_pub.user).dumps() + +# user_notifications_enabled = u.copy() +# user_notifications_enabled["preferences"]["notifications"] = { +# "enabled": True +# } +# user_notifications_disabled = u.copy() +# user_notifications_disabled["preferences"]["notifications"] = { +# "enabled": False +# } + +# recipient_enabled = { +# "user": user_notifications_enabled, +# "backends": [], +# } +# recipient_disabled = { +# "user": user_notifications_disabled, +# "backends": [], +# } + +# filtered_users = preferences_filter.run( +# [ +# recipient_disabled, +# recipient_enabled, +# ] +# ) + +# assert 1 == len(filtered_users) +# assert recipient_enabled == filtered_users[0]["user"]