mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-07 21:29:13 +00:00
Simplify get_conversation_by_user DB adapter code
This commit is contained in:
@@ -680,34 +680,18 @@ class ConversationAdapters:
|
|||||||
async def aget_conversation_by_user(
|
async def aget_conversation_by_user(
|
||||||
user: KhojUser, client_application: ClientApplication = None, conversation_id: int = None, title: str = None
|
user: KhojUser, client_application: ClientApplication = None, conversation_id: int = None, title: str = None
|
||||||
) -> Optional[Conversation]:
|
) -> Optional[Conversation]:
|
||||||
|
query = Conversation.objects.filter(user=user, client=client_application).prefetch_related("agent")
|
||||||
|
|
||||||
if conversation_id:
|
if conversation_id:
|
||||||
return (
|
return await query.filter(id=conversation_id).afirst()
|
||||||
await Conversation.objects.filter(user=user, client=client_application, id=conversation_id)
|
|
||||||
.prefetch_related("agent")
|
|
||||||
.afirst()
|
|
||||||
)
|
|
||||||
elif title:
|
elif title:
|
||||||
return (
|
return await query.filter(title=title).afirst()
|
||||||
await Conversation.objects.filter(user=user, client=client_application, title=title)
|
|
||||||
.prefetch_related("agent")
|
|
||||||
.afirst()
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
conversation = (
|
|
||||||
Conversation.objects.filter(user=user, client=client_application)
|
|
||||||
.prefetch_related("agent")
|
|
||||||
.order_by("-updated_at")
|
|
||||||
)
|
|
||||||
|
|
||||||
if await conversation.aexists():
|
conversation = await query.order_by("-updated_at").afirst()
|
||||||
return await conversation.prefetch_related("agent").afirst()
|
|
||||||
|
|
||||||
return await (
|
return conversation or await Conversation.objects.prefetch_related("agent").acreate(
|
||||||
Conversation.objects.filter(user=user, client=client_application)
|
user=user, client=client_application
|
||||||
.prefetch_related("agent")
|
)
|
||||||
.order_by("-updated_at")
|
|
||||||
.afirst()
|
|
||||||
) or await Conversation.objects.prefetch_related("agent").acreate(user=user, client=client_application)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def adelete_conversation_by_user(
|
async def adelete_conversation_by_user(
|
||||||
|
|||||||
Reference in New Issue
Block a user