diff --git a/src/api/events.py b/src/api/events.py index 9d5b8e6..662fbdf 100644 --- a/src/api/events.py +++ b/src/api/events.py @@ -320,6 +320,30 @@ async def delete_event(event_id: PydanticObjectId, user: CurrentUserDep) -> Resp return Response(status_code=status.HTTP_204_NO_CONTENT) +@events_router.delete("/{event_id}/invited") +async def delete_invited_event(event_id: PydanticObjectId, user: CurrentUserDep) -> Response: + """Remove yourself as an attendee of given event.""" + event = await Event.get(event_id, fetch_links=True) + if event is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Event with given id doesn't exist") + + for attendee in event.attendees: + if cast(User, attendee).id == user.id: + break + else: # nobreak + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail="You can only access events you attend", + ) + + event.attendees.remove(attendee) + _ = await event.replace() + + # NOTE: We might want to notify the event owner about the attendee leaving + + return Response(status_code=status.HTTP_204_NO_CONTENT) + + @events_router.post("") async def create_event( user: CurrentUserDep,