2018-09-06 18:35:02 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from funkwhale_api.common import serializers as common_serializers
|
|
|
|
from funkwhale_api.music import models as music_models
|
|
|
|
|
2018-09-13 15:18:23 +00:00
|
|
|
from . import filters
|
2018-09-06 18:35:02 +00:00
|
|
|
from . import models
|
2018-09-13 15:18:23 +00:00
|
|
|
from . import serializers as federation_serializers
|
2018-09-06 18:35:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NestedLibraryFollowSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = models.LibraryFollow
|
|
|
|
fields = ["creation_date", "uuid", "fid", "approved", "modification_date"]
|
|
|
|
|
|
|
|
|
2018-09-24 18:44:22 +00:00
|
|
|
class LibraryScanSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = music_models.LibraryScan
|
|
|
|
fields = [
|
|
|
|
"total_files",
|
|
|
|
"processed_files",
|
|
|
|
"errored_files",
|
|
|
|
"status",
|
|
|
|
"creation_date",
|
|
|
|
"modification_date",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2018-09-06 18:35:02 +00:00
|
|
|
class LibrarySerializer(serializers.ModelSerializer):
|
|
|
|
actor = federation_serializers.APIActorSerializer()
|
2018-09-22 12:29:30 +00:00
|
|
|
uploads_count = serializers.SerializerMethodField()
|
2018-09-24 18:44:22 +00:00
|
|
|
latest_scan = serializers.SerializerMethodField()
|
2018-09-06 18:35:02 +00:00
|
|
|
follow = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = music_models.Library
|
|
|
|
fields = [
|
|
|
|
"fid",
|
|
|
|
"uuid",
|
|
|
|
"actor",
|
|
|
|
"name",
|
|
|
|
"description",
|
|
|
|
"creation_date",
|
2018-09-22 12:29:30 +00:00
|
|
|
"uploads_count",
|
2018-09-06 18:35:02 +00:00
|
|
|
"privacy_level",
|
|
|
|
"follow",
|
2018-09-24 18:44:22 +00:00
|
|
|
"latest_scan",
|
2018-09-06 18:35:02 +00:00
|
|
|
]
|
|
|
|
|
2018-09-22 12:29:30 +00:00
|
|
|
def get_uploads_count(self, o):
|
|
|
|
return max(getattr(o, "_uploads_count", 0), o.uploads_count)
|
2018-09-06 18:35:02 +00:00
|
|
|
|
|
|
|
def get_follow(self, o):
|
|
|
|
try:
|
|
|
|
return NestedLibraryFollowSerializer(o._follows[0]).data
|
|
|
|
except (AttributeError, IndexError):
|
|
|
|
return None
|
|
|
|
|
2018-09-24 18:44:22 +00:00
|
|
|
def get_latest_scan(self, o):
|
|
|
|
scan = o.scans.order_by("-creation_date").first()
|
|
|
|
if scan:
|
|
|
|
return LibraryScanSerializer(scan).data
|
|
|
|
|
2018-09-06 18:35:02 +00:00
|
|
|
|
|
|
|
class LibraryFollowSerializer(serializers.ModelSerializer):
|
|
|
|
target = common_serializers.RelatedField("uuid", LibrarySerializer(), required=True)
|
2018-09-13 15:18:23 +00:00
|
|
|
actor = serializers.SerializerMethodField()
|
2018-09-06 18:35:02 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = models.LibraryFollow
|
2018-09-13 15:18:23 +00:00
|
|
|
fields = ["creation_date", "actor", "uuid", "target", "approved"]
|
|
|
|
read_only_fields = ["uuid", "actor", "approved", "creation_date"]
|
2018-09-06 18:35:02 +00:00
|
|
|
|
|
|
|
def validate_target(self, v):
|
|
|
|
actor = self.context["actor"]
|
2018-09-24 18:44:22 +00:00
|
|
|
if v.actor == actor:
|
|
|
|
raise serializers.ValidationError("You cannot follow your own library")
|
|
|
|
|
2018-09-06 18:35:02 +00:00
|
|
|
if v.received_follows.filter(actor=actor).exists():
|
|
|
|
raise serializers.ValidationError("You are already following this library")
|
|
|
|
return v
|
2018-09-13 15:18:23 +00:00
|
|
|
|
|
|
|
def get_actor(self, o):
|
|
|
|
return federation_serializers.APIActorSerializer(o.actor).data
|
|
|
|
|
|
|
|
|
|
|
|
def serialize_generic_relation(activity, obj):
|
|
|
|
data = {"uuid": obj.uuid, "type": obj._meta.label}
|
|
|
|
if data["type"] == "music.Library":
|
|
|
|
data["name"] = obj.name
|
|
|
|
if data["type"] == "federation.LibraryFollow":
|
|
|
|
data["approved"] = obj.approved
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
class ActivitySerializer(serializers.ModelSerializer):
|
|
|
|
actor = federation_serializers.APIActorSerializer()
|
|
|
|
object = serializers.SerializerMethodField()
|
|
|
|
target = serializers.SerializerMethodField()
|
|
|
|
related_object = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = models.Activity
|
|
|
|
fields = [
|
|
|
|
"uuid",
|
|
|
|
"fid",
|
|
|
|
"actor",
|
|
|
|
"payload",
|
|
|
|
"object",
|
|
|
|
"target",
|
|
|
|
"related_object",
|
|
|
|
"actor",
|
|
|
|
"creation_date",
|
|
|
|
"type",
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_object(self, o):
|
|
|
|
if o.object:
|
|
|
|
return serialize_generic_relation(o, o.object)
|
|
|
|
|
|
|
|
def get_related_object(self, o):
|
|
|
|
if o.related_object:
|
|
|
|
return serialize_generic_relation(o, o.related_object)
|
|
|
|
|
|
|
|
def get_target(self, o):
|
|
|
|
if o.target:
|
|
|
|
return serialize_generic_relation(o, o.target)
|
|
|
|
|
|
|
|
|
|
|
|
class InboxItemSerializer(serializers.ModelSerializer):
|
|
|
|
activity = ActivitySerializer()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = models.InboxItem
|
|
|
|
fields = ["id", "type", "activity", "is_read"]
|
|
|
|
read_only_fields = ["id", "type", "activity"]
|
|
|
|
|
|
|
|
|
|
|
|
class InboxItemActionSerializer(common_serializers.ActionSerializer):
|
|
|
|
actions = [common_serializers.Action("read", allow_all=True)]
|
|
|
|
filterset_class = filters.InboxItemFilter
|
|
|
|
|
|
|
|
def handle_read(self, objects):
|
|
|
|
return objects.update(is_read=True)
|