Exemplo n.º 1
0
def room_create(sock, data):
    user = User.find_user_by_sock(sock)
    target_name = data['details']['new_user']

    echo = None
    if "echo" in data:
        echo = data['echo']

    target_resource = user.resource
    target_online = User.find_user_by_name(target_name, user.resource)  # sock de outro usuario conectado
    target_offline = None
    if not target_online:
        api_mongo.insert_user(target_name, target_resource)
        target_offline = target_name

    if user == target_online:
        info = {"type": "chat", "payload": {"command": "error", "details": "choose_another_less_you"}}
        send(user, info)
    else:
        second_user = None
        if target_online:
            second_user = target_online.username
        if not second_user:
            second_user = target_offline

        if second_user:
            if not Room.check_exist_room([user.username, second_user], user.resource):  # se sala nao existe

                user_temp = []
                for username in [user.username, second_user]:
                    user_temp.append({"username": username, "seen": None, "received": None})

                room_name = Room.open(user_temp, target_resource)  # cria a sala e recebe nome unico a sala
                new_room = Room.find_room_by_name(room_name)  # sock para o nova sala criada
                new_room.users_name = user_temp  # insert two news name users
                new_room.alias = 'DEFAULT'  # insert alias
                alias = new_room.alias
                api_mongo.insert_room(user_temp, room_name, alias, target_resource, False, None)
                info = {"type": "chat", "payload": {"command": "room_id",
                                                    "alias": new_room.alias,
                                                    "room_id": room_name,
                                                    "users": user_temp,
                                                    "echo": echo,
                                                    "is_group": False}}
                send(user, info)
            else:  # se sala ja existe
                user_temp = []
                for username in [user.username, second_user]:
                    user_temp.append({"username": username, "seen": None, "received": None})

                room_to_append = Room.find_room_by_users([user.username, second_user], user.resource)
                room_restore(room_to_append)
                new_room = Room.find_room_by_name(room_to_append)  # sock para o nova sala criada
                info = {"type": "chat", "payload": {"command": "room_id",
                                                    "alias": new_room.alias,
                                                    "room_id": room_to_append,
                                                    "users": user_temp,
                                                    "is_group": new_room.is_group}}
                send(user, info)
Exemplo n.º 2
0
def room_user_add(sock, data):
    u = User.find_user_by_sock(sock)
    new_users_name = data['details']['new_user']
    if type(new_users_name) is unicode:
        new_users_name = [data['new_user']]

    echo = None
    if "echo" in data:
        echo = data['echo']

    origin = data['details']['destination']
    room_users = Room.find_users_by_room(origin)  # Lista de usuarios em uma sala
    u_list = []

    for el in new_users_name:
        if not (el in room_users):
            u_list.append(el)

    if room_users:
        for el in room_users:
            u_list.append(el)

    destination = Room.check_exist_room(u_list, u.resource)

    if not destination:
        room_name = Room.open([], u.resource)  # cria a sala e recebe nome unico a sala
        new_room = Room.find_room_by_name(room_name)  # sock para o nova sala criada
        destination = new_room.room_id
        new_room.is_group = True
        new_room.last_message = time.time()
        new_room.alias = 'DEFAULT'  # todo - validar nome default

        # for unew in u_list:  # todo validar se os usuarios estao off-line
        #     if unew != User.find_user_by_name(unew, u.resource)['username']:
        #         api_mongo.insert_user(unew, u.resource)

        obj_users = [{"username": i, "seen": None, "received": None} for i in u_list]
        api_mongo.insert_room(obj_users, room_name, new_room.alias, u.resource, True, new_room.last_message)

    if destination:
        room_restore(destination)
        new_room = Room.find_room_by_name(destination)  # sock para o nova sala criada
        [send_system_info(sock, destination, 'USER ADD', el) for el in new_users_name]  # envia message para grupo da entrada do user
        new_room.users_name = []
        obj_users = [{"username": i, "seen": None, "received": None} for i in u_list]
        new_room.users_name = obj_users
        new_room.is_group = True
        new_room.last_message = time.time()

        info = {
            "type": "chat",
            "payload": {
                "command": "room_id",
                "alias": new_room.alias,
                "room_id": new_room.room_id,
                "is_group": True,
                "users": obj_users,
                "echo": echo,
                "last_message":  new_room.last_message
            }
        }

        send_broadcast(room_users, info, u.resource)