|
@@ -46,6 +46,8 @@ class MessageService:
|
|
|
app_model=app_model, user=user, conversation_id=conversation_id
|
|
|
)
|
|
|
|
|
|
+ fetch_limit = limit + 1
|
|
|
+
|
|
|
if first_id:
|
|
|
first_message = (
|
|
|
db.session.query(Message)
|
|
@@ -64,7 +66,7 @@ class MessageService:
|
|
|
Message.id != first_message.id,
|
|
|
)
|
|
|
.order_by(Message.created_at.desc())
|
|
|
- .limit(limit)
|
|
|
+ .limit(fetch_limit)
|
|
|
.all()
|
|
|
)
|
|
|
else:
|
|
@@ -72,25 +74,14 @@ class MessageService:
|
|
|
db.session.query(Message)
|
|
|
.filter(Message.conversation_id == conversation.id)
|
|
|
.order_by(Message.created_at.desc())
|
|
|
- .limit(limit)
|
|
|
+ .limit(fetch_limit)
|
|
|
.all()
|
|
|
)
|
|
|
|
|
|
has_more = False
|
|
|
- if len(history_messages) == limit:
|
|
|
- current_page_first_message = history_messages[-1]
|
|
|
- rest_count = (
|
|
|
- db.session.query(Message)
|
|
|
- .filter(
|
|
|
- Message.conversation_id == conversation.id,
|
|
|
- Message.created_at < current_page_first_message.created_at,
|
|
|
- Message.id != current_page_first_message.id,
|
|
|
- )
|
|
|
- .count()
|
|
|
- )
|
|
|
-
|
|
|
- if rest_count > 0:
|
|
|
- has_more = True
|
|
|
+ if len(history_messages) > limit:
|
|
|
+ has_more = True
|
|
|
+ history_messages = history_messages[:-1]
|
|
|
|
|
|
if order == "asc":
|
|
|
history_messages = list(reversed(history_messages))
|
|
@@ -112,6 +103,8 @@ class MessageService:
|
|
|
|
|
|
base_query = db.session.query(Message)
|
|
|
|
|
|
+ fetch_limit = limit + 1
|
|
|
+
|
|
|
if conversation_id is not None:
|
|
|
conversation = ConversationService.get_conversation(
|
|
|
app_model=app_model, user=user, conversation_id=conversation_id
|
|
@@ -131,21 +124,16 @@ class MessageService:
|
|
|
history_messages = (
|
|
|
base_query.filter(Message.created_at < last_message.created_at, Message.id != last_message.id)
|
|
|
.order_by(Message.created_at.desc())
|
|
|
- .limit(limit)
|
|
|
+ .limit(fetch_limit)
|
|
|
.all()
|
|
|
)
|
|
|
else:
|
|
|
- history_messages = base_query.order_by(Message.created_at.desc()).limit(limit).all()
|
|
|
+ history_messages = base_query.order_by(Message.created_at.desc()).limit(fetch_limit).all()
|
|
|
|
|
|
has_more = False
|
|
|
- if len(history_messages) == limit:
|
|
|
- current_page_first_message = history_messages[-1]
|
|
|
- rest_count = base_query.filter(
|
|
|
- Message.created_at < current_page_first_message.created_at, Message.id != current_page_first_message.id
|
|
|
- ).count()
|
|
|
-
|
|
|
- if rest_count > 0:
|
|
|
- has_more = True
|
|
|
+ if len(history_messages) > limit:
|
|
|
+ has_more = True
|
|
|
+ history_messages = history_messages[:-1]
|
|
|
|
|
|
return InfiniteScrollPagination(data=history_messages, limit=limit, has_more=has_more)
|
|
|
|