Commits

Marcin Lulek committed 3d46bd8

updates

  • Participants
  • Parent commits 14c3d11

Comments (0)

Files changed (1)

gevent_cometd/app_views.py

     res = Response('{}', request=request)
     return res
 
+def pass_message(msg):
+    if msg.get('timestamp'):
+        # if present lets use timestamp provided in the message
+        if '.' in msg['timestamp']:
+            timestmp = datetime.datetime.strptime(msg['timestamp'],
+                                      '%Y-%m-%dT%H:%M:%S.%f')
+        else:
+            timestmp = datetime.datetime.strptime(msg['timestamp'],
+                                      '%Y-%m-%dT%H:%M:%S')
+    else:
+        timestmp = datetime.datetime.utcnow()
+    message = {'user': msg.get('user'),
+               'message': msg['message'],
+               'type':'message',
+               'timestamp': timestmp
+               }
+    pm_users = msg.get('pm_users', [])
+    if msg.get('channel'):
+        channel_inst = channel.Channel.get_channel(msg['channel'])
+        channel_inst.add_message(message, pm_users=pm_users)
+    elif pm_users:
+        # if pm then iterate over all users and notify about new message hiyoo!!
+        for user_id in pm_users:
+            user_inst = user.User.by_name(user_id)
+            if user_inst:
+                user_inst.add_message(message)
+
 def message(request, *args):
     """sends out message"""
     request_data, res = util.process_request(request)
     if res:
         return res
     for msg in request_data:
-        if msg.get('timestamp'):
-            # if present lets use timestamp provided in the message
-            if '.' in msg['timestamp']:
-                timestmp = datetime.datetime.strptime(msg['timestamp'],
-                                          '%Y-%m-%dT%H:%M:%S.%f')
-            else:
-                timestmp = datetime.datetime.strptime(msg['timestamp'],
-                                          '%Y-%m-%dT%H:%M:%S')
-        else:
-            timestmp = datetime.datetime.utcnow()
-        message = {'user': msg.get('user'),
-                   'message': msg['message'],
-                   'type':'message',
-                   'timestamp': timestmp
-                   }
-        pm_users = msg.get('pm_users', [])
-        if msg.get('channel'):
-            channel_inst = channel.Channel.get_channel(msg['channel'])
-            channel_inst.add_message(message, pm_users=pm_users)
-        elif pm_users:
-            # if pm then iterate over all users and notify about new message hiyoo!!
-            for user_id in pm_users:
-                user_inst = user.User.by_name(user_id)
-                if user_inst:
-                    user_inst.add_message(message)
-        else:
+        if not msg.get('channel') and not msg.get('pm_users', []):
             res = Response(json.dumps({'error':"No channels or usernames specified"}),
                            request=request)
             res.status = 400
             return res
+        gevent.Greenlet.spawn(pass_message, msg)
     res = Response(u"{}", request=request)
     return res