diff --git a/bookwyrm/models/notification.py b/bookwyrm/models/notification.py index 330bd5b72..59ba59d6c 100644 --- a/bookwyrm/models/notification.py +++ b/bookwyrm/models/notification.py @@ -3,7 +3,7 @@ from django.db import models, transaction from django.dispatch import receiver from .base_model import BookWyrmModel from . import Boost, Favorite, GroupMemberInvitation, ImportJob, ListItem, Report -from . import Status, User +from . import Status, User, UserFollowRequest class Notification(BookWyrmModel): @@ -257,3 +257,34 @@ def notify_user_on_list_item_add(sender, instance, created, *args, **kwargs): for membership in instance.book_list.group.memberships.all(): if membership.user != instance.user: Notification.notify_list_item(membership.user, instance) + + +@receiver(models.signals.post_save, sender=UserFollowRequest) +@transaction.atomic +# pylint: disable=unused-argument +def notify_user_on_follow(sender, instance, created, *args, **kwargs): + """Someone added to your list""" + if not created or not instance.user_object.local: + return + + manually_approves = instance.user_object.manually_approves_followers + if manually_approves: + # don't group notifications + notification = Notification.objects.filter( + user=instance.user_object, + related_users=instance.user_subject, + notification_type=Notification.FOLLOW_REQUEST, + ).first() + if not notification: + notification = Notification.objects.create( + user=instance.user_object, notification_type=Notification.FOLLOW_REQUEST + ) + notification.related_users.set([instance.user_subject]) + notification.read = False + notification.save() + else: + Notification.notify( + instance.user_object, + instance.user_subject, + notification_type=Notification.FOLLOW, + ) diff --git a/bookwyrm/models/relationship.py b/bookwyrm/models/relationship.py index bc483805b..082294c0e 100644 --- a/bookwyrm/models/relationship.py +++ b/bookwyrm/models/relationship.py @@ -1,5 +1,4 @@ """ defines relationships between users """ -from django.apps import apps from django.core.cache import cache from django.db import models, transaction, IntegrityError from django.db.models import Q @@ -148,16 +147,6 @@ class UserFollowRequest(ActivitypubMixin, UserRelationship): if not manually_approves: self.accept() - model = apps.get_model("bookwyrm.Notification", require_ready=True) - notification_type = ( - model.FOLLOW_REQUEST if manually_approves else model.FOLLOW - ) - model.notify( - self.user_object, - self.user_subject, - notification_type=notification_type, - ) - def get_accept_reject_id(self, status): """get id for sending an accept or reject of a local user""" diff --git a/bookwyrm/templates/notifications/items/follow_request.html b/bookwyrm/templates/notifications/items/follow_request.html index 9cec8116a..804c35282 100644 --- a/bookwyrm/templates/notifications/items/follow_request.html +++ b/bookwyrm/templates/notifications/items/follow_request.html @@ -8,8 +8,10 @@ {% endblock %} {% block description %} - {% trans "sent you a follow request" %} + {% blocktrans trimmed %} + {{ related_user }} sent you a follow request + {% endblocktrans %}
- {% include 'snippets/follow_request_buttons.html' with user=notification.related_user %} + {% include 'snippets/follow_request_buttons.html' with user=notification.related_users.first %}
{% endblock %}