Source

openju / openju / message_bus.py

Full commit
#!/usr/bin/python
# -*- coding: utf-8 -*-

import xmpp
from mongoengine.queryset import DoesNotExist

# from openju.util import none_or_int
from openju import g
from openju import current_app as app
from openju.validators import UserAlertError
from openju.routes import *

def error_handler(f):
    def new_f(conn, *args, **kw):
        try:
            f(conn, *args, **kw)
        except UserAlertError, e:
            conn.send(xmpp.Message(g.xmpp_user, unicode(e)))
    return new_f

@error_handler
def message_bus(conn, mess):
    """ kind a routing thing """
    
    text = mess.getBody()
    xmpp_user = mess.getFrom()
    user_jid = u'%(node)s@%(domain)s' % dict(
        node=xmpp_user.getNode(),
        domain=xmpp_user.getDomain())
    
    if text is not None: # don't know why, but sometimes it's None. TODO: debug it
        def auth_user():
            from openju.model import User
            try:
                return User.objects.get(jid=user_jid)
            except DoesNotExist:
                return  None
        
        def dispatch(xmpp_user):
            def call_list(*f_list):
                for f in f_list:
                    res = f(text)
                    if isinstance(res, unicode):
                        conn.send(xmpp.Message(xmpp_user, res))
                    if res is not None:
                        return
            
            call_list(
                cmd_register,
                cmd_last_messages,
                cmd_last_messages_from_friends,
                cmd_read_message,
                cmd_read_message_with_replies,
                cmd_post_reply,
                cmd_user_info,
                cmd_user_messages,
                cmd_subscribe,
                cmd_post_message,
                )
        
        def notify_logic(xmpp_user):
            def filter_empty(l):
                return (x for x in l if x is not None)
            
            def process_notifications(notification):
                from openju.controllers import notify
                action = getattr(notify_routes, notification.action, default=None)
                if action is not None:
                    res = action(notification.user, notification.meta)
                    if res is not None:
                        yield res
                else:
                    app.logger.exception(
                        Exception(u'Notification action %s not found' % action))
            
            notify_text = (
                u"\n\n"
                .join(
                    filter_empty(
                        process_notifications(
                            g.auth_user.get_notifications()))))
            if notify_text:
                conn.send(xmpp.Message(xmpp_user, ))
        
        # should be only place to put some g-stuff
        g.xmpp_user = xmpp_user
        g.text = text
        g.user_jid = user_jid
        
        g.auth_user = auth_user()
        dispatch(xmpp_user)
        notify_logic(xmpp_user)