See #170: filtering subscribed channels in API

merge-requests/1042/head
Eliot Berriot 2020-01-15 14:39:50 +01:00
rodzic 4236cc6274
commit 7ae0b23c15
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6B501DFD73514E14
2 zmienionych plików z 53 dodań i 1 usunięć

Wyświetl plik

@ -1,3 +1,5 @@
from django.db.models import Q
import django_filters import django_filters
from funkwhale_api.common import fields from funkwhale_api.common import fields
@ -23,12 +25,30 @@ class ChannelFilter(moderation_filters.HiddenContentFilterSet):
) )
tag = TAG_FILTER tag = TAG_FILTER
scope = common_filters.ActorScopeFilter(actor_field="attributed_to", distinct=True) scope = common_filters.ActorScopeFilter(actor_field="attributed_to", distinct=True)
subscribed = django_filters.BooleanFilter(
field_name="_", method="filter_subscribed"
)
class Meta: class Meta:
model = models.Channel model = models.Channel
fields = ["q", "scope", "tag"] fields = ["q", "scope", "tag", "subscribed"]
hidden_content_fields_mapping = moderation_filters.USER_FILTER_CONFIG["CHANNEL"] hidden_content_fields_mapping = moderation_filters.USER_FILTER_CONFIG["CHANNEL"]
def filter_subscribed(self, queryset, name, value):
if not self.request.user.is_authenticated:
return queryset.none()
emitted_follows = self.request.user.actor.emitted_follows.exclude(
target__channel__isnull=True
)
query = Q(actor__in=emitted_follows.values_list("target", flat=True))
if value is True:
return queryset.filter(query)
else:
return queryset.exclude(query)
class IncludeChannelsFilterSet(django_filters.FilterSet): class IncludeChannelsFilterSet(django_filters.FilterSet):
""" """

Wyświetl plik

@ -0,0 +1,32 @@
from funkwhale_api.audio import filters
from funkwhale_api.audio import models
def test_channel_filter_subscribed_true(factories, mocker, queryset_equal_list):
user = factories["users.User"](with_actor=True)
channel = factories["audio.Channel"]()
other_channel = factories["audio.Channel"]()
factories["audio.Subscription"](target=channel.actor, actor=user.actor)
factories["audio.Subscription"](target=other_channel.actor)
qs = models.Channel.objects.all()
filterset = filters.ChannelFilter(
{"subscribed": "true"}, request=mocker.Mock(user=user), queryset=qs
)
assert filterset.qs == [channel]
def test_channel_filter_subscribed_false(factories, mocker, queryset_equal_list):
user = factories["users.User"](with_actor=True)
channel = factories["audio.Channel"]()
other_channel = factories["audio.Channel"]()
factories["audio.Subscription"](target=channel.actor, actor=user.actor)
factories["audio.Subscription"](target=other_channel.actor)
qs = models.Channel.objects.all()
filterset = filters.ChannelFilter(
{"subscribed": "false"}, request=mocker.Mock(user=user), queryset=qs
)
assert filterset.qs == [other_channel]