Esempio n. 1
0
def sendMessage():    
    user = getCurrentUser()
    user.lastPing = time.time()
    talkid = request.POST.get('talkid').strip()
    msgText = request.POST.get('message').strip()        
    lastIdStr = request.POST.get('lastMsgId')
    print "message from "+user.name+": ", msgText        
    c = getConversationById(talkid)
    if c:
        message=Message(msgText, user) 
        c.messages.append(message)
        result = c.getMessagesAfter(int(lastIdStr))
        
        ''' if talking to pluto or alice - respond '''
        partner = c.getMyPartner(user.id)
        if partner==pluto:
            if msgText=='printdb':
                pmessage = Message(dbaccess.printStatus(), pluto)
                c.messages.append(pmessage)
            elif msgText=='show error':
                raise Exception('Artificial exception')              
            else:
                pmessage = Message(msgText+" :)", pluto)
                c.messages.append(pmessage)
        elif partner==alice:
            amessage = Message(getAliceBot().respond(msgText, user.id), alice); 
            c.messages.append(amessage)
            
        return json.dumps({'newTime':str(model.timeMillis()),
                           'messages':result}, cls=model.ComplexEncoder)
    else:
        return json.dumps({'newTime':str(model.timeMillis()), 'msg':"conversation is closed"})       
Esempio n. 2
0
def closeConversation():
    user = getCurrentUser()
    if user == chatter:
        user = makeNewUser()
    else:
        user.lastPing = time.time()
    talkid = request.POST.get('talkid').strip()
    c = getConversationById(talkid)
    if not c:
        return json.dumps({'msg': 'Conversation is already closed'})

    partner = c.getMyPartner(user.id)
    print 'closing conversation of', user.toString(), 'with', partner.name
    if not partner.id in users:
        return json.dumps({'msg': 'User is offline'})

    'close conversation'
    if c in user.conversations:
        user.conversations.remove(c)
    c.setAccepted(user.id, value=False)

    'update status (to stop blinking on the partner\'s page)'
    user.lastStatusChange = model.timeMillis()

    'remove or send close message'
    if not c.acceptedBy(partner.id):
        print 'removing conversation', c.partner1.name, '<-->', c.partner2.name
        if c in partner.conversations:
            partner.conversations.remove(c)
        conversations.remove(c)
    else:
        'send CLOSED system message to partner'
        message = Message(model.MSG_CLOSED, user, type='s')
        c.messages.append(message)
Esempio n. 3
0
def closeConversation():
    user = getCurrentUser()
    if user==chatter:
        user=makeNewUser()
    else:
        user.lastPing = time.time()
    talkid = request.POST.get('talkid').strip()
    c = getConversationById(talkid)
    if not c:
        return json.dumps({'msg':'Conversation is already closed'})
    
    partner = c.getMyPartner(user.id)    
    print 'closing conversation of', user.toString(), 'with', partner.name 
    if not partner.id in users:
        return json.dumps({'msg':'User is offline'})
    
    'close conversation'
    if c in user.conversations:
        user.conversations.remove(c)
    c.setAccepted(user.id, value=False)
    
    'update status (to stop blinking on the partner\'s page)'
    user.lastStatusChange = model.timeMillis();
    
    'remove or send close message'        
    if not c.acceptedBy(partner.id):
        print 'removing conversation', c.partner1.name,'<-->', c.partner2.name
        if c in partner.conversations:
            partner.conversations.remove(c)
        conversations.remove(c)        
    else:
        'send CLOSED system message to partner'
        message=Message(model.MSG_CLOSED, user, type='s') 
        c.messages.append(message)
Esempio n. 4
0
def requestConversation():
    user = getCurrentUser()
    if user==chatter:
        user=makeNewUser()
    else:
        user.lastPing = time.time()
    partnerid = request.POST.get('partnerid').strip()
    print 'processing request for', user.toString(), 'talk to', partnerid 
    if partnerid in users:
        partner = users[partnerid]
        c = findConversation(user, partner)
        if c:
            if not c.acceptedBy(user.id):
                'accepting conversation'
                print 'user', user.name, 'accepts talk to', partner.name
                c.setAccepted(user.id)
                user.conversations+=[c]
                message=Message(model.MSG_ACCEPTED, user, type='s') 
                c.messages.append(message)
                
        else:
            c = createConversation(user, partner)
            'automatically accept by pluto'
            if partner.id=='2' and not c in partner.conversations:
                partner.conversations+=[c]
                c.setAccepted(partner.id)
            
                        
        htmlBody = template('conversation', talk=c, partner=partner)        
        return json.dumps({'talkid':c.id, 'myid':user.id, 'html':htmlBody, 
                           'msgId':c.getLastMsgId(), 'time':model.timeMillis()})
    else:
        return json.dumps({'msg':'User is offline'})
Esempio n. 5
0
def ping():    
    user = getCurrentUser()
    if user == chatter:
        return json.dumps({'newTime':str(model.timeMillis())})
    
    req = request.POST.get('gettime')
    if req:
        print "returning server time"
        return json.dumps({'newTime':str(model.timeMillis())})
    
    user.lastPing = time.time()        
    msgIdMap = json.loads(request.POST.get('conversations'))
    lastTime =  request.POST.get('lastTime')
    print "request for changes from "+user.name        
    changes = getNewChanges(user, msgIdMap, int(lastTime))       
    return json.dumps({'newTime':str(model.timeMillis()),                        
                       'messages':changes['messages'],
                       'requests':changes['requests'],
                       'userchanges':changes['userchanges']}, cls=model.ComplexEncoder)
Esempio n. 6
0
def index():    
    user = getCurrentUser()
    print 'creating page for user', user.toString()
    result = filterUsers(user);
    response.headers['Cache-Control']='no-cache, must-revalidate'
    response.headers['Expires']='Sat, 26 Jul 1997 05:00:00 GMT'
    print users
    return dict(result=user, onliners=result["users"], 
                talks=user.conversations, allIcons=allUserIcons, 
                lastTime=model.timeMillis())
Esempio n. 7
0
def index():
    user = getCurrentUser()
    print 'creating page for user', user.toString()
    result = filterUsers(user)
    response.headers['Cache-Control'] = 'no-cache, must-revalidate'
    response.headers['Expires'] = 'Sat, 26 Jul 1997 05:00:00 GMT'
    print users
    return dict(result=user,
                onliners=result["users"],
                talks=user.conversations,
                allIcons=allUserIcons,
                lastTime=model.timeMillis())
Esempio n. 8
0
def sendMessage():
    user = getCurrentUser()
    user.lastPing = time.time()
    talkid = request.POST.get('talkid').strip()
    msgText = request.POST.get('message').strip()
    lastIdStr = request.POST.get('lastMsgId')
    print "message from " + user.name + ": ", msgText
    c = getConversationById(talkid)
    if c:
        message = Message(msgText, user)
        c.messages.append(message)
        result = c.getMessagesAfter(int(lastIdStr))
        ''' if talking to pluto or alice - respond '''
        partner = c.getMyPartner(user.id)
        if partner == pluto:
            if msgText == 'printdb':
                pmessage = Message(dbaccess.printStatus(), pluto)
                c.messages.append(pmessage)
            elif msgText == 'show error':
                raise Exception('Artificial exception')
            else:
                pmessage = Message(msgText + " :)", pluto)
                c.messages.append(pmessage)
        elif partner == alice:
            amessage = Message(getAliceBot().respond(msgText, user.id), alice)
            c.messages.append(amessage)

        return json.dumps(
            {
                'newTime': str(model.timeMillis()),
                'messages': result
            },
            cls=model.ComplexEncoder)
    else:
        return json.dumps({
            'newTime': str(model.timeMillis()),
            'msg': "conversation is closed"
        })
Esempio n. 9
0
def ping():
    user = getCurrentUser()
    if user == chatter:
        return json.dumps({'newTime': str(model.timeMillis())})

    req = request.POST.get('gettime')
    if req:
        print "returning server time"
        return json.dumps({'newTime': str(model.timeMillis())})

    user.lastPing = time.time()
    msgIdMap = json.loads(request.POST.get('conversations'))
    lastTime = request.POST.get('lastTime')
    print "request for changes from " + user.name
    changes = getNewChanges(user, msgIdMap, int(lastTime))
    return json.dumps(
        {
            'newTime': str(model.timeMillis()),
            'messages': changes['messages'],
            'requests': changes['requests'],
            'userchanges': changes['userchanges']
        },
        cls=model.ComplexEncoder)
Esempio n. 10
0
def requestConversation():
    user = getCurrentUser()
    if user == chatter:
        user = makeNewUser()
    else:
        user.lastPing = time.time()
    partnerid = request.POST.get('partnerid').strip()
    print 'processing request for', user.toString(), 'talk to', partnerid
    if partnerid in users:
        partner = users[partnerid]
        c = findConversation(user, partner)
        if c:
            if not c.acceptedBy(user.id):
                'accepting conversation'
                print 'user', user.name, 'accepts talk to', partner.name
                c.setAccepted(user.id)
                user.conversations += [c]
                message = Message(model.MSG_ACCEPTED, user, type='s')
                c.messages.append(message)

        else:
            c = createConversation(user, partner)
            'automatically accept by pluto'
            if partner.id == '2' and not c in partner.conversations:
                partner.conversations += [c]
                c.setAccepted(partner.id)

        htmlBody = template('conversation', talk=c, partner=partner)
        return json.dumps({
            'talkid': c.id,
            'myid': user.id,
            'html': htmlBody,
            'msgId': c.getLastMsgId(),
            'time': model.timeMillis()
        })
    else:
        return json.dumps({'msg': 'User is offline'})