Esempio n. 1
0
def all_users(token):
    if not is_user_exist(token):
        return {'result': '1', 'error': 'no such user'}

    users = sql.list({'table': 'users'})
    filtered = []
    for u in users:
        u.pop('token')
        u.pop('pass')
        filtered.append(u)

    return {'result': '0', 'data': filtered}
Esempio n. 2
0
def get_token(name, password):
    users = sql.list({'table': 'users'})
    user = None
    for u in users:
        if u['name'] == name and u['pass'] == password:
            user = u
            break

    if user == None:
        return {'result': '1', 'error': 'no user with such name and pass'}

    return {'result': '0', 'token': user['token']}
Esempio n. 3
0
def all_chats(token):
    if not is_user_exist(token):
        return {'result': '1', 'error': 'no such user'}

    user = get_user_by_token(token)
    chats = sql.list({'table': 'chats'})
    user_chats = []
    for c in chats:
        if c['first_user_id'] == user['id'] or c['second_user_id'] == user[
                'id']:
            user_chats.append(c)

    return {'result': '0', 'data': user_chats}
Esempio n. 4
0
def all_photos(token, chat_id):
    if not is_user_exist(token):
        return {'result': '1', 'error': 'no such user'}

    chat = get_chat_by_id(chat_id)
    if chat == None:
        return {'result': '1', 'error': 'no such chat'}

    user = get_user_by_token(token)
    if user['id'] != chat['first_user_id'] and user['id'] != chat[
            'second_user_id']:
        return {'result': '1', 'error': 'user is not a member of chat'}

    selected = []
    photos = sql.list({'table': 'photos'})
    for p in photos:
        if p['chat_id'] == chat_id:
            selected.append(p)
    return {'result': '1', 'error': selected}
Esempio n. 5
0
def get_photo_by_id(id):
    photos = sql.list({'table': 'photos'})
    for p in photos:
        if p['id'] == id:
            return p
    return None
Esempio n. 6
0
def get_chat_by_id(id):
    chats = sql.list({'table': 'chats'})
    for c in chats:
        if c['id'] == id:
            return c
    return None
Esempio n. 7
0
def get_user_by_id(id):
    users = sql.list({'table': 'users'})
    for u in users:
        if u['id'] == id:
            return u
    return None
Esempio n. 8
0
def get_user_by_token(token):
    users = sql.list({'table': 'users'})
    for u in users:
        if u['token'] == token:
            return u
    return None
Esempio n. 9
0
def is_user_exist(token):
    users = sql.list({'table': 'users'})
    for u in users:
        if u['token'] == token:
            return True
    return False