def api_get_messages(chat_id): user = get_user(username=auth.username()) chat = user.get_chat(chat_id) if not chat: return return_error('i have not got that chat') return jsonify({'chat': chat.to_json(), 'messages': to_json(get_messages(chat))})
def api_friend_delete(): js = get_json() if not js: return return_error('corrupted json') if not js.get('friend_name'): return return_error('i need friend name') user = get_user(username=auth.username()) friend = get_user(username=js['friend_name']) if not friend: return return_error('wrong friend name') re = user.delete_friend(friend) if re: return return_error(re) else: return return_ok()
def api_delete_chat_for_user(): js = get_json() if not js: return return_error('corrupted json') user = get_user(username=auth.username()) if not js.get('id'): return return_error("i need id of chat") re = delete_chat(user, js['id']) if re: return return_error(re) else: return return_ok()
def api_post_message(chat_id): js = get_json() if not js: return return_error('corrupted json') user = get_user(username=auth.username()) if not js.get('text'): return return_error('i need text of message') re = post_message(user, chat_id, js['text']) if re: return return_error(re) else: return return_ok()
def api_create_chat(): js = get_json() if not js: return return_error('corrupted json') user = get_user(username=auth.username()) if not js.get('id'): js['id'] = get_max_id(Chat) + 1 if not js.get('title'): js['title'] = js['id'] re = create_chat(user, js.get('members', []), js['id'], js['title']) if re: return return_error(re) else: return return_ok()
def api_friend_get(): user = get_user(username=auth.username()) return jsonify({'friends': to_json(user.get_friends())})
def api_login(): user = get_user(username=auth.username()) return jsonify(user.to_json())
def api_get_chats(): user = get_user(username=auth.username()) return jsonify({'chats': to_json(user.get_chats())})