Verified Commit 025cebd2 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

WIP

parent 8944c012
......@@ -178,12 +178,13 @@ class ArtistQuerySet(common_models.LocalFromFidQuerySet, models.QuerySet):
return self.annotate(is_playable_by_actor=subquery)
def playable_by(self, actor, include=True):
tracks = Track.objects.playable_by(actor, include)
matches = self.filter(tracks__in=tracks).values_list("pk")
query = get_viewable_by_query(actor, "tracks__uploads__library") & models.Q(
tracks__uploads__import_status="finished"
)
if include:
return self.filter(pk__in=matches)
return self.filter(query)
else:
return self.exclude(pk__in=matches)
return self.exclude(query)
class Artist(APIModelMixin):
......@@ -262,12 +263,13 @@ class AlbumQuerySet(common_models.LocalFromFidQuerySet, models.QuerySet):
return self.annotate(is_playable_by_actor=subquery)
def playable_by(self, actor, include=True):
tracks = Track.objects.playable_by(actor, include)
matches = self.filter(tracks__in=tracks).values_list("pk")
query = get_viewable_by_query(actor, "tracks__uploads__library") & models.Q(
tracks__uploads__import_status="finished"
)
if include:
return self.filter(pk__in=matches)
return self.filter(query)
else:
return self.exclude(pk__in=matches)
return self.exclude(query)
def with_prefetched_tracks_and_playable_uploads(self, actor):
tracks = Track.objects.with_playable_uploads(actor)
......@@ -418,12 +420,13 @@ class TrackQuerySet(common_models.LocalFromFidQuerySet, models.QuerySet):
return self.annotate(is_playable_by_actor=subquery)
def playable_by(self, actor, include=True):
files = Upload.objects.playable_by(actor, include)
matches = self.filter(uploads__in=files).values_list("pk")
query = get_viewable_by_query(actor, "uploads__library") & models.Q(
uploads__import_status="finished"
)
if include:
return self.filter(pk__in=matches)
return self.filter(query)
else:
return self.exclude(pk__in=matches)
return self.exclude(query)
def with_playable_uploads(self, actor):
uploads = Upload.objects.playable_by(actor).select_related("track")
......@@ -598,11 +601,10 @@ class Track(APIModelMixin):
class UploadQuerySet(common_models.NullsLastQuerySet):
def playable_by(self, actor, include=True):
libraries = Library.objects.viewable_by(actor)
query = get_viewable_by_query(actor, "library")
if include:
return self.filter(library__in=libraries, import_status="finished")
return self.exclude(library__in=libraries, import_status="finished")
return self.filter(query, import_status="finished")
return self.exclude(query, import_status="finished")
def local(self, include=True):
return self.exclude(library__actor__user__isnull=include)
......@@ -1025,6 +1027,37 @@ LIBRARY_PRIVACY_LEVEL_CHOICES = [
]
def prefixed_query(path, **kwargs):
if not path:
return models.Q(**kwargs)
prefixed_kwargs = {
"{}__{}".format(path, key): value for key, value in kwargs.items()
}
return models.Q(**prefixed_kwargs)
def get_viewable_by_query(actor, path=None):
from funkwhale_api.federation.models import LibraryFollow
if actor is None:
return prefixed_query(path, privacy_level="everyone")
me_query = prefixed_query(path, privacy_level="me", actor=actor)
instance_query = prefixed_query(
path, privacy_level="instance", actor__domain=actor.domain
)
followed_libraries = LibraryFollow.objects.filter(
actor=actor, approved=True
).values_list("target", flat=True)
return (
me_query
| instance_query
| prefixed_query(path, privacy_level="everyone")
| prefixed_query(path, pk__in=followed_libraries)
)
class LibraryQuerySet(models.QuerySet):
def with_follows(self, actor):
return self.prefetch_related(
......@@ -1036,22 +1069,7 @@ class LibraryQuerySet(models.QuerySet):
)
def viewable_by(self, actor):
from funkwhale_api.federation.models import LibraryFollow
if actor is None:
return Library.objects.filter(privacy_level="everyone")
me_query = models.Q(privacy_level="me", actor=actor)
instance_query = models.Q(privacy_level="instance", actor__domain=actor.domain)
followed_libraries = LibraryFollow.objects.filter(
actor=actor, approved=True
).values_list("target", flat=True)
return Library.objects.filter(
me_query
| instance_query
| models.Q(privacy_level="everyone")
| models.Q(pk__in=followed_libraries)
)
return self.filter(get_viewable_by_query(actor))
class Library(federation_models.FederationMixin):
......
This diff is collapsed.
Supports Markdown
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