From e5b915195850dfa814604a791664c4f2006a0e0a Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Mon, 23 Dec 2024 11:46:30 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=9A(frontend)=20add=20routes=20"/colla?= =?UTF-8?q?boration/ws/poll/"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We add the routes "/collaboration/ws/poll/". We updated the useCollaborationUrl hook to return the new route. We update ngnix to accept OPTIONS requests. --- docker/files/etc/nginx/conf.d/default.conf | 8 ++++++++ .../core/config/hooks/useCollaborationUrl.tsx | 19 +++++++++++++++++-- .../doc-management/hooks/useCollaboration.tsx | 10 +++++++--- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/docker/files/etc/nginx/conf.d/default.conf b/docker/files/etc/nginx/conf.d/default.conf index 2644822ce..e733e9c73 100644 --- a/docker/files/etc/nginx/conf.d/default.conf +++ b/docker/files/etc/nginx/conf.d/default.conf @@ -6,6 +6,14 @@ server { # Proxy auth for collaboration server location /collaboration/ws/ { + if ($request_method = OPTIONS) { + add_header 'Access-Control-Allow-Origin' 'http://localhost:3000'; + add_header 'Access-Control-Allow-Credentials' 'true'; + add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; + add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type'; + return 204; + } + # Collaboration Auth request configuration auth_request /collaboration-auth; auth_request_set $authHeader $upstream_http_authorization; diff --git a/src/frontend/apps/impress/src/core/config/hooks/useCollaborationUrl.tsx b/src/frontend/apps/impress/src/core/config/hooks/useCollaborationUrl.tsx index b06683729..c537dddb1 100644 --- a/src/frontend/apps/impress/src/core/config/hooks/useCollaborationUrl.tsx +++ b/src/frontend/apps/impress/src/core/config/hooks/useCollaborationUrl.tsx @@ -1,6 +1,13 @@ import { useConfig } from '../api'; -export const useCollaborationUrl = (room?: string) => { +export type CollaborationUrl = { + wsUrl: string; + pollUrl: string; +}; + +export const useCollaborationUrl = ( + room?: string, +): CollaborationUrl | undefined => { const { data: conf } = useConfig(); if (!room) { @@ -13,5 +20,13 @@ export const useCollaborationUrl = (room?: string) => { ? `wss://${window.location.host}/collaboration/ws/` : ''); - return `${base}?room=${room}`; + const wsUrl = `${base}?room=${room}`; + + let pollUrl = wsUrl.replace('/ws/', '/ws/poll/'); + pollUrl = pollUrl.replace('ws:', 'http:'); + if (pollUrl.includes('wss:')) { + pollUrl = pollUrl.replace('wss:', 'https:'); + } + + return { wsUrl, pollUrl }; }; diff --git a/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx b/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx index 848ccd005..8f32f4100 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx @@ -12,15 +12,19 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => { const { provider, createProvider, destroyProvider } = useProviderStore(); useEffect(() => { - if (!room || !collaborationUrl || provider) { + if (!room || !collaborationUrl?.wsUrl || provider) { return; } - const newProvider = createProvider(collaborationUrl, room, initialContent); + const newProvider = createProvider( + collaborationUrl.wsUrl, + room, + initialContent, + ); setBroadcastProvider(newProvider); }, [ provider, - collaborationUrl, + collaborationUrl?.wsUrl, room, initialContent, createProvider,