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

Add a filter_include option for the notify function? #135

Open
benjaoming opened this issue Dec 15, 2023 · 0 comments
Open

Add a filter_include option for the notify function? #135

benjaoming opened this issue Dec 15, 2023 · 0 comments

Comments

@benjaoming
Copy link
Member

It's not possible to frame all logic within filter_exclude and therefore, we should probably introduce a companion.

These filters are quite important for systems that need extra guarantees. There are cases where the set of notified users can fluctuate and inaccuracies can produce small leaks of information to the wrong users. This could be for instance if the consent is stored in another database table that needs to be queried.

Another way to use these filters is to have a dynamic set of subscribers. I.e. you can express "anyone who is currently a superuser" through filter_include={"settings__user__is_superuser": True}

def notify(
message: str,
key: str,
target_object: Any = None,
url: str = None,
filter_exclude: dict = None,
recipient_users: list = None,
) -> List[models.Notification]:
"""
Notify subscribing users of a new event. Key can be any kind of string,
just make sure to reuse it where applicable.
Here is the most basic example: Everyone subscribing to the `"new_comments"` key
are sent a notification with the message "New comment posted"::
notify("New comment posted", "new_comments")
Here is an example that will create a Notification object for everyone who
has a subscription for the key `"comment/response"` and the model instance `comment_instance`.
The idea would be that the poster of `comment_instance` will receive notifications when someone responds to that comment.
.. code-block:: python
notify(
"there was a response to your comment",
"comment/response",
target_object=comment_instance,
url=reverse('comments:view', args=(comment_instance.id,))
)
:param message: A string containing the message that should be sent to all subscribed users.
:param key: A key object which is matched to ``NotificationType.key``.
Users with a Subscription for that NotificationType will have a Notification object created.
:param url: A URL pointing to your notification.
If the URL is pointing to your Django project's unique website,
then add an ``/absolute/url``. However, if the URL should take the user to a different website,
use a full HTTP scheme, i.e. ``https://example.org/url/``.
:param recipient_users: A possible iterable of users that should be notified
instead of notifying all subscribers of the event.
Notice that users still have to be actually subscribed
to the event key!
:param target_object: Any Django model instance that this notification
relates to. Uses Django content types.
Subscriptions with a matching content_type and object_id will be notified.
:param filter_exclude: Keyword arguments passed to filter out Subscriptions.
Will be handed to ``Subscription.objects.exclude(**filter_exclude)``.
"""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant