Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Federate reports #3188

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions bookwyrm/activitypub/__init__.py
Expand Up @@ -24,6 +24,7 @@
from .verbs import Add, Remove
from .verbs import Announce, Like
from .verbs import Move
from .verbs import Flag

# this creates a list of all the Activity types that we can serialize,
# so when an Activity comes in from outside, we can check if it's known
Expand Down
10 changes: 10 additions & 0 deletions bookwyrm/activitypub/verbs.py
Expand Up @@ -268,3 +268,13 @@ def action(self, allow_external_connections=True):
else:
# we might do something with this to move other objects at some point
pass


@dataclass(init=False)
class Flag(Verb):
"""Report a user to their home server"""

to: str
object: List[str] = None
links: List[str] = None
type: str = "Flag"
8 changes: 5 additions & 3 deletions bookwyrm/emailing.py
Expand Up @@ -50,9 +50,11 @@ def password_reset_email(reset_code):
def moderation_report_email(report):
"""a report was created"""
data = email_data()
data["reporter"] = report.reporter.localname or report.reporter.username
if report.user:
data["reportee"] = report.user.localname or report.user.username
data["reporter"] = report.user.localname or report.user.username
if report.reported_user:
data["reportee"] = (
report.reported_user.localname or report.reported_user.username
)
data["report_link"] = report.remote_id
data["link_domain"] = report.links.exists()

Expand Down
2 changes: 1 addition & 1 deletion bookwyrm/forms/forms.py
Expand Up @@ -44,7 +44,7 @@ class Meta:
class ReportForm(CustomForm):
class Meta:
model = models.Report
fields = ["user", "reporter", "status", "links", "note"]
fields = ["reported_user", "user", "status", "links", "note", "allow_broadcast"]


class ReadThroughForm(CustomForm):
Expand Down
57 changes: 57 additions & 0 deletions bookwyrm/migrations/0192_auto_20240102_2156.py
@@ -0,0 +1,57 @@
# Generated by Django 3.2.23 on 2024-01-02 21:56

import bookwyrm.models.fields
from django.conf import settings
from django.db import migrations
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0191_merge_20240102_0326"),
]

operations = [
migrations.AlterField(
model_name="report",
name="links",
field=bookwyrm.models.fields.ManyToManyField(
blank=True, to="bookwyrm.Link"
),
),
migrations.AlterField(
model_name="report",
name="note",
field=bookwyrm.models.fields.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name="report",
name="reporter",
field=bookwyrm.models.fields.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
related_name="reporter",
to=settings.AUTH_USER_MODEL,
),
),
migrations.AlterField(
model_name="report",
name="status",
field=bookwyrm.models.fields.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.PROTECT,
to="bookwyrm.status",
),
),
migrations.AlterField(
model_name="report",
name="user",
field=bookwyrm.models.fields.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.PROTECT,
to=settings.AUTH_USER_MODEL,
),
),
]
18 changes: 18 additions & 0 deletions bookwyrm/migrations/0193_rename_user_report_reported_user.py
@@ -0,0 +1,18 @@
# Generated by Django 3.2.23 on 2024-01-02 22:16

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0192_auto_20240102_2156"),
]

operations = [
migrations.RenameField(
model_name="report",
old_name="user",
new_name="reported_user",
),
]
18 changes: 18 additions & 0 deletions bookwyrm/migrations/0194_rename_reporter_report_user.py
@@ -0,0 +1,18 @@
# Generated by Django 3.2.23 on 2024-01-02 22:17

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0193_rename_user_report_reported_user"),
]

operations = [
migrations.RenameField(
model_name="report",
old_name="reporter",
new_name="user",
),
]
39 changes: 39 additions & 0 deletions bookwyrm/migrations/0195_auto_20240102_2310.py
@@ -0,0 +1,39 @@
# Generated by Django 3.2.23 on 2024-01-02 23:10

import bookwyrm.models.fields
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0194_rename_reporter_report_user"),
]

operations = [
migrations.AddField(
model_name="report",
name="allow_broadcast",
field=models.BooleanField(default=False),
),
migrations.AlterField(
model_name="report",
name="reported_user",
field=bookwyrm.models.fields.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name="reported_user",
to=settings.AUTH_USER_MODEL,
),
),
migrations.AlterField(
model_name="report",
name="user",
field=bookwyrm.models.fields.ForeignKey(
on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL
),
),
]
8 changes: 4 additions & 4 deletions bookwyrm/models/antispam.py
Expand Up @@ -109,9 +109,9 @@ def automod_users(reporter):
return report_model.objects.bulk_create(
[
report_model(
reporter=reporter,
user=reporter,
note=_("Automatically generated report"),
user=u,
reported_user=u,
)
for u in users
]
Expand Down Expand Up @@ -143,9 +143,9 @@ def automod_statuses(reporter):
return report_model.objects.bulk_create(
[
report_model(
reporter=reporter,
user=reporter,
note=_("Automatically generated report"),
user=s.user,
reported_user=s.user,
status=s,
)
for s in statuses
Expand Down
47 changes: 35 additions & 12 deletions bookwyrm/models/report.py
@@ -1,10 +1,12 @@
""" flagged for moderation """
from django.core.exceptions import PermissionDenied
from django.db import models
from django.utils.translation import gettext_lazy as _

from bookwyrm import activitypub
from bookwyrm.settings import DOMAIN
from .activitypub_mixin import ActivityMixin
from .base_model import BookWyrmModel
from . import fields


# Report action enums
Expand All @@ -22,28 +24,49 @@
DELETE_ITEM = "delete_item"


class Report(BookWyrmModel):
class Report(ActivityMixin, BookWyrmModel):
"""reported status or user"""

reporter = models.ForeignKey(
"User", related_name="reporter", on_delete=models.PROTECT
activity_serializer = activitypub.Flag

user = fields.ForeignKey(
"User",
on_delete=models.PROTECT,
activitypub_field="actor",
)
note = models.TextField(null=True, blank=True)
user = models.ForeignKey("User", on_delete=models.PROTECT, null=True, blank=True)
status = models.ForeignKey(
note = fields.TextField(null=True, blank=True, activitypub_field="content")
reported_user = fields.ForeignKey(
"User",
related_name="reported_user",
on_delete=models.PROTECT,
null=True,
blank=True,
activitypub_field="to",
)
status = fields.ForeignKey(
"Status",
null=True,
blank=True,
on_delete=models.PROTECT,
activitypub_field="object",
)
links = models.ManyToManyField("Link", blank=True)
links = fields.ManyToManyField("Link", blank=True)
resolved = models.BooleanField(default=False)
allow_broadcast = models.BooleanField(default=False)

def raise_not_editable(self, viewer):
"""instead of user being the owner field, it's reporter"""
if self.reporter == viewer or viewer.has_perm("bookwyrm.moderate_user"):
def broadcast(self, activity, sender, *args, **kwargs):
"""only need to send an activity for remote offenders"""
# don't try to broadcast if the reporter doesn't want you to,
# or if the reported user is local
if self.reported_user.local or not self.allow_broadcast:
return
raise PermissionDenied()
super().broadcast(activity, sender, *args, **kwargs)

def get_recipients(self, software=None):
"""Send this to the public inbox of the offending instance"""
if self.reported_user.local:
return []
return [self.reported_user.shared_inbox or self.reported_user.inbox]

def get_remote_id(self):
return f"https://{DOMAIN}/settings/reports/{self.id}"
Expand Down
2 changes: 1 addition & 1 deletion bookwyrm/templates/report.html
Expand Up @@ -6,5 +6,5 @@
{% endblock %}

{% block content %}
{% include "snippets/report_modal.html" with user=user active=True static=True id="report-modal" %}
{% include "snippets/report_modal.html" with reported_user=reported_user active=True static=True id="report-modal" %}
{% endblock %}
12 changes: 6 additions & 6 deletions bookwyrm/templates/settings/reports/report.html
Expand Up @@ -27,7 +27,7 @@
</summary>
<div class="box">
{% trans "Update on your report:" as dm_template %}
{% include 'snippets/create_status/status.html' with type="direct" uuid=1 mention=report.reporter prepared_content=dm_template no_script=True %}
{% include 'snippets/create_status/status.html' with type="direct" uuid=1 mention=report.user prepared_content=dm_template no_script=True %}
</div>
</details>
</div>
Expand Down Expand Up @@ -56,10 +56,10 @@ <h3 class="title is-4">{% trans "Reported links" %}</h3>
</div>
{% endif %}

{% if report.user %}
{% include 'settings/users/user_info.html' with user=report.user %}
{% if report.reported_user %}
{% include 'settings/users/user_info.html' with user=report.reported_user %}

{% include 'settings/users/user_moderation_actions.html' with user=report.user %}
{% include 'settings/users/user_moderation_actions.html' with user=report.reported_user %}
{% endif %}

<div class="block content">
Expand All @@ -70,8 +70,8 @@ <h3 class="title is-4">{% trans "Moderation Activity" %}</h3>
<li class="mb-2">
<div class="is-flex">
<p class="mb-0 is-flex-grow-1">
{% blocktrans trimmed with user=report.reporter|username user_link=report.reporter.local_path %}
<a href="{{ user_link }}">{{ user}}</a> opened this report
{% blocktrans trimmed with user=report.user|username user_link=report.user.local_path %}
<a href="{{ user_link }}">{{ user }}</a> opened this report
{% endblocktrans %}
</p>
<span class="tag">{{ report.created_date }}</span>
Expand Down
8 changes: 4 additions & 4 deletions bookwyrm/templates/settings/reports/report_header.html
Expand Up @@ -3,14 +3,14 @@

{% if report.status %}

{% blocktrans trimmed with report_id=report.id username=report.user|username %}
{% blocktrans trimmed with report_id=report.id username=report.reported_user|username %}
Report #{{ report_id }}: Status posted by @{{ username }}
{% endblocktrans %}

{% elif report.links.exists %}

{% if report.user %}
{% blocktrans trimmed with report_id=report.id username=report.user|username %}
{% if report.reported_user %}
{% blocktrans trimmed with report_id=report.id username=report.reported_user|username %}
Report #{{ report_id }}: Link added by @{{ username }}
{% endblocktrans %}
{% else %}
Expand All @@ -21,7 +21,7 @@

{% else %}

{% blocktrans trimmed with report_id=report.id username=report.user|username %}
{% blocktrans trimmed with report_id=report.id username=report.reported_user|username %}
Report #{{ report_id }}: User @{{ username }}
{% endblocktrans %}

Expand Down
11 changes: 10 additions & 1 deletion bookwyrm/templates/settings/reports/report_preview.html
Expand Up @@ -21,8 +21,17 @@ <h2 class="card-header-title has-background-secondary is-block">

{% block card-footer %}
<div class="card-footer-item">
<p>{% blocktrans with username=report.reporter|username path=report.reporter.local_path %}Reported by <a href="{{ path }}">@{{ username }}</a>{% endblocktrans %}</p>
<p>{% blocktrans with username=report.user|username path=report.user.local_path %}Reported by <a href="{{ path }}">@{{ username }}</a>{% endblocktrans %}</p>
</div>
{% if not report.reported_user.local %}
<div class="card-footer-item">
{% if report.allow_broadcast %}
<p>{% trans "Sent to remote instance" %}</p>
{% else %}
<p>{% trans "<b>Not</b> sent to remote instance" %}</p>
{% endif %}
</div>
{% endif %}
<div class="card-footer-item">
{{ report.created_date | naturaltime }}
</div>
Expand Down
2 changes: 1 addition & 1 deletion bookwyrm/templates/snippets/report_button.html
Expand Up @@ -12,6 +12,6 @@
>
{% trans "Report" %}
</button>
{% include 'snippets/report_modal.html' with user=user id=modal_id status_id=status.id %}
{% include 'snippets/report_modal.html' with reported_user=user id=modal_id status_id=status.id %}

{% endwith %}