Exemplo n.º 1
0
def delete_messages_from_chat(chat_id, id_list):
    str_where = convert_args_to_querystr(' OR ', query=id_list, key='id')
    str_where = f"chat_id = {chat_id} AND {str_where}"
    execute(f"""
    DELETE FROM messages
    WHERE {str_where}
    """)
Exemplo n.º 2
0
def get_chats_by_user(user_id,
                      chat_id=None,
                      is_messages=False,
                      is_users=False,
                      limit='',
                      where='',
                      is_other=False):
    where_args = {'users_id': user_id}
    limit, limit2 = ('', limit) if isinstance(limit, int) else (limit, None)
    if where:
        if not is_other:
            where_sql = f"users_id = {user_id} AND {where}"
        else:
            where_sql = where
    else:
        if chat_id is not None:
            where_args.update(chat_id=chat_id)
        where_sql = convert_args_to_querystr(' AND ', query=where_args)

    if not is_other:
        chats = execute(
            "SELECT chat.* FROM chat_has_users "
            "INNER JOIN chat on chat_has_users.chat_id=chat.id "
            f"WHERE {where_sql} {limit};",
            limit=limit2)
    else:
        chats = execute(f"""
        SELECT chat.* FROM chat
        WHERE chat.id not in (
            SELECT chat.id FROM chat_has_users
            INNER JOIN users on chat_has_users.users_id = users.id
            INNER JOIN chat on chat_has_users.chat_id = chat.id
            WHERE users_id={user_id}) 
            AND {where_sql} {limit};
        """,
                        limit=limit2)
    chats_obj = _get_chats_sql(chats,
                               is_messages=is_messages,
                               is_users=is_users)

    if chats_obj is not None and chat_id is None:
        return chats_obj
    if chats_obj is None and chat_id is None:
        return []
    if chats_obj is None:
        return None
    return chats_obj[0]
Exemplo n.º 3
0
def find_user(**vargs):
    str_args = convert_args_to_querystr(' AND ', query=vargs)
    users = get_users(where='WHERE %s' % str_args)
    return users[0] if users else None
Exemplo n.º 4
0
def update_user(id_user, **vargs):
    str_args = convert_args_to_querystr(', ', query=vargs)
    return execute(f"UPDATE users SET {str_args} WHERE id=%s", id_user)
Exemplo n.º 5
0
def find_chat(where={}, is_messages=False, is_users=False):
    str_args = convert_args_to_querystr(' AND ', query=where)
    chats = get_chats(where='WHERE %s' % str_args,
                      is_messages=is_messages,
                      is_users=is_users)
    return chats[0] if chats else None