Older messages will now be purged when a user joins the room
Older messages will now be purged when a user joins the room

The aim of this is to prevent the new user from dumping out the entire stored history of a chat. Although the client will honour "last" they could conceivably override it to 0 and grab everything.

So, when they join, we now remove any messages older than 10 seconds.

At the moment, it's a little meaningless as you don't need to join in order to do pollMsg. That will obviously need to change.

--- a/server/LocalChat.py
+++ b/server/LocalChat.py
@@ -235,6 +235,16 @@
             return { "status": "NOK" }
         else:
             
+            
+            # Tidy older messages away.
+            #
+            # We do this so that a user who joins can't then send a poll with last:0 to retrieve the full history
+            #
+            # Basically, anything older than 10 seconds should go. Users who were already present will be able
+            # to scroll up and down in their client anyway
+            self.tidyMsgs(time.time()-10,room)
+            
+            
             # Push a message to the room to note that the user joined
             
             m = {
@@ -245,6 +255,7 @@
             self.cursor.execute("INSERT INTO messages (ts,room,msg) VALUES (?,?,?)",(time.time(),room,json.dumps(m)))
             msgid = self.cursor.lastrowid
             self.conn.commit()
+            
             # Check the latest message ID for that room
             self.cursor.execute("SELECT id from messages WHERE room=? and id != ? ORDER BY id DESC",(room,msgid))
             r = self.cursor.fetchone()
@@ -363,6 +374,20 @@
         return r[0]
     
 
+    def tidyMsgs(self,thresholdtime,room=False):
+        ''' Remove messages older than the threshold time
+        '''
+        
+        if room:
+            # Tidy from a specific room
+            self.cursor.execute("DELETE FROM messages where ts < ? and room = ?",(thresholdtime,room))
+            self.conn.commit()
+            
+        else:
+            self.cursor.execute("DELETE FROM messages where ts < ?",(thresholdtime,))
+            self.conn.commit()
+
+
 
     def test(self):
         return ['foo']