(frontend) create room in db when creating a new room

With unregistered rooms being now forbidden, we need to call create a room
in our Django db to get an access token.

Clicking on the 'Create room' will create a new entry using a Post request.
The output serialized already contains an access token to the LiveKit server,
thus we won't need to run the useQuery hook when navigating to the room.

/!\ this modification now prevent authenticated users to create rooms
by simply navigating to it. It'll be fixed in the upcoming commits.
This commit is contained in:
lebaudantoine
2024-07-31 17:47:28 +02:00
committed by aleb_the_flash
parent b261f2ee5b
commit fb0ee9e8f6
6 changed files with 51 additions and 4 deletions

View File

@@ -8,10 +8,20 @@ import { Centered } from '@/layout/Centered'
import { generateRoomId } from '@/features/rooms'
import { authUrl, useUser, UserAware } from '@/features/auth'
import { JoinMeetingDialog } from '../components/JoinMeetingDialog'
import { useCreateRoom } from '@/features/rooms'
export const Home = () => {
const { t } = useTranslation('home')
const { isLoggedIn } = useUser()
const { mutateAsync: createRoom } = useCreateRoom({
onSuccess: (data) => {
navigateTo('room', data.slug, {
state: { create: true, initialRoomData: data },
})
}
});
return (
<UserAware>
<Screen>
@@ -32,10 +42,10 @@ export const Home = () => {
variant="primary"
onPress={
isLoggedIn
? () =>
navigateTo('room', generateRoomId(), {
state: { create: true },
})
? async () => {
const slug = generateRoomId()
await createRoom({slug})
}
: undefined
}
href={isLoggedIn ? undefined : authUrl()}

View File

@@ -8,4 +8,7 @@ export type ApiRoom = {
room: string
token: string
}
configuration?: {
[key: string]: string | number | boolean;
}
}

View File

@@ -0,0 +1,25 @@
import { useMutation, UseMutationOptions } from "@tanstack/react-query";
import { fetchApi } from '@/api/fetchApi';
import { ApiError } from "@/api/ApiError";
import { ApiRoom } from "./ApiRoom";
export interface CreateRoomParams {
slug: string;
}
const createRoom = ({slug}: CreateRoomParams): Promise<ApiRoom> => {
return fetchApi(`rooms/`, {
method: 'POST',
body: JSON.stringify({
name: slug,
}),
})
};
export function useCreateRoom(options?: UseMutationOptions<ApiRoom, ApiError, CreateRoomParams>) {
return useMutation<ApiRoom, ApiError, CreateRoomParams>({
mutationFn: createRoom,
onSuccess: options?.onSuccess,
});
}

View File

@@ -11,24 +11,30 @@ import { navigateTo } from '@/navigation/navigateTo'
import { Screen } from '@/layout/Screen'
import { QueryAware } from '@/components/QueryAware'
import { fetchRoom } from '../api/fetchRoom'
import { ApiRoom } from '../api/ApiRoom'
import { InviteDialog } from './InviteDialog'
export const Conference = ({
roomId,
userConfig,
initialRoomData,
mode = 'join',
}: {
roomId: string
userConfig: LocalUserChoices
mode?: 'join' | 'create'
initialRoomData?: ApiRoom
}) => {
const { status, data } = useQuery({
queryKey: [keys.room, roomId, userConfig.username],
enabled: !initialRoomData,
initialData: initialRoomData,
queryFn: () =>
fetchRoom({
roomId: roomId as string,
username: userConfig.username,
}),
retry: false,
})
const roomOptions = useMemo((): RoomOptions => {

View File

@@ -2,3 +2,4 @@ export { Room as RoomRoute } from './routes/Room'
export { FeedbackRoute } from './routes/Feedback'
export { roomIdPattern, isRoomValid } from './utils/isRoomValid'
export { generateRoomId } from './utils/generateRoomId'
export { useCreateRoom } from './api/createRoom'

View File

@@ -15,6 +15,7 @@ export const Room = () => {
const [userConfig, setUserConfig] = useState<LocalUserChoices | null>(null)
const { roomId } = useParams()
const initialRoomData = history.state?.initialRoomData
const mode = isLoggedIn && history.state?.create ? 'create' : 'join'
const skipJoinScreen = isLoggedIn && mode === 'create'
@@ -33,6 +34,7 @@ export const Room = () => {
return (
<UserAware>
<Conference
initialRoomData={initialRoomData}
roomId={roomId}
mode={mode}
userConfig={{