serializers.py 2.36 KB
Newer Older
1
2
from rest_framework import serializers

3
from funkwhale_api import plugins
4
from funkwhale_api.activity import serializers as activity_serializers
5
from funkwhale_api.common import utils
Eliot Berriot's avatar
Eliot Berriot committed
6
from funkwhale_api.federation import serializers as federation_serializers
7
from funkwhale_api.music import models as music_models
8
9
from funkwhale_api.music.serializers import TrackActivitySerializer, TrackSerializer
from funkwhale_api.users.serializers import UserActivitySerializer, UserBasicSerializer
10

11
12
13
from . import models


14
15
class ListeningActivitySerializer(activity_serializers.ModelSerializer):
    type = serializers.SerializerMethodField()
Eliot Berriot's avatar
Eliot Berriot committed
16
17
18
    object = TrackActivitySerializer(source="track")
    actor = UserActivitySerializer(source="user")
    published = serializers.DateTimeField(source="creation_date")
19
20
21

    class Meta:
        model = models.Listening
Eliot Berriot's avatar
Eliot Berriot committed
22
        fields = ["id", "local_id", "object", "type", "actor", "published"]
23
24
25
26
27

    def get_actor(self, obj):
        return UserActivitySerializer(obj.user).data

    def get_type(self, obj):
Eliot Berriot's avatar
Eliot Berriot committed
28
        return "Listen"
29
30


31
class ListeningSerializer(serializers.ModelSerializer):
32
33
    track = TrackSerializer(read_only=True)
    user = UserBasicSerializer(read_only=True)
Eliot Berriot's avatar
Eliot Berriot committed
34
    actor = serializers.SerializerMethodField()
35
36
37

    class Meta:
        model = models.Listening
Eliot Berriot's avatar
Eliot Berriot committed
38
        fields = ("id", "user", "track", "creation_date", "actor")
39
40
41
42
43
44

    def create(self, validated_data):
        validated_data["user"] = self.context["user"]

        return super().create(validated_data)

Eliot Berriot's avatar
Eliot Berriot committed
45
46
47
48
49
    def get_actor(self, obj):
        actor = obj.user.actor
        if actor:
            return federation_serializers.APIActorSerializer(actor).data

50
51

class ListeningWriteSerializer(serializers.ModelSerializer):
52
53
    class Meta:
        model = models.Listening
Eliot Berriot's avatar
Eliot Berriot committed
54
        fields = ("id", "user", "track", "creation_date")
55
56

    def create(self, validated_data):
Eliot Berriot's avatar
Eliot Berriot committed
57
        validated_data["user"] = self.context["user"]
58

59
60
61
62
63
64
65
66
67
        instance = super().create(validated_data)
        plugins_conf = getattr(self.context["request"], "plugins_conf", None)
        utils.on_commit(
            plugins.hooks.dispatch,
            "history.listening.created",
            listening=instance,
            plugins_conf=plugins_conf,
        )
        return instance
68
69
70
71
72
73


class NowSerializer(serializers.Serializer):
    track = serializers.PrimaryKeyRelatedField(
        queryset=music_models.Track.objects.all()
    )