See #170: limit the amount of channels allowed per user

merge-requests/1042/head
Eliot Berriot 2020-02-14 11:50:30 +01:00
rodzic 11a42e362d
commit a3505d2099
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6B501DFD73514E14
5 zmienionych plików z 52 dodań i 2 usunięć

Wyświetl plik

@ -14,3 +14,12 @@ class ChannelsEnabled(types.BooleanPreference):
"If disabled, the channels feature will be completely switched off, "
"and users won't be able to create channels or subscribe to them."
)
@global_preferences_registry.register
class MaxChannels(types.IntegerPreference):
show_in_api = True
section = audio
default = 20
name = "max_channels"
verbose_name = "Max channels allowed per user"

Wyświetl plik

@ -5,6 +5,7 @@ from rest_framework import serializers
from funkwhale_api.common import serializers as common_serializers
from funkwhale_api.common import utils as common_utils
from funkwhale_api.common import locales
from funkwhale_api.common import preferences
from funkwhale_api.federation import serializers as federation_serializers
from funkwhale_api.federation import utils as federation_utils
from funkwhale_api.music import models as music_models
@ -59,6 +60,11 @@ class ChannelCreateSerializer(serializers.Serializer):
metadata = serializers.DictField(required=False)
def validate(self, validated_data):
existing_channels = self.context["actor"].owned_channels.count()
if existing_channels >= preferences.get("audio__max_channels"):
raise serializers.ValidationError(
"You have reached the maximum amount of allowed channels"
)
validated_data = super().validate(validated_data)
metadata = validated_data.pop("metadata", {})
if validated_data["content_category"] == "podcast":

Wyświetl plik

@ -138,6 +138,8 @@ class ChannelViewSet(
"update",
"partial_update",
]
if self.request.user.is_authenticated:
context["actor"] = self.request.user.actor
return context

Wyświetl plik

@ -23,7 +23,9 @@ def test_channel_serializer_create(factories):
"content_category": "other",
}
serializer = serializers.ChannelCreateSerializer(data=data)
serializer = serializers.ChannelCreateSerializer(
data=data, context={"actor": attributed_to}
)
assert serializer.is_valid(raise_exception=True) is True
channel = serializer.save(attributed_to=attributed_to)
@ -49,6 +51,26 @@ def test_channel_serializer_create(factories):
assert channel.library.actor == attributed_to
def test_channel_serializer_create_honor_max_channels_setting(factories, preferences):
preferences["audio__max_channels"] = 1
attributed_to = factories["federation.Actor"](local=True)
factories["audio.Channel"](attributed_to=attributed_to)
data = {
# TODO: cover
"name": "My channel",
"username": "mychannel",
"description": {"text": "This is my channel", "content_type": "text/markdown"},
"tags": ["hello", "world"],
"content_category": "other",
}
serializer = serializers.ChannelCreateSerializer(
data=data, context={"actor": attributed_to}
)
with pytest.raises(serializers.serializers.ValidationError, match=r".*max.*"):
assert serializer.is_valid(raise_exception=True)
def test_channel_serializer_create_podcast(factories):
attributed_to = factories["federation.Actor"](local=True)
@ -62,7 +84,9 @@ def test_channel_serializer_create_podcast(factories):
"metadata": {"itunes_category": "Sports", "language": "en"},
}
serializer = serializers.ChannelCreateSerializer(data=data)
serializer = serializers.ChannelCreateSerializer(
data=data, context={"actor": attributed_to}
)
assert serializer.is_valid(raise_exception=True) is True
channel = serializer.save(attributed_to=attributed_to)

Wyświetl plik

@ -80,6 +80,7 @@ export default {
let instanceLabel = this.$pgettext('Content/Admin/Menu','Instance information')
let usersLabel = this.$pgettext('*/*/*/Noun', 'Users')
let musicLabel = this.$pgettext('*/*/*/Noun', 'Music')
let channelsLabel = this.$pgettext('*/*/*', 'Channels')
let playlistsLabel = this.$pgettext('*/*/*', 'Playlists')
let federationLabel = this.$pgettext('*/*/*', 'Federation')
let moderationLabel = this.$pgettext('*/Moderation/*', 'Moderation')
@ -120,6 +121,14 @@ export default {
{name: "music__transcoding_cache_duration"},
]
},
{
label: channelsLabel,
id: "channels",
settings: [
{name: "audio__channels_enabled"},
{name: "audio__max_channels"},
]
},
{
label: playlistsLabel,
id: "playlists",