def library_library(request, uuid, redirect_to_ap): queryset = models.Library.objects.filter(uuid=uuid) try: obj = queryset.get() except models.Library.DoesNotExist: return [] if redirect_to_ap: raise middleware.ApiRedirect(obj.fid) library_url = utils.join_url( settings.FUNKWHALE_URL, utils.spa_reverse("library_library", kwargs={"uuid": obj.uuid}), ) metas = [ {"tag": "meta", "property": "og:url", "content": library_url}, {"tag": "meta", "property": "og:type", "content": "website"}, {"tag": "meta", "property": "og:title", "content": obj.name}, {"tag": "meta", "property": "og:description", "content": obj.description}, ] if preferences.get("federation__enabled"): metas.append( { "tag": "link", "rel": "alternate", "type": "application/activity+json", "href": obj.fid, } ) return metas
def actor_detail_username(request, username, redirect_to_ap): validator = federation_utils.get_actor_data_from_username try: username_data = validator(username) except serializers.ValidationError: return [] queryset = (models.Actor.objects.filter( preferred_username__iexact=username_data["username"]).local(). select_related("attachment_icon")) try: obj = queryset.get() except models.Actor.DoesNotExist: return [] if redirect_to_ap: raise middleware.ApiRedirect(obj.fid) obj_url = utils.join_url( settings.FUNKWHALE_URL, utils.spa_reverse("actor_detail", kwargs={"username": obj.preferred_username}), ) metas = [ { "tag": "meta", "property": "og:url", "content": obj_url }, { "tag": "meta", "property": "og:title", "content": obj.display_name }, { "tag": "meta", "property": "og:type", "content": "profile" }, ] if obj.attachment_icon: metas.append({ "tag": "meta", "property": "og:image", "content": obj.attachment_icon.download_url_medium_square_crop, }) if preferences.get("federation__enabled"): metas.append({ "tag": "link", "rel": "alternate", "type": "application/activity+json", "href": obj.fid, }) return metas
def test_spa_middleware_handles_api_redirect(mocker): get_response = mocker.Mock(return_value=mocker.Mock(status_code=404)) redirect_url = "/test" mocker.patch.object(middleware, "serve_spa", side_effect=middleware.ApiRedirect(redirect_url)) api_view = mocker.Mock() match = mocker.Mock(args=["hello"], kwargs={"foo": "bar"}, func=api_view) mocker.patch.object(middleware.urls, "resolve", return_value=match) request = mocker.Mock(path="/") m = middleware.SPAFallbackMiddleware(get_response) response = m(request) api_view.assert_called_once_with(request, "hello", foo="bar") assert response == api_view.return_value
def library_track(request, pk, redirect_to_ap): queryset = models.Track.objects.filter(pk=pk).select_related("album", "artist") try: obj = queryset.get() except models.Track.DoesNotExist: return [] playable_uploads = obj.uploads.playable_by(None).order_by("id") upload = playable_uploads.first() if redirect_to_ap: redirect_url = upload.fid if upload else obj.fid raise middleware.ApiRedirect(redirect_url) track_url = utils.join_url( settings.FUNKWHALE_URL, utils.spa_reverse("library_track", kwargs={"pk": obj.pk}), ) metas = [ {"tag": "meta", "property": "og:url", "content": track_url}, {"tag": "meta", "property": "og:title", "content": obj.title}, {"tag": "meta", "property": "og:type", "content": "music.song"}, {"tag": "meta", "property": "music:album:disc", "content": obj.disc_number}, {"tag": "meta", "property": "music:album:track", "content": obj.position}, { "tag": "meta", "property": "music:musician", "content": utils.join_url( settings.FUNKWHALE_URL, utils.spa_reverse("library_artist", kwargs={"pk": obj.artist.pk}), ), }, ] if obj.album: metas.append( { "tag": "meta", "property": "music:album", "content": utils.join_url( settings.FUNKWHALE_URL, utils.spa_reverse("library_album", kwargs={"pk": obj.album.pk}), ), }, ) if obj.attachment_cover: metas.append( { "tag": "meta", "property": "og:image", "content": obj.attachment_cover.download_url_medium_square_crop, } ) elif obj.album and obj.album.attachment_cover: metas.append( { "tag": "meta", "property": "og:image", "content": obj.album.attachment_cover.download_url_medium_square_crop, } ) if upload: metas.append( { "tag": "meta", "property": "og:audio", "content": utils.join_url(settings.FUNKWHALE_URL, obj.listen_url), } ) if preferences.get("federation__enabled"): metas.append( { "tag": "link", "rel": "alternate", "type": "application/activity+json", "href": upload.fid, } ) metas.append( { "tag": "link", "rel": "alternate", "type": "application/json+oembed", "href": ( utils.join_url(settings.FUNKWHALE_URL, reverse("api:v1:oembed")) + "?format=json&url={}".format(urllib.parse.quote_plus(track_url)) ), } ) # twitter player is also supported in various software metas += get_twitter_card_metas(type="track", id=obj.pk) return metas
def library_artist(request, pk, redirect_to_ap): queryset = models.Artist.objects.filter(pk=pk) try: obj = queryset.get() except models.Artist.DoesNotExist: return [] if redirect_to_ap: raise middleware.ApiRedirect(obj.fid) artist_url = utils.join_url( settings.FUNKWHALE_URL, utils.spa_reverse("library_artist", kwargs={"pk": obj.pk}), ) # we use latest album's cover as artist image latest_album = ( obj.albums.exclude(attachment_cover=None).order_by("release_date").last() ) metas = [ {"tag": "meta", "property": "og:url", "content": artist_url}, {"tag": "meta", "property": "og:title", "content": obj.name}, {"tag": "meta", "property": "og:type", "content": "profile"}, ] if latest_album and latest_album.attachment_cover: metas.append( { "tag": "meta", "property": "og:image", "content": latest_album.attachment_cover.download_url_medium_square_crop, } ) if preferences.get("federation__enabled"): metas.append( { "tag": "link", "rel": "alternate", "type": "application/activity+json", "href": obj.fid, } ) if ( models.Upload.objects.filter(Q(track__artist=obj) | Q(track__album__artist=obj)) .playable_by(None) .exists() ): metas.append( { "tag": "link", "rel": "alternate", "type": "application/json+oembed", "href": ( utils.join_url(settings.FUNKWHALE_URL, reverse("api:v1:oembed")) + "?format=json&url={}".format(urllib.parse.quote_plus(artist_url)) ), } ) # twitter player is also supported in various software metas += get_twitter_card_metas(type="artist", id=obj.pk) return metas
def library_album(request, pk, redirect_to_ap): queryset = models.Album.objects.filter(pk=pk).select_related("artist") try: obj = queryset.get() except models.Album.DoesNotExist: return [] if redirect_to_ap: raise middleware.ApiRedirect(obj.fid) album_url = utils.join_url( settings.FUNKWHALE_URL, utils.spa_reverse("library_album", kwargs={"pk": obj.pk}), ) metas = [ {"tag": "meta", "property": "og:url", "content": album_url}, {"tag": "meta", "property": "og:title", "content": obj.title}, {"tag": "meta", "property": "og:type", "content": "music.album"}, { "tag": "meta", "property": "music:musician", "content": utils.join_url( settings.FUNKWHALE_URL, utils.spa_reverse("library_artist", kwargs={"pk": obj.artist.pk}), ), }, ] if obj.release_date: metas.append( { "tag": "meta", "property": "music:release_date", "content": str(obj.release_date), } ) if obj.attachment_cover: metas.append( { "tag": "meta", "property": "og:image", "content": obj.attachment_cover.download_url_medium_square_crop, } ) if preferences.get("federation__enabled"): metas.append( { "tag": "link", "rel": "alternate", "type": "application/activity+json", "href": obj.fid, } ) if models.Upload.objects.filter(track__album=obj).playable_by(None).exists(): metas.append( { "tag": "link", "rel": "alternate", "type": "application/json+oembed", "href": ( utils.join_url(settings.FUNKWHALE_URL, reverse("api:v1:oembed")) + "?format=json&url={}".format(urllib.parse.quote_plus(album_url)) ), } ) # twitter player is also supported in various software metas += get_twitter_card_metas(type="album", id=obj.pk) return metas
def channel_detail(query, redirect_to_ap): queryset = models.Channel.objects.filter(query).select_related( "artist__attachment_cover", "actor", "library") try: obj = queryset.get() except models.Channel.DoesNotExist: return [] if redirect_to_ap: raise middleware.ApiRedirect(obj.actor.fid) obj_url = utils.join_url( settings.FUNKWHALE_URL, utils.spa_reverse("channel_detail", kwargs={"username": obj.actor.full_username}), ) metas = [ { "tag": "meta", "property": "og:url", "content": obj_url }, { "tag": "meta", "property": "og:title", "content": obj.artist.name }, { "tag": "meta", "property": "og:type", "content": "profile" }, ] if obj.artist.attachment_cover: metas.append({ "tag": "meta", "property": "og:image", "content": obj.artist.attachment_cover.download_url_medium_square_crop, }) if preferences.get("federation__enabled"): metas.append({ "tag": "link", "rel": "alternate", "type": "application/activity+json", "href": obj.actor.fid, }) metas.append( { "tag": "link", "rel": "alternate", "type": "application/rss+xml", "href": obj.get_rss_url(), "title": "{} - RSS Podcast Feed".format(obj.artist.name), }, ) if obj.library.uploads.all().playable_by(None).exists(): metas.append({ "tag": "link", "rel": "alternate", "type": "application/json+oembed", "href": (utils.join_url(settings.FUNKWHALE_URL, reverse("api:v1:oembed")) + "?format=json&url={}".format(urllib.parse.quote_plus(obj_url))), }) # twitter player is also supported in various software metas += spa_views.get_twitter_card_metas(type="channel", id=obj.uuid) return metas