Skip to content
Snippets Groups Projects
nodeinfo.py 3.89 KiB
Newer Older
import memoize.djangocache

import funkwhale_api
from funkwhale_api.common import preferences
from funkwhale_api.federation import actors, models as federation_models
Eliot Berriot's avatar
Eliot Berriot committed
from funkwhale_api.federation import utils as federation_utils
Eliot Berriot's avatar
Eliot Berriot committed
from funkwhale_api.moderation import models as moderation_models
from funkwhale_api.music import utils as music_utils
Eliot Berriot's avatar
Eliot Berriot committed
store = memoize.djangocache.Cache("default")
memo = memoize.Memoizer(store, namespace="instance:stats")
    all_preferences = preferences.all()
    share_stats = all_preferences.get("instance__nodeinfo_stats_enabled")
    allow_list_enabled = all_preferences.get("moderation__allow_list_enabled")
    allow_list_public = all_preferences.get("moderation__allow_list_public")
Eliot Berriot's avatar
Eliot Berriot committed
    banner = all_preferences.get("instance__banner")
    unauthenticated_report_types = all_preferences.get(
Eliot Berriot's avatar
Eliot Berriot committed
        "moderation__unauthenticated_report_types"
    )
    if allow_list_enabled and allow_list_public:
        allowed_domains = list(
            federation_models.Domain.objects.filter(allowed=True)
            .order_by("name")
            .values_list("name", flat=True)
        )
    else:
        allowed_domains = None
Eliot Berriot's avatar
Eliot Berriot committed
        "version": "2.0",
        "software": {"name": "funkwhale", "version": funkwhale_api.__version__},
        "protocols": ["activitypub"],
        "services": {"inbound": [], "outbound": []},
        "openRegistrations": all_preferences.get("users__registration_enabled"),
        "usage": {"users": {"total": 0, "activeHalfyear": 0, "activeMonth": 0}},
Eliot Berriot's avatar
Eliot Berriot committed
        "metadata": {
Eliot Berriot's avatar
Eliot Berriot committed
            "actorId": actors.get_service_actor().fid,
            "private": all_preferences.get("instance__nodeinfo_private"),
            "shortDescription": all_preferences.get("instance__short_description"),
            "longDescription": all_preferences.get("instance__long_description"),
            "rules": all_preferences.get("instance__rules"),
            "contactEmail": all_preferences.get("instance__contact_email"),
            "terms": all_preferences.get("instance__terms"),
            "nodeName": all_preferences.get("instance__name"),
Eliot Berriot's avatar
Eliot Berriot committed
            "banner": federation_utils.full_url(banner.url) if banner else None,
            "defaultUploadQuota": all_preferences.get("users__upload_quota"),
Eliot Berriot's avatar
Eliot Berriot committed
            "library": {
                "federationEnabled": all_preferences.get("federation__enabled"),
                "federationNeedsApproval": all_preferences.get(
Eliot Berriot's avatar
Eliot Berriot committed
                    "federation__music_needs_approval"
                ),
                "anonymousCanListen": not all_preferences.get(
Eliot Berriot's avatar
Eliot Berriot committed
                    "common__api_authentication_required"
                ),
            "supportedUploadExtensions": music_utils.SUPPORTED_EXTENSIONS,
            "allowList": {"enabled": allow_list_enabled, "domains": allowed_domains},
Eliot Berriot's avatar
Eliot Berriot committed
            "reportTypes": [
                {"type": t, "label": l, "anonymous": t in unauthenticated_report_types}
                for t, l in moderation_models.REPORT_TYPES
            ],
Eliot Berriot's avatar
Eliot Berriot committed
        },
Eliot Berriot's avatar
Eliot Berriot committed
        getter = memo(lambda: stats.get(), max_age=600)
        statistics = getter()
        data["usage"]["users"]["total"] = statistics["users"]["total"]
        data["usage"]["users"]["activeHalfyear"] = statistics["users"][
            "active_halfyear"
        ]
        data["usage"]["users"]["activeMonth"] = statistics["users"]["active_month"]
Eliot Berriot's avatar
Eliot Berriot committed
        data["metadata"]["library"]["tracks"] = {"total": statistics["tracks"]}
        data["metadata"]["library"]["artists"] = {"total": statistics["artists"]}
        data["metadata"]["library"]["albums"] = {"total": statistics["albums"]}
        data["metadata"]["library"]["music"] = {"hours": statistics["music_duration"]}
Eliot Berriot's avatar
Eliot Berriot committed
        data["metadata"]["usage"] = {
            "favorites": {"tracks": {"total": statistics["track_favorites"]}},
            "listenings": {"total": statistics["listenings"]},