def delete(message_id: str, user_id: str): try: del_message = logic.delete_messages(message_id, user_id) except: raise internal_errors.InternalError(detail="incorrect id format") if not del_message: raise user_errors.NotFoundError(obj=f"Message {message_id}") return del_message
def invite_member(room_id: str, member_name: str): try: member_id = get_user_id(member_name) if not member_id: raise user_errors.NotFoundError(obj=f"User {member_name}") member = logic.check_member_exists(room_id, member_id) except Exception as e: raise room_errors.IdFormatError() if member: raise room_errors.ExistingError(obj=f"Member {member_name}") return logic.invite_member(room_id, member_name)
def update(message_id: str, user_id: str,content: str): try: message_exist = logic.get_one_message(message_id) except: raise internal_errors.InternalError(detail="incorrect id format") if message_exist: try: return logic.update_messages(message_id, user_id,content) except: raise internal_errors.InternalError(detail="Update failed") else: return user_errors.NotFoundError(obj=f"Messages {message_id}")
def update_user(user_id: str, username: str, avatar: Optional[str] = None, name: Optional[str] = None): existing_user = logic.get_user_by_id(user_id) if existing_user: try: user_update = logic.update_current_user(user_id, username, avatar, name) except: raise internal_errors.InternalError(detail="Could not update user") return user_update else: raise user_errors.NotFoundError(obj=f"User {user_id}")
def send_message(content: str, sender_id: Optional[str] = None, room_id: Optional[str] = None, username: Optional[str] = None): try: is_room = check_room_exists(room_id) except: raise internal_errors.InternalError(detail="Incorrect room id format") if not is_room: raise room_errors.NotFoundError(obj=f"Room {room_id}") try: sender_exist = check_user_exist(sender_id) except: raise internal_errors.InternalError(detail="Incorrect user id format") if not sender_exist: raise user_errors.NotFoundError(obj=f"Sender {sender_id}") if is_room: try: message = logic.send_to_user(content, sender_id, room_id) except: raise internal_errors.InternalError(detail="Check id format") _message = message.to_dict() _message["username"] = username event = {"event_type": "new_message", "payload": _message} members = room_members(room_id) for member in members: if (member != sender_id): publish_event(redis_uri=settings.REDIS_URI, channel=member, event=event) return message return False
def delete_user(username: str): existing_user = logic.get_user(username) if existing_user: return logic.disabe_user(username) else: raise user_errors.NotFoundError(obj=f"User {username}")