Implemented ability to fetch all messages since last check.
Implemented ability to fetch all messages since last check.

If you setup a room and stage some messages with the following:

# Create Room
curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"createRoom","payload":"{\"roomName\":\"BenTest\",\"owner\":\"ben\",\"passhash\":\"abcdefg\"}"}'

# Subscribe User
curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"inviteUser","payload":"{\"roomName\":\"BenTest\",\"user\":\"ben2\"}"}'

# Send a message
curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"sendMsg","payload":"{\"roomName\":\"BenTest\", \"msg\":\"ENCRYPTED-DATA\",\"user\":\"ben2\"}"}'

# Send another
curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"sendMsg","payload":"{\"roomName\":\"BenTest\", \"msg\":\"ENCRYPTED-DATA2\",\"user\":\"ben2\"}"}'

You can then fetch those messages with the following call

curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"pollMsg","payload":"{\"roomName\":\"BenTest\", \"mylast\":0,\"user\":\"ben2\"}"}'

This means we now have the basic components in place:

* Rooms can be created
* Users can be subscribed to the room
* Messages can be sent
* Clients can check for updated messages

Course, there's no auth yet, but it's probably better to try and build a working client first (as debugging is easier without a complex auth system in the way)

--- a/server/LocalChat.py
+++ b/server/LocalChat.py
@@ -120,9 +120,11 @@
             return self.inviteUser(reqjson)
         
         elif reqjson['action'] == 'sendMsg':
-            print "Send message"
             return self.sendMsg(reqjson)
         
+        elif reqjson['action'] == 'pollMsg':
+            return self.fetchMsgs(reqjson)
+         
         
         
         
@@ -215,8 +217,7 @@
         
         '''
         
-        print reqjson
-        if "user" not in reqjson['payload'] or not self.validateUser(reqjson['payload']):
+        if not self.validateUser(reqjson['payload']):
             return 403
         
         
@@ -238,7 +239,7 @@
         r = self.cursor.fetchone()
         
         if not r:
-            last = None
+            last = 0
         else:
             last = r[0]
         
@@ -249,7 +250,45 @@
             }
         
         
-    
+    def fetchMsgs(self,reqjson):
+        ''' Check to see if there are any new messages in the room
+        
+        curl -v -X POST http://127.0.0.1:8090/ -H "Content-Type: application/json" --data '{"action":"pollMsg","payload":"{\"roomName\":\"BenTest\", \"mylast\":1,\"user\":\"ben2\"}"}'
+        
+        '''
+        
+        if not self.validateUser(reqjson['payload']):
+            return 403
+
+        if "mylast" not in reqjson['payload']:
+            return 400
+        
+        room = self.getRoomID(reqjson['payload']["roomName"])
+        print room
+        if not room:
+            return 400        
+        
+        self.cursor.execute("""SELECT id,msg FROM messages
+            WHERE room=? AND
+            id > ?
+            ORDER BY ts ASC           
+            """,(room,reqjson['payload']['mylast']))
+        
+        r = self.cursor.fetchall()
+        
+        if not r:
+            # No changes
+            return {"status":"unchanged","last":reqjson['payload']['mylast']}
+        
+        # Otherwise, return the messages
+        return {"status":"updated",
+                "messages" : r
+                }
+    
+    
+        
+        
+        
     
     def validateUser(self,payload):
         ''' Placeholder for now. Auth will be handled later