diff --git a/api/funkwhale_api/manage/serializers.py b/api/funkwhale_api/manage/serializers.py index 8d4b01b05..38a9cd57a 100644 --- a/api/funkwhale_api/manage/serializers.py +++ b/api/funkwhale_api/manage/serializers.py @@ -8,6 +8,7 @@ from funkwhale_api.federation import models as federation_models from funkwhale_api.federation import fields as federation_fields from funkwhale_api.federation import tasks as federation_tasks from funkwhale_api.moderation import models as moderation_models +from funkwhale_api.moderation import serializers as moderation_serializers from funkwhale_api.music import models as music_models from funkwhale_api.music import serializers as music_serializers from funkwhale_api.tags import models as tags_models @@ -629,3 +630,42 @@ class ManageTagActionSerializer(common_serializers.ActionSerializer): @transaction.atomic def handle_delete(self, objects): return objects.delete() + + +class ManageReportSerializer(serializers.ModelSerializer): + assigned_to = ManageBaseActorSerializer() + target_owner = ManageBaseActorSerializer() + submitter = ManageBaseActorSerializer() + target = moderation_serializers.TARGET_FIELD + + class Meta: + model = moderation_models.Report + fields = [ + "id", + "uuid", + "fid", + "creation_date", + "handled_date", + "summary", + "type", + "target", + "target_state", + "is_handled", + "assigned_to", + "target_owner", + "submitter", + "submitter_email", + ] + read_only_fields = [ + "id", + "uuid", + "fid", + "submitter", + "submitter_email", + "creation_date", + "handled_date", + "target", + "target_state", + "target_owner", + "summary", + ] diff --git a/api/funkwhale_api/moderation/factories.py b/api/funkwhale_api/moderation/factories.py index 4bf7ce584..3db5d2060 100644 --- a/api/funkwhale_api/moderation/factories.py +++ b/api/funkwhale_api/moderation/factories.py @@ -5,6 +5,8 @@ from funkwhale_api.federation import factories as federation_factories from funkwhale_api.music import factories as music_factories from funkwhale_api.users import factories as users_factories +from . import serializers + @registry.register class InstancePolicyFactory(NoUpdateOnCreate, factory.DjangoModelFactory): @@ -51,3 +53,13 @@ class ReportFactory(NoUpdateOnCreate, factory.DjangoModelFactory): class Params: anonymous = factory.Trait(actor=None, submitter_email=factory.Faker("email")) + assigned = factory.Trait( + assigned_to=factory.SubFactory(federation_factories.ActorFactory) + ) + + @factory.post_generation + def _set_target_owner(self, create, extracted, **kwargs): + if not self.target: + return + + self.target_owner = serializers.get_target_owner(self.target) diff --git a/api/funkwhale_api/moderation/serializers.py b/api/funkwhale_api/moderation/serializers.py index c52a9e913..6e216f29e 100644 --- a/api/funkwhale_api/moderation/serializers.py +++ b/api/funkwhale_api/moderation/serializers.py @@ -160,26 +160,29 @@ def get_target_owner(target): return mapping[target.__class__](target) +TARGET_FIELD = common_fields.GenericRelation( + { + "artist": {"queryset": music_models.Artist.objects.all()}, + "album": {"queryset": music_models.Album.objects.all()}, + "track": {"queryset": music_models.Track.objects.all()}, + "library": { + "queryset": music_models.Library.objects.all(), + "id_attr": "uuid", + "id_field": serializers.UUIDField(), + }, + "playlist": {"queryset": playlists_models.Playlist.objects.all()}, + "account": { + "queryset": federation_models.Actor.objects.all(), + "id_attr": "full_username", + "id_field": serializers.EmailField(), + "get_query": get_actor_query, + }, + } +) + + class ReportSerializer(serializers.ModelSerializer): - target = common_fields.GenericRelation( - { - "artist": {"queryset": music_models.Artist.objects.all()}, - "album": {"queryset": music_models.Album.objects.all()}, - "track": {"queryset": music_models.Track.objects.all()}, - "library": { - "queryset": music_models.Library.objects.all(), - "id_attr": "uuid", - "id_field": serializers.UUIDField(), - }, - "playlist": {"queryset": playlists_models.Playlist.objects.all()}, - "account": { - "queryset": federation_models.Actor.objects.all(), - "id_attr": "full_username", - "id_field": serializers.EmailField(), - "get_query": get_actor_query, - }, - } - ) + target = TARGET_FIELD class Meta: model = models.Report diff --git a/api/tests/manage/test_serializers.py b/api/tests/manage/test_serializers.py index 16fd9ce58..7f99dd901 100644 --- a/api/tests/manage/test_serializers.py +++ b/api/tests/manage/test_serializers.py @@ -521,3 +521,31 @@ def test_manage_tag_serializer(factories, to_api_date): s = serializers.ManageTagSerializer(tag) assert s.data == expected + + +def test_manage_report_serializer(factories, to_api_date): + artist = factories["music.Artist"](attributed=True) + report = factories["moderation.Report"]( + target=artist, target_state={"hello": "world"}, assigned=True + ) + expected = { + "id": report.id, + "uuid": str(report.uuid), + "fid": report.fid, + "creation_date": to_api_date(report.creation_date), + "handled_date": None, + "summary": report.summary, + "is_handled": report.is_handled, + "type": report.type, + "submitter_email": None, + "submitter": serializers.ManageBaseActorSerializer(report.submitter).data, + "assigned_to": serializers.ManageBaseActorSerializer(report.assigned_to).data, + "target": {"type": "artist", "id": artist.pk}, + "target_owner": serializers.ManageBaseActorSerializer( + artist.attributed_to + ).data, + "target_state": report.target_state, + } + s = serializers.ManageReportSerializer(report) + + assert s.data == expected