pull/18581/head
AlexChimTrm 1 year ago
parent 3e74dcba2c
commit 7471802eb7

@ -15,7 +15,7 @@ from fields.conversation_fields import (
simple_conversation_fields,
)
from fields.conversation_variable_fields import (
paginated_conversation_variable_fields,
conversation_variable_infinite_scroll_pagination_fields,
)
from libs.helper import uuid_value
from models.model import App, AppMode, EndUser
@ -98,7 +98,7 @@ class ConversationRenameApi(Resource):
class ConversationVariablesApi(Resource):
@validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY))
@marshal_with(paginated_conversation_variable_fields)
@marshal_with(conversation_variable_infinite_scroll_pagination_fields)
def get(self, app_model: App, end_user: EndUser, c_id):
# conversational variable only for chat app
@ -108,8 +108,14 @@ class ConversationVariablesApi(Resource):
conversation_id = str(c_id)
parser = reqparse.RequestParser()
parser.add_argument("limit", type=int_range(1, 100), required=False, default=20, location="args")
args = parser.parse_args()
try:
return ConversationService.get_conversational_variable(app_model, conversation_id, end_user)
return ConversationService.get_conversational_variable(
app_model, conversation_id, end_user, args["limit"]
)
except services.errors.conversation.ConversationNotExistsError:
raise NotFound("Conversation Not Exists.")

@ -19,3 +19,9 @@ paginated_conversation_variable_fields = {
"has_more": fields.Boolean,
"data": fields.List(fields.Nested(conversation_variable_fields), attribute="data"),
}
conversation_variable_infinite_scroll_pagination_fields = {
"limit": fields.Integer,
"has_more": fields.Boolean,
"data": fields.List(fields.Nested(conversation_variable_fields)),
}

@ -173,7 +173,8 @@ class ConversationService:
cls,
app_model: App,
conversation_id: str,
user: Optional[Union[Account, EndUser]]
user: Optional[Union[Account, EndUser]],
limit: int,
) -> InfiniteScrollPagination:
conversation = cls.get_conversation(app_model, conversation_id, user)
@ -184,24 +185,21 @@ class ConversationService:
.order_by(ConversationVariable.created_at)
)
page = 1
page_size = 100
stmt = stmt.limit(page_size).offset((page - 1) * page_size)
total_count = db.session.scalar(select(func.count()).select_from(stmt))
has_more = total_count > limit
stmt = stmt.limit(limit)
with Session(db.engine) as session:
rows = session.scalars(stmt).all()
return {
"page": page,
"limit": page_size,
"total": len(rows),
"has_more": False,
"data": [
{
"created_at": row.created_at,
"updated_at": row.updated_at,
**row.to_variable().model_dump(),
}
for row in rows
],
}
variables = [
{
"created_at": row.created_at,
"updated_at": row.updated_at,
**row.to_variable().model_dump(),
}
for row in rows
]
return InfiniteScrollPagination(variables, limit, has_more)

Loading…
Cancel
Save