moviewyrm/bookwyrm/models/antispam.py

144 lines
4.2 KiB
Python
Raw Normal View History

2021-09-17 18:57:28 +00:00
""" Lets try NOT to sell viagra """
2022-02-24 20:15:08 +00:00
from functools import reduce
import operator
from django.apps import apps
2021-09-17 18:57:28 +00:00
from django.db import models
2022-02-24 20:15:08 +00:00
from django.db.models import Q
from django.utils.translation import gettext_lazy as _
2021-09-17 18:57:28 +00:00
2022-02-24 20:15:08 +00:00
from bookwyrm.tasks import app
2021-09-17 18:57:28 +00:00
from .user import User
class EmailBlocklist(models.Model):
"""blocked email addresses"""
created_date = models.DateTimeField(auto_now_add=True)
domain = models.CharField(max_length=255, unique=True)
is_active = models.BooleanField(default=True)
class Meta:
"""default sorting"""
ordering = ("-created_date",)
@property
def users(self):
"""find the users associated with this address"""
return User.objects.filter(email__endswith=f"@{self.domain}")
class IPBlocklist(models.Model):
"""blocked ip addresses"""
created_date = models.DateTimeField(auto_now_add=True)
address = models.CharField(max_length=255, unique=True)
is_active = models.BooleanField(default=True)
class Meta:
"""default sorting"""
ordering = ("-created_date",)
2022-02-24 19:18:43 +00:00
class AutoMod(models.Model):
"""rules to automatically flag suspicious activity"""
2022-02-24 20:15:08 +00:00
2022-02-24 19:18:43 +00:00
string_match = models.CharField(max_length=200, unique=True)
flag_users = models.BooleanField(default=True)
flag_statuses = models.BooleanField(default=True)
created_by = models.ForeignKey("User", on_delete=models.PROTECT)
2022-02-24 20:15:08 +00:00
@app.task(queue="low_priority")
def automod_task():
"""Create reports"""
if not AutoMod.objects.exists():
return
reporter = AutoMod.objects.first().created_by
2022-02-24 21:20:18 +00:00
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
]
)
2022-02-24 20:15:08 +00:00
def automod_users(reporter):
"""check users for moderation flags"""
user_rules = AutoMod.objects.filter(flag_users=True).values_list(
"string_match", flat=True
)
2022-02-24 21:27:26 +00:00
filters = []
for field in ["username", "summary", "name"]:
filters += [{f"{field}__icontains": r} for r in user_rules]
users = (
User.objects.filter(
reduce(operator.or_, (Q(**f) for f in filters)),
is_active=True,
report__isnull=True, # don't flag users that already have reports
)
.distinct()
.values_list("id", flat=True)
)
2022-02-24 20:15:08 +00:00
report_model = apps.get_model("bookwyrm", "Report", require_ready=True)
2022-02-24 21:20:18 +00:00
return report_model.objects.bulk_create(
2022-02-24 20:15:31 +00:00
[
report_model(
reporter=reporter,
note=_("Automatically generated report"),
user=u,
)
for u in users
]
)
2022-02-24 20:15:08 +00:00
def automod_statuses(reporter):
2022-02-24 20:15:31 +00:00
"""check statues for moderation flags"""
2022-02-24 20:15:08 +00:00
status_rules = AutoMod.objects.filter(flag_statuses=True).values_list(
"string_match", flat=True
)
2022-02-24 21:27:26 +00:00
filters = []
for field in ["content", "content_warning", "quotation__quote", "review__name"]:
filters += [{f"{field}__icontains": r} for r in status_rules]
2022-02-24 20:15:08 +00:00
status_model = apps.get_model("bookwyrm", "Status", require_ready=True)
statuses = status_model.objects.filter(
reduce(operator.or_, (Q(**f) for f in filters)),
deleted=False,
2022-02-24 20:15:31 +00:00
report__isnull=True, # don't flag statuses that already have reports
2022-02-24 21:27:26 +00:00
).distinct()
2022-02-24 20:15:08 +00:00
report_model = apps.get_model("bookwyrm", "Report", require_ready=True)
2022-02-24 21:20:18 +00:00
return report_model.objects.bulk_create(
2022-02-24 20:15:31 +00:00
[
report_model(
reporter=reporter,
note=_("Automatically generated report"),
user=s.user,
2022-02-24 20:48:52 +00:00
status=s,
2022-02-24 20:15:31 +00:00
)
for s in statuses
]
)