🔧 fix(store.py): set liked_by_user flag to True for components liked by the user

🔧 fix(schema.py): add liked_by_user field to ListComponentResponse schema
🔧 fix(service.py): add get_liked_by_user_components method to StoreService to retrieve liked components by user
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-10-31 19:58:10 -03:00
commit e64d0fcf03
3 changed files with 36 additions and 0 deletions

View file

@ -84,6 +84,16 @@ def list_components(
limit=limit,
filter_by_user=filter_by_user,
)
# 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 to True
for component in result:
if str(component.id) in liked_by_user_ids:
component.liked_by_user = True
return result
except Exception as exc:

View file

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

View file

@ -1,6 +1,7 @@
from datetime import datetime
import json
from uuid import UUID
from fastapi import params
from langflow.services.base import Service
from typing import TYPE_CHECKING, List, Dict, Any, Optional, Union
import httpx
@ -169,6 +170,7 @@ class StoreService(Service):
# Only public components or the ones created by the user
# check for "public" or "Public"
if filter_by_user and not api_key:
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]
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:
url = f"{self.components_url}/{component_id}"
params = {