]> git.parisson.com Git - teleforma.git/commitdiff
add CHAT_LIMIT_HOURS and limit chat queryset in time
authorGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Sun, 23 Jan 2022 23:10:57 +0000 (00:10 +0100)
committerGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Sun, 23 Jan 2022 23:10:57 +0000 (00:10 +0100)
app/settings.py
teleforma/ws/chat.py

index 7df91150b2a212c61dc6be1a9b545a279ba99f8e..2e741da355538b4962b0d08bcf533a0831406782 100644 (file)
@@ -53,8 +53,6 @@ CHANNEL_LAYERS = {
     },
 }
 
-ENABLE_CHAT = True
-
 DATABASES = {
     'default': {
         # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
@@ -504,6 +502,9 @@ if DEBUG_TOOLBAR:
 USE_WEBPACK_DEV_SERVER = False
 WEBPACK_DEV_SERVER_URL = "http://172.24.104.152:3000/"
 
+ENABLE_CHAT = True
+CHAT_LIMIT_HOURS = 72
+
 
 ##################
 # LOCAL SETTINGS #
index 4dfd56e95827f193d4ddf4967ec28b064de99915..fa909243c09c231f46fd0caf962b9ba0fc416187 100644 (file)
@@ -4,6 +4,7 @@ from teleforma.models.chat import ChatMessage
 from django.conf import settings 
 from channels.generic.websocket import AsyncJsonWebsocketConsumer
 from channels.db import database_sync_to_async
+from datetime import datetime, timedelta
 
 
 @log_consumer_exceptions
@@ -54,8 +55,10 @@ class ChatConsumer(AsyncJsonWebsocketConsumer):
         
     @database_sync_to_async
     def get_messages_list(self):
+        limit_hours = getattr(settings, 'CHAT_LIMIT_HOURS', 72)
+        limit_datetime = datetime.now() - timedelta(hours=limit_hours)
         messages = [message.to_dict() for message in ChatMessage.objects.filter(
-            room_name=self.room_name).order_by('-created')[:100]]
+            room_name=self.room_name).filter(created__gte=limit_datetime).order_by('-created')]
         messages = messages[::-1]
         return messages