Esempio n. 1
0
    def create_conversation(self, convo):
        """Create conversation or marks them for update based on last message time"""

        convo_id = convo['id']
        # id looks like: u'77871924:6924356'
        # our id is first
        from_user_id = convo_id.split(':')[1]
        # username from conversation list of users
        try:
            from_user_name = (user['username'] for user in convo['users']
                              if str(user['id']) == from_user_id).next()
        except KeyError as e:
            if from_user_id == 'system':
                from_user_name = 'system'
            else:
                raise e

        last_message_time = parser.parse(convo['last_message']['sent_at'])
        try:
            convo = Conversation.objects.get(convo_id=convo_id)
            convo.needs_update = convo.last_message_time != last_message_time
            convo.save()
        except Conversation.DoesNotExist:
            convo = Conversation(
                convo_id=convo_id,
                user_name=from_user_name,
                user_id=from_user_id,
                last_message_time=last_message_time
            )
            convo.save()
            logger.info('created conversation: %s', convo)
Esempio n. 2
0
def get_or_create_conversation(conv_id, user2):
    try:
        int(conv_id)
        return Conversation.objects.get(id=conv_id)
    except ValueError:
        user1 = User.objects.get(username=conv_id)
        try:
            return Conversation.objects.filter(isgroup=False, members=user2).get(members=user1)
        except Conversation.DoesNotExist:
            conv = Conversation(isgroup=False)
            conv.save()
            conv.members.add(user1)
            conv.members.add(user2)
            return conv
Esempio n. 3
0
def webapp_conversation_start(request, uuid):
    template = loader.get_template('annonce.html')
    context = {}
    ad = Ad.objects.get(pk=uuid)

    try:
        conversation = Conversation.objects.get(ad=ad)
        try:
            messages = Message.objects.get(conversation=conversation)
        except Message.DoesNotExist:
            messages = None
            context.update({
                'ad': ad,
                'conversation': conversation
            })

    except Conversation.DoesNotExist:
        Conversation(ad=ad).save()
        conversation = Conversation.objects.get(ad=ad)

    if conversation is not None:
        context.update({
            'ad': ad,
            'conversation': conversation
        })

    return HttpResponse(template.render(context, request))
Esempio n. 4
0
 def test_register_response(self, mock_tasks):
     self.mock_wallet.return_value.get_new_address.return_value = '1'
     convo = Conversation(
         convo_id='test',
         last_message_time=datetime.now()
     )
     convo.save()
     Message(
         user_name='bopeep2',
         user_id='whatev',
         conversation=convo,
         message='register',
         sent_at=datetime.now()
     ).save()
     self.processor.process_messages()
     self.assertEqual(User.objects.get(user_name='bopeep2').deposit_address, '1')
Esempio n. 5
0
def create_pm(sender, text, user_channel_id):
    """
    Create a new message
    """
    try:
        lc = UserChannel.objects.get(id=user_channel_id)
        uc = UserChannel.objects.get(channel_id=lc.channel_id, nickname=sender)
    except UserChannel.DoesNotExist:
        uc = UserChannel(channel_id=lc.channel_id, nickname=sender)
        uc.save()
    try:
        conv = Conversation.objects.get(
            Q(user_channel_1__in=[lc, uc]) & Q(user_channel_2__in=[lc, uc]))
    except Exception:
        conv = Conversation(user_channel_1=lc, user_channel_2=uc)
        conv.save()
    pm = PrivateMessage(conversation=conv, user_channel=uc, text=text)
    pm.save()
    return pm
Esempio n. 6
0
def create_pm(sender, text, user_channel_id):
    """
    Create a new message
    """
    try:
        lc = UserChannel.objects.get(id=user_channel_id)
        uc = UserChannel.objects.get(channel_id=lc.channel_id, nickname=sender)
    except UserChannel.DoesNotExist:
        uc = UserChannel(channel_id=lc.channel_id, nickname=sender)
        uc.save()
    try:
        conv = Conversation.objects.get(
            Q(user_channel_1__in=[lc, uc]) &
            Q(user_channel_2__in=[lc, uc]))
    except Exception:
        conv = Conversation(user_channel_1=lc, user_channel_2=uc)
        conv.save()
    pm = PrivateMessage(conversation=conv, user_channel=uc, text=text)
    pm.save()
    return pm
Esempio n. 7
0
def message_received(client, server, message):
    message = decode_utf_8(message)

    user = connected.get(client["id"])
    if user is not None:
        if message[0] == "m":
            message = message[2:]
            index = message.index(";")
            target = message[:index]
            message = message[(index + 1):]

            print_color("Client(%d) said: %s" % (client['id'], message))
            conversation = get_or_create_conversation(target, user)

            new_chat_message = ChatMessage(user=user, conversation=conversation, text=message)
            new_chat_message.save()
            for c in server.clients:
                if c['id'] != client['id']:
                    if conversation.isgroup:
                        name = get_conv_name(conversation)
                    else:
                        name = user.username
                    server.send_message(c, "m;" + str(conversation.id) + ";" + user.username + ":" + name + ";" + message)
        if (message[0] == "n") or (message[0] == "o"):
            param1 = message[2:]
            if user.username != param1:
                conversation = get_or_create_conversation(param1, user)
                chat_messages = ChatMessage.objects.filter(conversation=conversation)
                data = {}
                counter = 0
                for current_message in chat_messages:
                    data[counter] = "m;" + str(
                        conversation.id) + ";" + current_message.user.username + ";" + current_message.text
                    counter += 1
                if message[0] == "o":
                    send_list(server, client, user.username)
                    send_group_list(server, client, user.username)
                    send_online(server)
                    server.send_message(client, "w;" + message[2:] + ";" + str(get_or_create_conversation(message[2:], user).id) + ";" + User.objects.get(username=message[2:]).get_color())

                    u = User.objects.get(username=message[2:])
                    for x in connected.get_id(u):
                        client_temp = connected.get_client(x)
                        send_list(server, client_temp, u.username)
                        send_group_list(server, client_temp, u.username)
                if message[0] == "n":
                    server.send_message(client, "a;" + str(conversation.id) + ";" + json.dumps(data))
        if message[0] == "g":
            id = message[2:].split(";")[0]
            users_list = message[(3+len(id)):].split(";")

            conversation = Conversation.objects.get(id=id)
            if conversation.isgroup:
                new_members = User.objects.filter(username__in=users_list)
                conversation.members.add(*new_members)
                for u in conversation.members.all():
                    for x in connected.get_id(u):
                        client_temp = connected.get_client(x)
                        send_list(server, client_temp, u.username)
                        send_group_list(server, client_temp, u.username)
            else:
                new_members = (conversation.members.all() | User.objects.filter(username__in=users_list)).distinct()
                if len(new_members) > 2:
                    conv = Conversation(isgroup=True)
                    conv.save()
                    conv.members.add(*new_members)
                    for u in conv.members.all():
                        for x in connected.get_id(u):
                            client_temp = connected.get_client(x)
                            send_list(server, client_temp, u.username)
                            send_group_list(server, client_temp, u.username)
        if message[0] == "r":
            id = message[2:].split(";")[0]
            new_name = message[(3 + len(id)):]
            conv = get_or_create_conversation(id, user)
            if conv.isgroup:
                conv.name = new_name
                conv.save()
                for u in conv.members.all():
                    for x in connected.get_id(u):
                        client_temp = connected.get_client(x)
                        server.send_message(client_temp, "r;" + str(conv.id) + ";" + get_conv_name(conv))
                        send_list(server, client_temp, u.username)
                        send_group_list(server, client_temp, u.username)
                send_online(server)
        if message[0] == "O":
            conversation = get_or_create_conversation(message[2:], user)
            server.send_message(client, "w;" + get_conv_name(conversation) + ";" + message[2:] + "; #ffffff")
    else:
        try:
            session_id = decrypt(SECRET_KEY_WEBSOCKET, message)
            session = Session.objects.get(pk=session_id)
            u = User.objects.get(id=session.get_decoded().get('_auth_user_id', None))
            connected.add(client, u)

            send_list(server, client, u.username)
            send_group_list(server, client, u.username)
            send_online(server)
            server.send_message(client, "u;" + u.username)
        except Session.DoesNotExist:
            logging.error("Error: user doesn't exist")