Merge branch 'feature/store' of github.com:logspace-ai/langflow into feature/store

This commit is contained in:
cristhianzl 2023-11-01 09:12:01 -03:00
commit 6799007514
3 changed files with 40 additions and 1 deletions

View file

@ -75,7 +75,7 @@ def list_components(
page: int = 1, page: int = 1,
limit: int = 10, limit: int = 10,
store_service: StoreService = Depends(get_store_service), store_service: StoreService = Depends(get_store_service),
store_api_Key: str = Depends(get_optional_user_store_api_key), store_api_Key: Optional[str] = Depends(get_optional_user_store_api_key),
): ):
try: try:
result = store_service.query_components( result = store_service.query_components(
@ -85,6 +85,19 @@ def list_components(
filter_by_user=filter_by_user, filter_by_user=filter_by_user,
) )
if not store_api_Key:
return result
# Now, from the result, we need to get the components
# the user likes and set the liked_by_user to True
liked_by_user_ids = store_service.get_liked_by_user_components(
component_ids=[str(component.id) for component in result],
api_key=store_api_Key,
)
# Now we need to set the liked_by_user attribute
for component in result:
component.liked_by_user = str(component.id) in liked_by_user_ids
return result return result
except Exception as exc: except Exception as exc:
raise HTTPException(status_code=400, detail=str(exc)) raise HTTPException(status_code=400, detail=str(exc))

View file

@ -42,6 +42,7 @@ class ListComponentResponse(BaseModel):
name: Optional[str] name: Optional[str]
description: Optional[str] description: Optional[str]
liked_by_count: Optional[int] liked_by_count: Optional[int]
liked_by_user: Optional[bool]
is_component: Optional[bool] is_component: Optional[bool]
metadata: Optional[dict] metadata: Optional[dict]
user_created: Optional[dict] user_created: Optional[dict]

View file

@ -1,6 +1,7 @@
from datetime import datetime from datetime import datetime
import json import json
from uuid import UUID from uuid import UUID
from fastapi import params
from langflow.services.base import Service from langflow.services.base import Service
from typing import TYPE_CHECKING, List, Dict, Any, Optional, Union from typing import TYPE_CHECKING, List, Dict, Any, Optional, Union
import httpx import httpx
@ -169,6 +170,7 @@ class StoreService(Service):
# Only public components or the ones created by the user # Only public components or the ones created by the user
# check for "public" or "Public" # check for "public" or "Public"
if filter_by_user and not api_key: if filter_by_user and not api_key:
raise ValueError("No API key provided") raise ValueError("No API key provided")
@ -190,6 +192,29 @@ class StoreService(Service):
# component.tags = [tags_id.tags_id for tags_id in component.tags] # component.tags = [tags_id.tags_id for tags_id in component.tags]
return results_objects return results_objects
def get_liked_by_user_components(
self, component_ids: List[UUID], api_key: str
) -> List[UUID]:
# Get fields id
# filter should be "id is in component_ids AND liked_by directus_users_id token is api_key"
# return the ids
user_data = self._get(
f"{self.base_url}/users/me", api_key, params={"fields": "id"}
)
params = {
"fields": "id",
"filter": json.dumps(
{
"_and": [
{"id": {"_in": component_ids}},
{"liked_by": {"directus_users_id": {"_eq": user_data["id"]}}},
]
}
),
}
results = self._get(self.components_url, api_key, params)
return [result["id"] for result in results]
def download(self, api_key: str, component_id: str) -> DownloadComponentResponse: def download(self, api_key: str, component_id: str) -> DownloadComponentResponse:
url = f"{self.components_url}/{component_id}" url = f"{self.components_url}/{component_id}"
params = { params = {