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

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5b229fa
Makes reports an activitypub model
mouse-reeve Jan 2, 2024
5c0ade5
Changes field names in report model so the reporter is "user"
mouse-reeve Jan 2, 2024
052823e
Allow the user to decide if they want to send along a report
mouse-reeve Jan 2, 2024
d4013b9
Fixes some user/reported_user references
mouse-reeve Jan 3, 2024
0612930
UI for deciding to send reports to remote
mouse-reeve Jan 3, 2024
8de3668
Expands tests and fixes logic error in report recipients
mouse-reeve Jan 3, 2024
9682f41
Merge branch 'main' into federate-reports
mouse-reeve Apr 3, 2024
a7669aa
Adds merge migration
mouse-reeve Apr 3, 2024
8fdcd4b
Fixes error in report tests
mouse-reeve Apr 3, 2024
f5c3b62
Makes report button work without javascript
mouse-reeve Apr 3, 2024
875c712
Fixes static view of report form in no js mode
mouse-reeve Apr 3, 2024
2e4e965
Adds first-pass test for incoming reports
mouse-reeve Apr 3, 2024
e0719b3
Json view of admin report
mouse-reeve Apr 3, 2024
4ef396f
Merge branch 'main' into federate-reports
mouse-reeve Aug 24, 2024
aab2588
Merge branch 'federate-reports' of github.com:bookwyrm-social/bookwyr…
mouse-reeve Aug 24, 2024
a920c11
Adds merge migration
mouse-reeve Aug 24, 2024
063f6d6
Adds list of "object"s to activitypub flag object
mouse-reeve Aug 24, 2024
c76087e
Get flagged status out of incoming Flag activity
mouse-reeve Aug 24, 2024
0dcd95d
Converts status to many to many field on report model
mouse-reeve Aug 24, 2024
6e5d064
Updates tests for reports
mouse-reeve Aug 24, 2024
e21f57d
Updates antispam automoderator to handle new statuses field
mouse-reeve Aug 26, 2024
b272de8
Merge branch 'main' into federate-reports
mouse-reeve Aug 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bookwyrm/activitypub/__init__.py
Original file line number Diff line number Diff line change
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
35 changes: 34 additions & 1 deletion bookwyrm/activitypub/verbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from typing import List
from django.apps import apps

from .base_activity import ActivityObject, Signature, resolve_remote_id
from .base_activity import (
ActivityObject,
ActivitySerializerError,
Signature,
resolve_remote_id,
)
from .ordered_collection import CollectionItem


Expand Down Expand Up @@ -264,3 +269,31 @@ 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"
content: str = None

def action(self, allow_external_connections=False):
"""Create the report and attach reported statuses"""
report = self.to_model(allow_external_connections=allow_external_connections)
# go through "objects" and figure out what they are
for obj in self.object:
# what type of obj is it?
try:
item = resolve_remote_id(
remote_id=obj,
save=False,
model="Status",
allow_external_connections=allow_external_connections,
)
except ActivitySerializerError:
continue
report.statuses.add(item)
8 changes: 5 additions & 3 deletions bookwyrm/emailing.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,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
9 changes: 8 additions & 1 deletion bookwyrm/forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ class Meta:
class ReportForm(CustomForm):
class Meta:
model = models.Report
fields = ["user", "reporter", "status", "links", "note"]
fields = [
"reported_user",
"user",
"statuses",
"links",
"note",
"allow_broadcast",
]


class ReadThroughForm(CustomForm):
Expand Down
57 changes: 57 additions & 0 deletions bookwyrm/migrations/0192_auto_20240102_2156.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
),
),
]
13 changes: 13 additions & 0 deletions bookwyrm/migrations/0200_merge_20240403_1728.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generated by Django 3.2.25 on 2024-04-03 17:28

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0195_auto_20240102_2310"),
("bookwyrm", "0199_status_bookwyrm_st_remote__06aeba_idx"),
]

operations = []
13 changes: 13 additions & 0 deletions bookwyrm/migrations/0209_merge_20240824_2211.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generated by Django 4.2.15 on 2024-08-24 22:11

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0200_merge_20240403_1728"),
("bookwyrm", "0208_merge_0207_merge_20240629_0626_0207_sqlparse_update"),
]

operations = []
52 changes: 52 additions & 0 deletions bookwyrm/migrations/0210_report_statuses_alter_report_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 4.2.15 on 2024-08-24 23:28

from django.db import migrations, models
import django.db.models.deletion


def copy_status_field(apps, schema_editor):
"""Move the foreign key status to the new many to many field"""
reports = apps.get_model("bookwyrm", "Report")
db_alias = schema_editor.connection.alias
for report in reports.objects.using(db_alias).filter(status__isnull=False):
report.statuses.add(report.status)


def reverse_copy_status_field(apps, schema_editor):
"""Move the first status in the many to many to the old FK"""
reports = apps.get_model("bookwyrm", "Report")
db_alias = schema_editor.connection.alias
for report in reports.objects.using(db_alias).filter(statuses__isnull=False):
report.status = report.statuses.first()
report.save(broadcast=False, updated_fields=["status"])


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0209_merge_20240824_2211"),
]

operations = [
migrations.AddField(
model_name="report",
name="statuses",
field=models.ManyToManyField(
blank=True,
null=True,
related_name="status_intermediary_name",
to="bookwyrm.status",
),
),
migrations.AlterField(
model_name="report",
name="status",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.PROTECT,
to="bookwyrm.status",
),
),
migrations.RunPython(copy_status_field, reverse_code=reverse_copy_status_field),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.15 on 2024-08-24 23:40

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0210_report_statuses_alter_report_status"),
]

operations = [
migrations.RemoveField(
model_name="report",
name="status",
),
migrations.AlterField(
model_name="report",
name="statuses",
field=models.ManyToManyField(blank=True, null=True, to="bookwyrm.status"),
),
]
22 changes: 11 additions & 11 deletions bookwyrm/models/antispam.py
Original file line number Diff line number Diff line change
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 @@ -140,14 +140,14 @@ def automod_statuses(reporter):
).distinct()

report_model = apps.get_model("bookwyrm", "Report", require_ready=True)
return report_model.objects.bulk_create(
[
report_model(
reporter=reporter,
reports = []
for status in statuses:
with transaction.atomic():
report = report_model.objects.create(
user=reporter,
note=_("Automatically generated report"),
user=s.user,
status=s,
reported_user=status.user,
)
for s in statuses
]
)
report.statuses.add(status)
reports.append(report)
return reports
Loading
Loading