Verified Commit df88f9c9 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Dispatch hook event when creating listening

parent 3da36273
from rest_framework import serializers
from funkwhale_api import plugins
from funkwhale_api.activity import serializers as activity_serializers
from funkwhale_api.common import utils
from funkwhale_api.federation import serializers as federation_serializers
from funkwhale_api.music.serializers import TrackActivitySerializer, TrackSerializer
from funkwhale_api.users.serializers import UserActivitySerializer, UserBasicSerializer
......@@ -53,4 +55,12 @@ class ListeningWriteSerializer(serializers.ModelSerializer):
def create(self, validated_data):
validated_data["user"] = self.context["user"]
return super().create(validated_data)
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
from funkwhale_api import plugins
from funkwhale_api.federation import serializers as federation_serializers
from funkwhale_api.history import serializers
from funkwhale_api.music import serializers as music_serializers
......@@ -18,3 +19,27 @@ def test_listening_serializer(factories, to_api_date):
serializer = serializers.ListeningSerializer(listening)
assert serializer.data == expected
def test_listening_create(factories, to_api_date, mocker, now):
user = factories["users.User"]()
track = factories["music.Track"]()
payload = {"track": track.pk}
on_commit = mocker.patch("funkwhale_api.common.utils.on_commit")
request = mocker.Mock(plugins_conf=mocker.Mock())
serializer = serializers.ListeningWriteSerializer(
data=payload, context={"request": request, "user": user}
)
assert serializer.is_valid(raise_exception=True) is True
listening = serializer.save()
assert serializer.instance.user == user
assert serializer.instance.track == track
on_commit.assert_called_once_with(
plugins.hooks.dispatch,
"history.listening.created",
listening=listening,
plugins_conf=request.plugins_conf,
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment