Fixes follow request notifications

Since the main way to interact with them is by approving them in the
notification, I didn't group them
This commit is contained in:
Mouse Reeve 2022-07-05 15:03:20 -07:00
parent e54c563865
commit 7fe722b595
3 changed files with 36 additions and 14 deletions

View file

@ -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,
)

View file

@ -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"""

View file

@ -8,8 +8,10 @@
{% endblock %}
{% block description %}
{% trans "sent you a follow request" %}
{% blocktrans trimmed %}
<a href="{{ related_user_link }}">{{ related_user }}</a> sent you a follow request
{% endblocktrans %}
<div class="row shrink">
{% include 'snippets/follow_request_buttons.html' with user=notification.related_user %}
{% include 'snippets/follow_request_buttons.html' with user=notification.related_users.first %}
</div>
{% endblock %}