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}
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']}
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}
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}
def get_photo_by_id(id): photos = sql.list({'table': 'photos'}) for p in photos: if p['id'] == id: return p return None
def get_chat_by_id(id): chats = sql.list({'table': 'chats'}) for c in chats: if c['id'] == id: return c return None
def get_user_by_id(id): users = sql.list({'table': 'users'}) for u in users: if u['id'] == id: return u return None
def get_user_by_token(token): users = sql.list({'table': 'users'}) for u in users: if u['token'] == token: return u return None
def is_user_exist(token): users = sql.list({'table': 'users'}) for u in users: if u['token'] == token: return True return False