async def chat_create(): manager = chat.get_chat_manager() create_room_form = CreateRoomForm() if create_room_form.validate_on_submit(): form = create_room_form room_name = form.room_name.data password = None if not len(form.password.data) else form.password.data is_public = form.public.data guest_limit = (None if form.guest_limit.data == 0 else form.guest_limit.data) admin_list = [] try: room = manager.create_room(room_name, password=password, guest_limit=guest_limit, admin_list=admin_list, is_public=is_public) return redirect('/chat/{}'.format(room.id), code=307) except ChatException: await flash('The room name "{}" is not available'.format(room_name) ) form_join = JoinRoomForm() public_rooms = manager.get_public_rooms() return await render_template('chat.html', create_room_form=create_room_form, form_join=form_join, public_rooms=public_rooms)
async def chat_room(room_id): manager = chat.get_chat_manager() room = manager.get_room(room_id) if room is None: return '404', 404 if room.is_full(): return 'Guest limit already reached', 418 if room.authenticate(): # i.e. a password is not required return await render_template('chatroom.html', title='Camus | {}'.format(room.name)) form = JoinRoomForm() if form.validate_on_submit(): password = form.password.data if room.authenticate(password): # TODO: Generate token to be used with websocket return await render_template('chatroom.html', title='Camus | {}'.format(room.name)) await flash('Invalid password') return await render_template('join-room.html', title='Camus | Join a room', form=form, room=room)
async def chat_create(): manager = chat.get_chat_manager() form_create = RoomCreate() if form_create.validate_on_submit(): form = form_create room_name = form.room_name.data password = None if not len(form.password.data) else form.password.data is_public = form.public.data guest_limit = None if form.guest_limit.data == 0 else form.guest_limit.data admin_list = [] try: room = manager.create_room(room_name, password=password, guest_limit=guest_limit, admin_list=admin_list, is_public=is_public) return redirect('/chat/{}'.format(room.id)) except ChatException as e: await flash('Room name not available') form_join = RoomJoin() public_rooms = manager.get_public_rooms() return await render_template('chat.html', form_create=form_create, form_join=form_join, public_rooms=public_rooms)
async def public(): manager = chat.get_chat_manager() public_rooms = manager.get_public_rooms() return await render_template('public.html', title='Camus Video Chat | Public Rooms', public_rooms=public_rooms)
async def test_get_public_rooms(self): manager = get_chat_manager() public_room = manager.create_room('public', is_public=True) assert public_room in manager.get_public_rooms() private_room = manager.create_room('private', is_public=False) assert private_room not in manager.get_public_rooms()
async def test_remove_client(self): manager = get_chat_manager() client = manager.create_client() room = manager.create_room('rubbish') room.add_client(client) await manager.remove_client(client) assert client.id not in manager.clients assert client not in manager.clients.values()
async def test_create_room(self): manager = get_chat_manager() room_id = uuid.uuid4().hex room = manager.create_room(room_id) assert room is not None assert room.id == room_id assert room_id in manager.rooms assert manager.rooms[room_id] == room
async def test_create_client(self): manager = get_chat_manager() client = manager.create_client() assert client is not None assert client.id is not None client_id = uuid.uuid4().hex client = manager.create_client(client_id) assert client is not None assert client.id == client_id
async def chat_room_ws(room_id): manager = chat.get_chat_manager() room = manager.get_room(room_id) if room is None: return # close the websocket client = manager.create_client() room.add_client(client) send_task = asyncio.create_task( copy_current_websocket_context(ws_send)(client.outbox), ) receive_task = asyncio.create_task( copy_current_websocket_context(ws_receive)(client.inbox), ) try: await asyncio.gather(send_task, receive_task) finally: send_task.cancel() receive_task.cancel()
def test_get_chat_manager(): assert get_chat_manager() is not None
async def test_add_room(self): manager = get_chat_manager() room = ChatRoom('waste') manager.add_room(room) assert room.id in manager.rooms
async def test_get_room(self): manager = get_chat_manager() room = manager.create_room('refuse') got_room = manager.get_room(room.id) assert room == got_room