def create_chat(): """ Creates a chat specified by a client Client must post json including: "chat_name": name for the chat "users": a list of users to add on creation of the chat """ # TODO: Add authentication allowing any logged in user to use data = request.get_json() chat_name = data.get("chat_name") try: new_chat = Chat.create(name=chat_name) except Exception as e: return str(e), 400 if data.get("users") is not None: try: for phone_number in data.get("users"): user = User.select().where( User.phone_number == phone_number).get() Participation.create(chat=new_chat, user=user, rank="member") except Exception as e: return str(e), 400 return jsonify(model_to_dict(new_chat)), 201
def chat_info(chat_id): chat = Chat.get(Chat.id == chat_id) users = [ model_to_dict(x.user) for x in Participation.select().where(Participation.chat == chat_id) ] return dict(chat=model_to_dict(chat), users=users)
def list_user_chats(user_id): """List all chats for a given user""" chats = [] raw_chats = Participation.select().where(Participation.user == user_id) for item in raw_chats: chat = Chat.get(Chat.id == item.chat) chats.append(model_to_dict(chat)) return jsonify(chats), 200
def chat_updates(user_id): """List all chat updates for a given user""" chats = [] raw_chats = Participation.select().where(Participation.user == user_id) for item in raw_chats: events = item.chat.events updates = events[item.cursor:] if updates: chats.append({"chat": int(item.chat.id), "events": updates}) return jsonify(chats), 200
def delete_participant(chat_id, user_id): try: participant = Participation.get(Participation.chat == chat_id, Participation.user == user_id) if participant.delete_instance(): return "", 204 else: return "", 400 except Exception as e: return str(e), 400
def delete_chat(chat_id): try: chat = Chat.get(Chat.id == chat_id) query = Participation.delete().where(Participation.chat == chat) query.execute() if chat.delete_instance(): return "", 204 else: return "", 400 except Exception as e: return str(e), 400
def update_cursor(chat_id, user_id): data = request.get_json() new_cursor = data.get("cursor") try: query = Participation.update(cursor=new_cursor).where( Participation.chat == chat_id, Participation.user == user_id) query.execute() return "", 204 except Exception as e: return str(e), 400
def update_participant(chat_id, user_id): data = request.get_json() is_admin = data.get("is_admin") try: query = Participation.update(is_admin=is_admin).where( Participation.chat == chat_id, Participation.user == user_id) query.execute() return "", 204 except Exception as e: return str(e), 400
def add_user_to_chat(chat_id): """ Adds a specified user to a chat identified by url "chat_id" Client must post json including: "is_admin": either 0 for member or 1 for admin Client must include in the json one of either: "user_id": the id of the user "phone_number": the phone number of the user """ # TODO: Add authentication allowing any member of the chat to use data = request.get_json() user_id = data.get("user_id") phone_number = data.get("phone_number") is_admin = data.get("is_admin") if is_admin is None or (user_id is None and phone_number is None): return str("Bad Data"), 400 try: Participation.create(chat=chat_id, user=user_id, is_admin=is_admin) except Exception as e: return str(e), 400 return "", 204