diff --git a/backend/models/brain_entity.py b/backend/models/brain_entity.py index bd9eafeefcca..a101af601076 100644 --- a/backend/models/brain_entity.py +++ b/backend/models/brain_entity.py @@ -39,3 +39,4 @@ class PublicBrain(BaseModel): id: UUID name: str description: Optional[str] + number_of_subscribers: int = 0 diff --git a/backend/models/databases/supabase/brains.py b/backend/models/databases/supabase/brains.py index 4402f47f5d8c..5389d0848dff 100644 --- a/backend/models/databases/supabase/brains.py +++ b/backend/models/databases/supabase/brains.py @@ -84,13 +84,13 @@ def get_public_brains(self) -> list[PublicBrain]: ) public_brains: list[PublicBrain] = [] for item in response.data: - public_brains.append( - PublicBrain( - id=item["id"], - name=item["name"], - description=item["description"], - ) + brain = PublicBrain( + id=item["id"], + name=item["name"], + description=item["description"], ) + brain.number_of_subscribers = self.get_brain_subscribers_count(brain.id) + public_brains.append(brain) return public_brains def get_brain_for_user(self, user_id, brain_id) -> MinimalBrainEntity | None: @@ -292,3 +292,16 @@ def get_brain_by_id(self, brain_id: UUID) -> BrainEntity | None: return None return BrainEntity(**response[0]) + + def get_brain_subscribers_count(self, brain_id: UUID) -> int: + response = ( + self.db.from_("brains_users") + .select( + "count", + ) + .filter("brain_id", "eq", str(brain_id)) + .execute() + ).data + if len(response) == 0: + raise ValueError(f"Brain with id {brain_id} does not exist.") + return response[0]["count"] diff --git a/backend/repository/brain/create_brain.py b/backend/repository/brain/create_brain.py index 84d7665aba27..e722dcdcc68e 100644 --- a/backend/repository/brain/create_brain.py +++ b/backend/repository/brain/create_brain.py @@ -1,5 +1,5 @@ -from models.databases.supabase.brains import CreateBrainProperties from models import BrainEntity, get_supabase_db +from models.databases.supabase.brains import CreateBrainProperties def create_brain(brain: CreateBrainProperties) -> BrainEntity: diff --git a/frontend/lib/context/BrainProvider/types.ts b/frontend/lib/context/BrainProvider/types.ts index f46c6e179980..ffc425e356f7 100644 --- a/frontend/lib/context/BrainProvider/types.ts +++ b/frontend/lib/context/BrainProvider/types.ts @@ -37,6 +37,7 @@ export type PublicBrain = { id: UUID; name: string; description?: string; + number_of_subscribers: number; }; export type BrainContextType = ReturnType;