Compare commits

..

No commits in common. 'bec6c10e0862e48fed1d507a095582449729e1ad' and '7f3ba4fb653b3c6de81b3908ca7670ff43f81a83' have entirely different histories.

  1. 18
      backend/blueprints/chat/__init__.py

@ -90,12 +90,23 @@ def get_messages():
if not reference_message: if not reference_message:
return jsonify({'message': 'Reference message not found'}), 404 return jsonify({'message': 'Reference message not found'}), 404
query = query.order_by(Chat.chatDate.asc()).where(Chat.chatDate > reference_message.chatDate) query = query.order_by(Chat.chatDate.asc()).where(Chat.chatDate > reference_message.chatDate)
# For after queries, reverse the order later
query = query.order_by(Chat.chatDate)
except ValueError: except ValueError:
return jsonify({'message': 'Invalid message ID format'}), 400 return jsonify({'message': 'Invalid message ID format'}), 400
else: else:
query = query.order_by(Chat.chatDate.desc()) # Default ordering
query = query.order_by(desc(Chat.chatDate))
# Apply limit and execute query
query = query.limit(limit) query = query.limit(limit)
messages = db.session.execute(query).scalars() messages = list(db.session.execute(query).scalars())
# Reverse the order for 'after' queries to maintain consistency
if after_id:
messages.reverse()
# Format messages
chat_messages = [{ chat_messages = [{
'id': str(msg.id), 'id': str(msg.id),
'text': msg.textContent, 'text': msg.textContent,
@ -103,9 +114,12 @@ def get_messages():
'username': msg.user.username, 'username': msg.user.username,
'timestamp': msg.chatDate.isoformat() 'timestamp': msg.chatDate.isoformat()
} for msg in messages] } for msg in messages]
return jsonify({ return jsonify({
'messages': chat_messages, 'messages': chat_messages,
'count': len(chat_messages), 'count': len(chat_messages),
'hasMore': len(chat_messages) == limit
}), 200 }), 200
except Exception as e: except Exception as e:
return jsonify({'message': f'An error occurred: {str(e)}'}), 500 return jsonify({'message': f'An error occurred: {str(e)}'}), 500
Loading…
Cancel
Save