mirror of
https://github.com/dartdavros/chatballs.git
synced 2026-10-05 09:14:58 +03:00
28 lines
1.1 KiB
Python
28 lines
1.1 KiB
Python
from django.db.models import F, Max, QuerySet
|
|
|
|
from hub_platform.conversations.models import Conversation
|
|
from hub_platform.tenancy.context import TenantContext
|
|
|
|
|
|
def conversations_for_context(context: TenantContext) -> QuerySet[Conversation]:
|
|
return (
|
|
Conversation.objects.filter(organization_id=context.organization_id)
|
|
.select_related(
|
|
"channel",
|
|
"channel__product",
|
|
"contact",
|
|
"connection",
|
|
"assigned_operator",
|
|
"support_identity_snapshot",
|
|
)
|
|
# Инбокс сортируется по времени последнего сообщения (а не по служебной
|
|
# активности вроде claim/takeover); fallback — last_activity_at для
|
|
# диалогов без сообщений.
|
|
.annotate(_last_message_at=Max("messages__created_at"))
|
|
.order_by(F("_last_message_at").desc(nulls_last=True), "-last_activity_at")
|
|
)
|
|
|
|
|
|
def conversation_for_context(*, context: TenantContext, conversation_id: int) -> Conversation:
|
|
return conversations_for_context(context).get(id=conversation_id)
|