Skip to content

Commit

Permalink
add: disc/is_public to meta data
Browse files Browse the repository at this point in the history
  • Loading branch information
statefb committed Dec 3, 2023
1 parent 90af829 commit fe91c6c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 10 deletions.
12 changes: 11 additions & 1 deletion backend/app/repositories/custom_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ def find_private_bots_by_user_id(
owned=True,
available=True,
is_pinned=item["IsPinned"],
description=item["Description"],
is_public="PublicBotId" in item,
)
for item in response["Items"]
]
Expand All @@ -215,6 +217,8 @@ def find_private_bots_by_user_id(
owned=True,
available=True,
is_pinned=item["IsPinned"],
description=item["Description"],
is_public="PublicBotId" in item,
)
for item in response["Items"]
]
Expand Down Expand Up @@ -280,6 +284,8 @@ def find_all_bots_by_user_id(
is_pinned=item["IsPinned"],
owned=False,
available=True,
description=bot.description,
is_public=True,
)
except RecordNotFoundError:
# Original bot is removed
Expand All @@ -294,6 +300,8 @@ def find_all_bots_by_user_id(
owned=False,
# NOTE: Original bot is removed
available=False,
description="This item is no longer available",
is_public=False,
)

if is_original_available and bot.title != item["Title"]:
Expand Down Expand Up @@ -323,6 +331,8 @@ def find_all_bots_by_user_id(
is_pinned=item["IsPinned"],
owned=True,
available=True,
description=item["Description"],
is_public=False,
)
)

Expand Down Expand Up @@ -352,7 +362,7 @@ def find_private_bot_by_id(user_id: str, bot_id: str) -> BotModel:
create_time=float(item["CreateTime"]),
last_used_time=float(item["LastBotUsed"]),
is_pinned=item["IsPinned"],
public_bot_id=None,
public_bot_id=None if "PublicBotId" not in item else item["PublicBotId"],
)

logger.debug(f"Found bot: {bot}")
Expand Down
2 changes: 2 additions & 0 deletions backend/app/repositories/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ class ConversationMeta(BaseModel):
class BotMeta(BaseModel):
id: str
title: str
description: str
create_time: float
last_used_time: float
is_pinned: bool
is_public: bool
# Whether the bot is owned by the user
owned: bool
# Whether the bot is available or not.
Expand Down
7 changes: 4 additions & 3 deletions backend/app/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ def get_all_bots(
is_pinned=bot.is_pinned,
owned=True,
available=True,
description=bot.description,
is_public=bot.is_public,
)
for bot in bots
]
Expand All @@ -225,16 +227,15 @@ def get_bot(request: Request, bot_id: str):
"""Get bot by id. This returns both owned private and shared public bots."""
current_user: User = request.state.current_user

is_public, bot = fetch_bot(current_user.id, bot_id)
owned = not is_public
owned, bot = fetch_bot(current_user.id, bot_id)
output = BotOutput(
id=bot.id,
title=bot.title,
instruction=bot.instruction,
description=bot.description,
create_time=bot.create_time,
last_used_time=bot.last_used_time,
is_public=bot.public_bot_id is not None,
is_public=True if bot.public_bot_id else False,
is_pinned=bot.is_pinned,
owned=owned,
)
Expand Down
2 changes: 2 additions & 0 deletions backend/app/route_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ class BotOutput(BaseSchema):
class BotMetaOutput(BaseSchema):
id: str
title: str
description: str
create_time: float
last_used_time: float
is_pinned: bool
is_public: bool
owned: bool
# Whether the bot is available or not.
# This can be `False` if the bot is not owned by the user and original bot is removed.
Expand Down
6 changes: 3 additions & 3 deletions backend/app/usecases/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ def modify_owned_bot(

def fetch_bot(user_id: str, bot_id: str) -> tuple[bool, BotModel]:
"""Fetch bot by id.
The first element of the returned tuple is whether the bot is public or not.
The first element of the returned tuple is whether the bot is owned or not.
`True` means the bot is public.
"""
try:
return False, find_private_bot_by_id(user_id, bot_id)
return True, find_private_bot_by_id(user_id, bot_id)
except RecordNotFoundError:
pass #

try:
return True, find_public_bot_by_id(bot_id)
return False, find_public_bot_by_id(bot_id)
except RecordNotFoundError:
raise RecordNotFoundError(
f"Bot with ID {bot_id} not found in both private (for user {user_id}) and public items."
Expand Down
9 changes: 6 additions & 3 deletions backend/tests/repositories/test_custom_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def test_store_and_find_bot(self):
self.assertEqual(bot[0].create_time, 1627984879.9)
self.assertEqual(bot[0].last_used_time, 1627984879.9)
self.assertEqual(bot[0].is_pinned, False)
self.assertEqual(bot[0].is_pinned, False)
self.assertEqual(bot[0].description, "Test Bot Description")
self.assertEqual(bot[0].is_public, False)

delete_bot_by_id("user1", "1")
bot = find_private_bots_by_user_id("user1")
Expand Down Expand Up @@ -253,7 +256,7 @@ def test_order_is_descending(self):
self.assertEqual(bots[3].id, "1")

# Should be alias2 -> 4 -> 2 -> alias1 -> 3 -> 1
bots = find_all_bots_by_user_id("user1")
bots = find_all_bots_by_user_id("user1", limit=6)
self.assertEqual(len(bots), 6)
self.assertEqual(bots[0].id, "public2")
self.assertEqual(bots[1].id, "4")
Expand Down Expand Up @@ -320,7 +323,7 @@ def test_update_bot_visibility(self):
update_bot(
"user2", "public1", title="Updated Title", description="", instruction=""
)
bots = find_all_bots_by_user_id("user1")
bots = find_all_bots_by_user_id("user1", limit=3)
self.assertEqual(len(bots), 3)
self.assertEqual(bots[0].id, "1")
self.assertEqual(bots[1].id, "2")
Expand All @@ -330,7 +333,7 @@ def test_update_bot_visibility(self):

# Make private
update_bot_visibility("user2", "public1", False)
bots = find_all_bots_by_user_id("user1")
bots = find_all_bots_by_user_id("user1", limit=3)
self.assertEqual(len(bots), 3)
self.assertEqual(bots[0].id, "1")
self.assertEqual(bots[1].id, "2")
Expand Down
2 changes: 2 additions & 0 deletions cdk/lib/constructs/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export class Database extends Construct {
"LastBotUsed",
"OriginalBotId",
"IsPinned",
"Description",
"PublicBotId",
],
});

Expand Down

0 comments on commit fe91c6c

Please sign in to comment.