mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-02-02 12:22:22 +00:00
Merge pull request #2300 from bookwyrm-social/notification
Fixes creating notifications for automod reports
This commit is contained in:
commit
d023f71058
5 changed files with 17 additions and 12 deletions
|
@ -61,17 +61,14 @@ def automod_task():
|
||||||
if not reports:
|
if not reports:
|
||||||
return
|
return
|
||||||
|
|
||||||
admins = User.objects.filter(
|
admins = User.admins()
|
||||||
models.Q(user_permissions__name__in=["moderate_user", "moderate_post"])
|
|
||||||
| models.Q(is_superuser=True)
|
|
||||||
).all()
|
|
||||||
notification_model = apps.get_model("bookwyrm", "Notification", require_ready=True)
|
notification_model = apps.get_model("bookwyrm", "Notification", require_ready=True)
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
for admin in admins:
|
for admin in admins:
|
||||||
notification, _ = notification_model.objects.get_or_create(
|
notification, _ = notification_model.objects.get_or_create(
|
||||||
user=admin, notification_type=notification_model.REPORT, read=False
|
user=admin, notification_type=notification_model.REPORT, read=False
|
||||||
)
|
)
|
||||||
notification.related_repors.add(reports)
|
notification.related_reports.set(reports)
|
||||||
|
|
||||||
|
|
||||||
def automod_users(reporter):
|
def automod_users(reporter):
|
||||||
|
|
|
@ -231,10 +231,7 @@ def notify_admins_on_report(sender, instance, created, *args, **kwargs):
|
||||||
return
|
return
|
||||||
|
|
||||||
# moderators and superusers should be notified
|
# moderators and superusers should be notified
|
||||||
admins = User.objects.filter(
|
admins = User.admins()
|
||||||
models.Q(user_permissions__name__in=["moderate_user", "moderate_post"])
|
|
||||||
| models.Q(is_superuser=True)
|
|
||||||
).all()
|
|
||||||
for admin in admins:
|
for admin in admins:
|
||||||
notification, _ = Notification.objects.get_or_create(
|
notification, _ = Notification.objects.get_or_create(
|
||||||
user=admin,
|
user=admin,
|
||||||
|
|
|
@ -231,6 +231,14 @@ class User(OrderedCollectionPageMixin, AbstractUser):
|
||||||
queryset = queryset.exclude(blocks=viewer)
|
queryset = queryset.exclude(blocks=viewer)
|
||||||
return queryset
|
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):
|
def update_active_date(self):
|
||||||
"""this user is here! they are doing things!"""
|
"""this user is here! they are doing things!"""
|
||||||
self.last_active_date = timezone.now()
|
self.last_active_date = timezone.now()
|
||||||
|
|
|
@ -14,6 +14,7 @@ from bookwyrm.models.antispam import automod_task
|
||||||
class AutomodModel(TestCase):
|
class AutomodModel(TestCase):
|
||||||
"""every response to a get request, html or json"""
|
"""every response to a get request, html or json"""
|
||||||
|
|
||||||
|
# pylint: disable=invalid-name
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""we need basic test data and mocks"""
|
"""we need basic test data and mocks"""
|
||||||
self.factory = RequestFactory()
|
self.factory = RequestFactory()
|
||||||
|
@ -26,6 +27,7 @@ class AutomodModel(TestCase):
|
||||||
"password",
|
"password",
|
||||||
local=True,
|
local=True,
|
||||||
localname="mouse",
|
localname="mouse",
|
||||||
|
is_superuser=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_automod_task_no_rules(self, *_):
|
def test_automod_task_no_rules(self, *_):
|
||||||
|
@ -33,6 +35,7 @@ class AutomodModel(TestCase):
|
||||||
self.assertFalse(models.Report.objects.exists())
|
self.assertFalse(models.Report.objects.exists())
|
||||||
automod_task()
|
automod_task()
|
||||||
self.assertFalse(models.Report.objects.exists())
|
self.assertFalse(models.Report.objects.exists())
|
||||||
|
self.assertFalse(models.Notification.objects.exists())
|
||||||
|
|
||||||
def test_automod_task_user(self, *_):
|
def test_automod_task_user(self, *_):
|
||||||
"""scan activity"""
|
"""scan activity"""
|
||||||
|
@ -52,6 +55,7 @@ class AutomodModel(TestCase):
|
||||||
reports = models.Report.objects.all()
|
reports = models.Report.objects.all()
|
||||||
self.assertEqual(reports.count(), 1)
|
self.assertEqual(reports.count(), 1)
|
||||||
self.assertEqual(reports.first().user, self.local_user)
|
self.assertEqual(reports.first().user, self.local_user)
|
||||||
|
self.assertEqual(models.Notification.objects.count(), 1)
|
||||||
|
|
||||||
def test_automod_status(self, *_):
|
def test_automod_status(self, *_):
|
||||||
"""scan activity"""
|
"""scan activity"""
|
||||||
|
@ -73,3 +77,4 @@ class AutomodModel(TestCase):
|
||||||
self.assertEqual(reports.count(), 1)
|
self.assertEqual(reports.count(), 1)
|
||||||
self.assertEqual(reports.first().status, status)
|
self.assertEqual(reports.first().status, status)
|
||||||
self.assertEqual(reports.first().user, self.local_user)
|
self.assertEqual(reports.first().user, self.local_user)
|
||||||
|
self.assertEqual(models.Notification.objects.count(), 1)
|
||||||
|
|
|
@ -19,9 +19,7 @@ def about(request):
|
||||||
"status_count": models.Status.objects.filter(
|
"status_count": models.Status.objects.filter(
|
||||||
user__local=True, deleted=False
|
user__local=True, deleted=False
|
||||||
).count(),
|
).count(),
|
||||||
"admins": models.User.objects.filter(
|
"admins": models.User.admins(),
|
||||||
groups__name__in=["admin", "moderator"]
|
|
||||||
).distinct(),
|
|
||||||
"version": settings.VERSION,
|
"version": settings.VERSION,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue