2018-09-06 18:35:02 +00:00
|
|
|
import requests.exceptions
|
|
|
|
|
2020-03-02 16:23:03 +00:00
|
|
|
from django.conf import settings
|
2018-09-13 15:18:23 +00:00
|
|
|
from django.db import transaction
|
2020-04-20 15:10:59 +00:00
|
|
|
from django.db.models import Count, Q
|
2018-09-06 18:35:02 +00:00
|
|
|
|
|
|
|
from rest_framework import decorators
|
|
|
|
from rest_framework import mixins
|
2019-04-18 12:37:17 +00:00
|
|
|
from rest_framework import permissions
|
2018-09-06 18:35:02 +00:00
|
|
|
from rest_framework import response
|
|
|
|
from rest_framework import viewsets
|
|
|
|
|
2019-12-26 10:28:12 +00:00
|
|
|
from funkwhale_api.common import preferences
|
2020-03-02 16:23:03 +00:00
|
|
|
from funkwhale_api.common import utils as common_utils
|
2019-12-26 10:28:12 +00:00
|
|
|
from funkwhale_api.common.permissions import ConditionalAuthentication
|
2018-09-06 18:35:02 +00:00
|
|
|
from funkwhale_api.music import models as music_models
|
2020-02-05 14:06:07 +00:00
|
|
|
from funkwhale_api.music import views as music_views
|
2019-03-25 16:02:51 +00:00
|
|
|
from funkwhale_api.users.oauth import permissions as oauth_permissions
|
2018-09-06 18:35:02 +00:00
|
|
|
|
2018-09-13 15:18:23 +00:00
|
|
|
from . import activity
|
2018-09-06 18:35:02 +00:00
|
|
|
from . import api_serializers
|
2019-01-09 16:52:14 +00:00
|
|
|
from . import exceptions
|
2018-09-06 18:35:02 +00:00
|
|
|
from . import filters
|
|
|
|
from . import models
|
|
|
|
from . import routes
|
|
|
|
from . import serializers
|
2020-03-02 16:23:03 +00:00
|
|
|
from . import tasks
|
2018-09-06 18:35:02 +00:00
|
|
|
from . import utils
|
|
|
|
|
|
|
|
|
2018-09-13 15:18:23 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def update_follow(follow, approved):
|
|
|
|
follow.approved = approved
|
|
|
|
follow.save(update_fields=["approved"])
|
2019-03-07 11:09:45 +00:00
|
|
|
if approved:
|
|
|
|
routes.outbox.dispatch({"type": "Accept"}, context={"follow": follow})
|
2020-08-18 18:40:02 +00:00
|
|
|
else:
|
|
|
|
routes.outbox.dispatch({"type": "Reject"}, context={"follow": follow})
|
2018-09-13 15:18:23 +00:00
|
|
|
|
|
|
|
|
2018-09-06 18:35:02 +00:00
|
|
|
class LibraryFollowViewSet(
|
|
|
|
mixins.CreateModelMixin,
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
mixins.RetrieveModelMixin,
|
2018-09-24 18:44:22 +00:00
|
|
|
mixins.DestroyModelMixin,
|
2018-09-06 18:35:02 +00:00
|
|
|
viewsets.GenericViewSet,
|
|
|
|
):
|
|
|
|
lookup_field = "uuid"
|
|
|
|
queryset = (
|
|
|
|
models.LibraryFollow.objects.all()
|
|
|
|
.order_by("-creation_date")
|
2018-09-24 18:44:22 +00:00
|
|
|
.select_related("actor", "target__actor")
|
2018-09-06 18:35:02 +00:00
|
|
|
)
|
|
|
|
serializer_class = api_serializers.LibraryFollowSerializer
|
2019-03-25 16:02:51 +00:00
|
|
|
permission_classes = [oauth_permissions.ScopePermission]
|
|
|
|
required_scope = "follows"
|
2019-01-11 11:03:06 +00:00
|
|
|
filterset_class = filters.LibraryFollowFilter
|
2018-09-06 18:35:02 +00:00
|
|
|
ordering_fields = ("creation_date",)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super().get_queryset()
|
2020-08-18 18:40:02 +00:00
|
|
|
return qs.filter(actor=self.request.user.actor).exclude(approved=False)
|
2018-09-06 18:35:02 +00:00
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
follow = serializer.save(actor=self.request.user.actor)
|
|
|
|
routes.outbox.dispatch({"type": "Follow"}, context={"follow": follow})
|
|
|
|
|
2018-09-24 18:44:22 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def perform_destroy(self, instance):
|
|
|
|
routes.outbox.dispatch(
|
|
|
|
{"type": "Undo", "object": {"type": "Follow"}}, context={"follow": instance}
|
|
|
|
)
|
|
|
|
instance.delete()
|
|
|
|
|
2018-09-06 18:35:02 +00:00
|
|
|
def get_serializer_context(self):
|
|
|
|
context = super().get_serializer_context()
|
|
|
|
context["actor"] = self.request.user.actor
|
|
|
|
return context
|
|
|
|
|
2019-01-11 12:33:35 +00:00
|
|
|
@decorators.action(methods=["post"], detail=True)
|
2018-09-13 15:18:23 +00:00
|
|
|
def accept(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
follow = self.queryset.get(
|
|
|
|
target__actor=self.request.user.actor, uuid=kwargs["uuid"]
|
|
|
|
)
|
|
|
|
except models.LibraryFollow.DoesNotExist:
|
|
|
|
return response.Response({}, status=404)
|
|
|
|
update_follow(follow, approved=True)
|
|
|
|
return response.Response(status=204)
|
|
|
|
|
2019-01-11 12:33:35 +00:00
|
|
|
@decorators.action(methods=["post"], detail=True)
|
2018-09-13 15:18:23 +00:00
|
|
|
def reject(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
follow = self.queryset.get(
|
|
|
|
target__actor=self.request.user.actor, uuid=kwargs["uuid"]
|
|
|
|
)
|
|
|
|
except models.LibraryFollow.DoesNotExist:
|
|
|
|
return response.Response({}, status=404)
|
|
|
|
|
|
|
|
update_follow(follow, approved=False)
|
|
|
|
return response.Response(status=204)
|
|
|
|
|
2020-03-04 21:18:28 +00:00
|
|
|
@decorators.action(methods=["get"], detail=False)
|
|
|
|
def all(self, request, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Return all the subscriptions of the current user, with only limited data
|
|
|
|
to have a performant endpoint and avoid lots of queries just to display
|
|
|
|
subscription status in the UI
|
|
|
|
"""
|
|
|
|
follows = list(
|
|
|
|
self.get_queryset().values_list("uuid", "target__uuid", "approved")
|
|
|
|
)
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
"results": [
|
|
|
|
{"uuid": str(u[0]), "library": str(u[1]), "approved": u[2]}
|
|
|
|
for u in follows
|
|
|
|
],
|
|
|
|
"count": len(follows),
|
|
|
|
}
|
|
|
|
return response.Response(payload, status=200)
|
|
|
|
|
2018-09-06 18:35:02 +00:00
|
|
|
|
|
|
|
class LibraryViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
|
|
|
|
lookup_field = "uuid"
|
|
|
|
queryset = (
|
|
|
|
music_models.Library.objects.all()
|
|
|
|
.order_by("-creation_date")
|
2018-09-28 20:19:43 +00:00
|
|
|
.select_related("actor__user")
|
2018-09-22 12:29:30 +00:00
|
|
|
.annotate(_uploads_count=Count("uploads"))
|
2018-09-06 18:35:02 +00:00
|
|
|
)
|
|
|
|
serializer_class = api_serializers.LibrarySerializer
|
2019-03-25 16:02:51 +00:00
|
|
|
permission_classes = [oauth_permissions.ScopePermission]
|
|
|
|
required_scope = "libraries"
|
2018-09-06 18:35:02 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super().get_queryset()
|
|
|
|
return qs.viewable_by(actor=self.request.user.actor)
|
|
|
|
|
2019-01-11 12:33:35 +00:00
|
|
|
@decorators.action(methods=["post"], detail=True)
|
2018-09-06 18:35:02 +00:00
|
|
|
def scan(self, request, *args, **kwargs):
|
2018-09-24 18:44:22 +00:00
|
|
|
library = self.get_object()
|
2018-09-28 20:19:43 +00:00
|
|
|
if library.actor.get_user():
|
2018-09-24 18:44:22 +00:00
|
|
|
return response.Response({"status": "skipped"}, 200)
|
|
|
|
|
|
|
|
scan = library.schedule_scan(actor=request.user.actor)
|
|
|
|
if scan:
|
|
|
|
return response.Response(
|
|
|
|
{
|
|
|
|
"status": "scheduled",
|
|
|
|
"scan": api_serializers.LibraryScanSerializer(scan).data,
|
|
|
|
},
|
|
|
|
200,
|
|
|
|
)
|
|
|
|
return response.Response({"status": "skipped"}, 200)
|
|
|
|
|
2019-01-11 12:33:35 +00:00
|
|
|
@decorators.action(methods=["post"], detail=False)
|
2018-09-24 18:44:22 +00:00
|
|
|
def fetch(self, request, *args, **kwargs):
|
2018-09-06 18:35:02 +00:00
|
|
|
try:
|
|
|
|
fid = request.data["fid"]
|
|
|
|
except KeyError:
|
|
|
|
return response.Response({"fid": ["This field is required"]})
|
|
|
|
try:
|
2019-01-09 16:52:14 +00:00
|
|
|
library = utils.retrieve_ap_object(
|
2018-09-06 18:35:02 +00:00
|
|
|
fid,
|
2019-03-15 11:08:45 +00:00
|
|
|
actor=request.user.actor,
|
2018-09-06 18:35:02 +00:00
|
|
|
queryset=self.queryset,
|
|
|
|
serializer_class=serializers.LibrarySerializer,
|
|
|
|
)
|
2019-01-09 16:52:14 +00:00
|
|
|
except exceptions.BlockedActorOrDomain:
|
|
|
|
return response.Response(
|
|
|
|
{"detail": "This domain/account is blocked on your instance."},
|
|
|
|
status=400,
|
|
|
|
)
|
2018-09-06 18:35:02 +00:00
|
|
|
except requests.exceptions.RequestException as e:
|
|
|
|
return response.Response(
|
2018-09-24 18:44:22 +00:00
|
|
|
{"detail": "Error while fetching the library: {}".format(str(e))},
|
2018-09-06 18:35:02 +00:00
|
|
|
status=400,
|
|
|
|
)
|
|
|
|
except serializers.serializers.ValidationError as e:
|
|
|
|
return response.Response(
|
|
|
|
{"detail": "Invalid data in remote library: {}".format(str(e))},
|
|
|
|
status=400,
|
|
|
|
)
|
|
|
|
serializer = self.serializer_class(library)
|
|
|
|
return response.Response({"count": 1, "results": [serializer.data]})
|
2018-09-13 15:18:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InboxItemViewSet(
|
|
|
|
mixins.UpdateModelMixin,
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
viewsets.GenericViewSet,
|
|
|
|
):
|
|
|
|
|
|
|
|
queryset = (
|
|
|
|
models.InboxItem.objects.select_related("activity__actor")
|
|
|
|
.prefetch_related("activity__object", "activity__target")
|
|
|
|
.filter(activity__type__in=activity.BROADCAST_TO_USER_ACTIVITIES, type="to")
|
|
|
|
.order_by("-activity__creation_date")
|
|
|
|
)
|
|
|
|
serializer_class = api_serializers.InboxItemSerializer
|
2019-03-25 16:02:51 +00:00
|
|
|
permission_classes = [oauth_permissions.ScopePermission]
|
|
|
|
required_scope = "notifications"
|
2019-01-11 11:03:06 +00:00
|
|
|
filterset_class = filters.InboxItemFilter
|
2018-09-13 15:18:23 +00:00
|
|
|
ordering_fields = ("activity__creation_date",)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super().get_queryset()
|
|
|
|
return qs.filter(actor=self.request.user.actor)
|
|
|
|
|
2019-01-11 12:33:35 +00:00
|
|
|
@decorators.action(methods=["post"], detail=False)
|
2018-09-13 15:18:23 +00:00
|
|
|
def action(self, request, *args, **kwargs):
|
|
|
|
queryset = self.get_queryset()
|
|
|
|
serializer = api_serializers.InboxItemActionSerializer(
|
|
|
|
request.data, queryset=queryset
|
|
|
|
)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
result = serializer.save()
|
|
|
|
return response.Response(result, status=200)
|
2019-04-18 12:37:17 +00:00
|
|
|
|
|
|
|
|
2020-03-02 16:23:03 +00:00
|
|
|
class FetchViewSet(
|
|
|
|
mixins.CreateModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet
|
|
|
|
):
|
2019-04-18 12:37:17 +00:00
|
|
|
|
|
|
|
queryset = models.Fetch.objects.select_related("actor")
|
|
|
|
serializer_class = api_serializers.FetchSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
2020-03-02 16:23:03 +00:00
|
|
|
throttling_scopes = {"create": {"authenticated": "fetch"}}
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().filter(actor=self.request.user.actor)
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
fetch = serializer.save(actor=self.request.user.actor)
|
|
|
|
if fetch.status == "finished":
|
|
|
|
# a duplicate was returned, no need to fetch again
|
|
|
|
return
|
|
|
|
if settings.FEDERATION_SYNCHRONOUS_FETCH:
|
|
|
|
tasks.fetch(fetch_id=fetch.pk)
|
|
|
|
fetch.refresh_from_db()
|
|
|
|
else:
|
|
|
|
common_utils.on_commit(tasks.fetch.delay, fetch_id=fetch.pk)
|
2019-12-26 10:28:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DomainViewSet(
|
|
|
|
mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet
|
|
|
|
):
|
|
|
|
queryset = models.Domain.objects.order_by("name").external()
|
|
|
|
permission_classes = [ConditionalAuthentication]
|
|
|
|
serializer_class = api_serializers.DomainSerializer
|
|
|
|
ordering_fields = ("creation_date", "name")
|
|
|
|
max_page_size = 100
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super().get_queryset()
|
|
|
|
qs = qs.exclude(
|
|
|
|
instance_policy__is_active=True, instance_policy__block_all=True
|
|
|
|
)
|
|
|
|
if preferences.get("moderation__allow_list_enabled"):
|
|
|
|
qs = qs.filter(allowed=True)
|
|
|
|
return qs
|
2020-02-05 14:06:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ActorViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
|
|
|
|
queryset = models.Actor.objects.select_related(
|
|
|
|
"user", "channel", "summary_obj", "attachment_icon"
|
|
|
|
)
|
|
|
|
permission_classes = [ConditionalAuthentication]
|
|
|
|
serializer_class = api_serializers.FullActorSerializer
|
|
|
|
lookup_field = "full_username"
|
|
|
|
lookup_value_regex = r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
queryset = self.get_queryset()
|
|
|
|
username, domain = self.kwargs["full_username"].split("@", 1)
|
|
|
|
return queryset.get(preferred_username=username, domain_id=domain)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super().get_queryset()
|
|
|
|
qs = qs.exclude(
|
|
|
|
domain__instance_policy__is_active=True,
|
|
|
|
domain__instance_policy__block_all=True,
|
|
|
|
)
|
|
|
|
if preferences.get("moderation__allow_list_enabled"):
|
2020-04-20 15:10:59 +00:00
|
|
|
query = Q(domain_id=settings.FUNKWHALE_HOSTNAME) | Q(domain__allowed=True)
|
|
|
|
qs = qs.filter(query)
|
2020-02-05 14:06:07 +00:00
|
|
|
return qs
|
|
|
|
|
|
|
|
libraries = decorators.action(methods=["get"], detail=True)(
|
|
|
|
music_views.get_libraries(
|
|
|
|
filter_uploads=lambda o, uploads: uploads.filter(library__actor=o)
|
|
|
|
)
|
|
|
|
)
|