Merge pull request #2300 from bookwyrm-social/notification

Fixes creating notifications for automod reports
This commit is contained in:
Mouse Reeve 2022-09-19 12:00:02 -07:00 committed by GitHub
commit d023f71058
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 12 deletions

View file

@ -61,17 +61,14 @@ def automod_task():
if not reports:
return
admins = User.objects.filter(
models.Q(user_permissions__name__in=["moderate_user", "moderate_post"])
| models.Q(is_superuser=True)
).all()
admins = User.admins()
notification_model = apps.get_model("bookwyrm", "Notification", require_ready=True)
with transaction.atomic():
for admin in admins:
notification, _ = notification_model.objects.get_or_create(
user=admin, notification_type=notification_model.REPORT, read=False
)
notification.related_repors.add(reports)
notification.related_reports.set(reports)
def automod_users(reporter):

View file

@ -231,10 +231,7 @@ def notify_admins_on_report(sender, instance, created, *args, **kwargs):
return
# moderators and superusers should be notified
admins = User.objects.filter(
models.Q(user_permissions__name__in=["moderate_user", "moderate_post"])
| models.Q(is_superuser=True)
).all()
admins = User.admins()
for admin in admins:
notification, _ = Notification.objects.get_or_create(
user=admin,

View file

@ -231,6 +231,14 @@ class User(OrderedCollectionPageMixin, AbstractUser):
queryset = queryset.exclude(blocks=viewer)
return queryset
@classmethod
def admins(cls):
"""Get a queryset of the admins for this instance"""
return cls.objects.filter(
models.Q(user_permissions__name__in=["moderate_user", "moderate_post"])
| models.Q(is_superuser=True)
)
def update_active_date(self):
"""this user is here! they are doing things!"""
self.last_active_date = timezone.now()

View file

@ -14,6 +14,7 @@ from bookwyrm.models.antispam import automod_task
class AutomodModel(TestCase):
"""every response to a get request, html or json"""
# pylint: disable=invalid-name
def setUp(self):
"""we need basic test data and mocks"""
self.factory = RequestFactory()
@ -26,6 +27,7 @@ class AutomodModel(TestCase):
"password",
local=True,
localname="mouse",
is_superuser=True,
)
def test_automod_task_no_rules(self, *_):
@ -33,6 +35,7 @@ class AutomodModel(TestCase):
self.assertFalse(models.Report.objects.exists())
automod_task()
self.assertFalse(models.Report.objects.exists())
self.assertFalse(models.Notification.objects.exists())
def test_automod_task_user(self, *_):
"""scan activity"""
@ -52,6 +55,7 @@ class AutomodModel(TestCase):
reports = models.Report.objects.all()
self.assertEqual(reports.count(), 1)
self.assertEqual(reports.first().user, self.local_user)
self.assertEqual(models.Notification.objects.count(), 1)
def test_automod_status(self, *_):
"""scan activity"""
@ -73,3 +77,4 @@ class AutomodModel(TestCase):
self.assertEqual(reports.count(), 1)
self.assertEqual(reports.first().status, status)
self.assertEqual(reports.first().user, self.local_user)
self.assertEqual(models.Notification.objects.count(), 1)

View file

@ -19,9 +19,7 @@ def about(request):
"status_count": models.Status.objects.filter(
user__local=True, deleted=False
).count(),
"admins": models.User.objects.filter(
groups__name__in=["admin", "moderator"]
).distinct(),
"admins": models.User.admins(),
"version": settings.VERSION,
}