From 93f82fbf18079b7d9dd5a28f5edb7466eb515122 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 24 Feb 2022 13:20:18 -0800 Subject: [PATCH] Adds notifications --- bookwyrm/migrations/0139_report_status.py | 16 +++++++++--- .../migrations/0140_remove_report_statuses.py | 6 ++--- .../migrations/0141_alter_report_status.py | 13 +++++++--- bookwyrm/models/antispam.py | 26 ++++++++++++++++--- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/bookwyrm/migrations/0139_report_status.py b/bookwyrm/migrations/0139_report_status.py index fc9386335..c85a43b8f 100644 --- a/bookwyrm/migrations/0139_report_status.py +++ b/bookwyrm/migrations/0139_report_status.py @@ -13,6 +13,7 @@ def set_report_statuses(apps, schema_editor): report.status = report.statuses.first() report.save() + def set_reverse(apps, schema_editor): """copy over status fields""" db_alias = schema_editor.connection.alias @@ -21,17 +22,24 @@ def set_reverse(apps, schema_editor): for report in reports: report.statuses.set(report.status) + class Migration(migrations.Migration): dependencies = [ - ('bookwyrm', '0138_automod'), + ("bookwyrm", "0138_automod"), ] operations = [ migrations.AddField( - model_name='report', - name='status', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='reports', to='bookwyrm.status'), + model_name="report", + name="status", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + related_name="reports", + to="bookwyrm.status", + ), ), migrations.RunPython(set_report_statuses, reverse_code=set_reverse), ] diff --git a/bookwyrm/migrations/0140_remove_report_statuses.py b/bookwyrm/migrations/0140_remove_report_statuses.py index 94303ce9b..57651c3bf 100644 --- a/bookwyrm/migrations/0140_remove_report_statuses.py +++ b/bookwyrm/migrations/0140_remove_report_statuses.py @@ -6,12 +6,12 @@ from django.db import migrations class Migration(migrations.Migration): dependencies = [ - ('bookwyrm', '0139_report_status'), + ("bookwyrm", "0139_report_status"), ] operations = [ migrations.RemoveField( - model_name='report', - name='statuses', + model_name="report", + name="statuses", ), ] diff --git a/bookwyrm/migrations/0141_alter_report_status.py b/bookwyrm/migrations/0141_alter_report_status.py index e0a7e58ee..58102dd50 100644 --- a/bookwyrm/migrations/0141_alter_report_status.py +++ b/bookwyrm/migrations/0141_alter_report_status.py @@ -7,13 +7,18 @@ import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ - ('bookwyrm', '0140_remove_report_statuses'), + ("bookwyrm", "0140_remove_report_statuses"), ] operations = [ migrations.AlterField( - model_name='report', - name='status', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='bookwyrm.status'), + model_name="report", + name="status", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + to="bookwyrm.status", + ), ), ] diff --git a/bookwyrm/models/antispam.py b/bookwyrm/models/antispam.py index d948d4190..8508c3959 100644 --- a/bookwyrm/models/antispam.py +++ b/bookwyrm/models/antispam.py @@ -57,8 +57,26 @@ def automod_task(): if not AutoMod.objects.exists(): return reporter = AutoMod.objects.first().created_by - automod_users(reporter) - automod_statuses(reporter) + reports = automod_users(reporter) + automod_statuses(reporter) + if reports: + admins = User.objects.filter( + 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 + ) + for admin in admins: + notification_model.objects.bulk_create( + [ + notification_model( + user=admin, + related_report=r, + notification_type="REPORT", + ) + for r in reports + ] + ) def automod_users(reporter): @@ -76,7 +94,7 @@ def automod_users(reporter): report_model = apps.get_model("bookwyrm", "Report", require_ready=True) - report_model.objects.bulk_create( + return report_model.objects.bulk_create( [ report_model( reporter=reporter, @@ -103,7 +121,7 @@ def automod_statuses(reporter): ) report_model = apps.get_model("bookwyrm", "Report", require_ready=True) - report_model.objects.bulk_create( + return report_model.objects.bulk_create( [ report_model( reporter=reporter,