def for_user(self, request: request.Request, **kwargs): distinct_id = request.GET.get("distinct_id", None) if not distinct_id: raise serializers.ValidationError( "Please provide a distinct_id to continue.") flags = get_active_feature_flags(self.team, distinct_id) return Response({"distinct_id": distinct_id, "flags_enabled": flags})
def _ensure_web_feature_flags_in_properties(event: Dict[str, Any], team: Team, distinct_id: str): """If the event comes from web, ensure that it contains property $active_feature_flags.""" if event["properties"].get("$lib") == "web" and not event[ "properties"].get("$active_feature_flags"): event["properties"][ "$active_feature_flags"] = get_active_feature_flags( team, distinct_id)
def _add_missing_feature_flags(properties: Dict, team: Team, distinct_id: str) -> None: # Only add missing feature flags on web if not properties.get("$lib") == "web" or properties.get( "$active_feature_flags"): return properties["$active_feature_flags"] = get_active_feature_flags( team, distinct_id)
def _ensure_web_feature_flags_in_properties(event: Dict[str, Any], team: Team, distinct_id: str): """If the event comes from web, ensure that it contains property $active_feature_flags.""" if event["properties"].get( "$lib" ) == "web" and "$active_feature_flags" not in event["properties"]: flags = get_active_feature_flags(team, distinct_id) event["properties"]["$active_feature_flags"] = list(flags.keys()) for k, v in flags.items(): event["properties"][f"$feature/{k}"] = v
def get_decide(request: HttpRequest): response = { "config": { "enable_collect_everything": True }, "editorParams": {}, "isAuthenticated": False, "supportedCompression": ["gzip", "gzip-js", "lz64"], } if request.COOKIES.get(settings.TOOLBAR_COOKIE_NAME): response["isAuthenticated"] = True if settings.JS_URL: response["editorParams"] = { "jsURL": settings.JS_URL, "toolbarVersion": "toolbar" } if request.user.is_authenticated: r, update_user_token = decide_editor_params(request) response.update(r) if update_user_token: request.user.temporary_token = secrets.token_urlsafe(32) request.user.save() response["featureFlags"] = [] response["sessionRecording"] = False if request.method == "POST": try: data = load_data_from_request(request) except RequestParsingError as error: capture_exception( error ) # We still capture this on Sentry to identify actual potential bugs return cors_response( request, generate_exception_response(f"Malformed request data: {error}", code="malformed_data"), ) token = _get_token(data, request) team = Team.objects.get_team_from_token(token) if team is None and token: project_id = _get_project_id(data, request) if not project_id: return cors_response( request, generate_exception_response( "Project API key invalid. You can find your project API key in PostHog project settings.", code="invalid_api_key", type="authentication_error", status_code=status.HTTP_401_UNAUTHORIZED, ), ) user = User.objects.get_from_personal_api_key(token) if user is None: return cors_response( request, generate_exception_response( "Invalid Personal API key.", code="invalid_personal_key", type="authentication_error", status_code=status.HTTP_401_UNAUTHORIZED, ), ) team = user.teams.get(id=project_id) if team: response["featureFlags"] = get_active_feature_flags( team, data["distinct_id"]) if team.session_recording_opt_in and (on_permitted_domain( team, request) or len(team.app_urls) == 0): response["sessionRecording"] = {"endpoint": "/s/"} return cors_response(request, JsonResponse(response))
def get_decide(request: HttpRequest): response = { "config": { "enable_collect_everything": True }, "editorParams": {}, "isAuthenticated": False, "supportedCompression": ["gzip", "gzip-js", "lz64"], } if request.COOKIES.get(settings.TOOLBAR_COOKIE_NAME): response["isAuthenticated"] = True if settings.JS_URL: response["editorParams"] = { "jsURL": settings.JS_URL, "toolbarVersion": "toolbar" } if request.user.is_authenticated: r, update_user_token = decide_editor_params(request) response.update(r) if update_user_token: request.user.temporary_token = secrets.token_urlsafe(32) request.user.save() response["featureFlags"] = [] response["sessionRecording"] = False if request.method == "POST": try: data_from_request = load_data_from_request(request) data = data_from_request["data"] except (json.decoder.JSONDecodeError, TypeError): return cors_response( request, JsonResponse( { "code": "validation", "message": "Malformed request data. Make sure you're sending valid JSON.", }, status=400, ), ) token = _get_token(data, request) team = Team.objects.get_team_from_token(token) if team is None and token: project_id = _get_project_id(data, request) if not project_id: return cors_response( request, JsonResponse( { "code": "validation", "message": "Project API key invalid. You can find your project API key in PostHog project settings.", }, status=401, ), ) user = User.objects.get_from_personal_api_key(token) if user is None: return cors_response( request, JsonResponse( { "code": "validation", "message": "Personal API key invalid.", }, status=401, ), ) team = user.teams.get(id=project_id) if team: response["featureFlags"] = get_active_feature_flags( team, data_from_request["data"]["distinct_id"]) if team.session_recording_opt_in and (on_permitted_domain( team, request) or len(team.app_urls) == 0): response["sessionRecording"] = {"endpoint": "/s/"} return cors_response(request, JsonResponse(response))