From 4e1ac79e2d2ca1d11044275896b67210d2bc67ce Mon Sep 17 00:00:00 2001 From: Marcos Date: Tue, 12 Nov 2024 11:23:43 -0300 Subject: [PATCH] chore: Added comment on get_message_history() explaining the double inversion. --- learning_assistant/api.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/learning_assistant/api.py b/learning_assistant/api.py index 2006b26..55c73c1 100644 --- a/learning_assistant/api.py +++ b/learning_assistant/api.py @@ -217,6 +217,10 @@ def get_message_history(courserun_key, user, message_count): Returns a number of messages equal to the message_count value. """ + # Explanation over the double reverse: This fetches the last message_count elements ordered by creating order DESC. + # Slicing the list in the model is an equivalent of adding LIMIT on the query. + # The result is the last chat messages for that user and course but in inversed order, so in order to flip them + # its first turn into a list and then reversed. message_history = list(LearningAssistantMessage.objects.filter( course_id=courserun_key, user=user).order_by('-created')[:message_count])[::-1] return message_history