Merge pull request #2208 from bookwyrm-social/follow-bug

Fixes bug in notifications breaking follows
This commit is contained in:
Mouse Reeve 2022-07-09 12:40:38 -07:00 committed by GitHub
commit 27e9eced67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 5 deletions

View file

@ -71,7 +71,9 @@ class Notification(BookWyrmModel):
"""Create a notification"""
if related_user and (not user.local or user == related_user):
return
notification, _ = cls.objects.get_or_create(user=user, **kwargs)
notification = cls.objects.filter(user=user, **kwargs).first()
if not notification:
notification = cls.objects.create(user=user, **kwargs)
if related_user:
notification.related_users.add(related_user)
notification.read = False

View file

@ -26,7 +26,7 @@
<form action="{% url 'unfollow' %}" method="POST" class="interaction follow_{{ user.id }} {% if not relationship.is_following and not relationship.is_follow_pending %}is-hidden{%endif %}" data-id="follow_{{ user.id }}">
{% csrf_token %}
<input type="hidden" name="user" value="{{ user.username }}">
{% if user.manually_approves_followers and not relationship.is_following %}
{% if relationship.is_follow_pending %}
<button class="button is-small is-danger is-light" type="submit">
{% trans "Undo follow request" %}
</button>

View file

@ -42,11 +42,11 @@ def get_relationship(context, user_object):
"""caches the relationship between the logged in user and another user"""
user = context["request"].user
return get_or_set(
f"cached-relationship-{user.id}-{user_object.id}",
f"relationship-{user.id}-{user_object.id}",
get_relationship_name,
user,
user_object,
timeout=259200,
timeout=60 * 60,
)

View file

@ -76,6 +76,17 @@ class Notification(TestCase):
notification.refresh_from_db()
self.assertEqual(notification.related_users.count(), 2)
def test_notify_grouping_with_dupes(self):
"""If there are multiple options to group with, don't cause an error"""
models.Notification.objects.create(
user=self.local_user, notification_type="FAVORITE"
)
models.Notification.objects.create(
user=self.local_user, notification_type="FAVORITE"
)
models.Notification.notify(self.local_user, None, notification_type="FAVORITE")
self.assertEqual(models.Notification.objects.count(), 2)
def test_notify_remote(self):
"""Don't create notifications for remote users"""
models.Notification.notify(

View file

@ -1,7 +1,9 @@
""" views for actions you can take in the application """
import urllib.parse
import re
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.views.decorators.http import require_POST
@ -13,6 +15,7 @@ from .helpers import (
handle_remote_webfinger,
subscribe_remote_webfinger,
WebFingerError,
is_api_request,
)
@ -34,6 +37,8 @@ def follow(request):
# that means we should save to trigger a re-broadcast
follow_request.save()
if is_api_request(request):
return HttpResponse()
return redirect(to_follow.local_path)
@ -58,8 +63,10 @@ def unfollow(request):
except models.UserFollowRequest.DoesNotExist:
clear_cache(request.user, to_unfollow)
if is_api_request(request):
return HttpResponse()
# this is handled with ajax so it shouldn't really matter
return redirect(request.headers.get("Referer", "/"))
return redirect("/")
@login_required