Skip to content

Commit

Permalink
notifications: add user recipient and user email backend
Browse files Browse the repository at this point in the history
  • Loading branch information
rekt-hard committed Apr 27, 2023
1 parent 189315d commit a0de8de
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
41 changes: 37 additions & 4 deletions invenio_users_resources/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}>",
# }
# )
49 changes: 49 additions & 0 deletions tests/test_notifications.py
Original file line number Diff line number Diff line change
@@ -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"]

0 comments on commit a0de8de

Please sign in to comment.