Skip to content

Commit

Permalink
Fix ci/cd errs
Browse files Browse the repository at this point in the history
Signed-off-by: Hoang Pham <[email protected]>
  • Loading branch information
hweihwang committed Jul 1, 2024
1 parent c3fc74a commit 5b69076
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 37 deletions.
4 changes: 4 additions & 0 deletions lib/Controller/JWTController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Whiteboard\Controller;

Expand Down
8 changes: 6 additions & 2 deletions lib/Controller/WhiteboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ public function show(int $fileId): DataResponse {
return new DataResponse(['message' => 'Unauthorized'], Http::STATUS_UNAUTHORIZED);
}

[$jwt] = sscanf($authHeader, 'Bearer %s');
$assignedValues = sscanf($authHeader, 'Bearer %s', $jwt);

if (!$jwt) {
if (!$assignedValues) {
return new DataResponse(['message' => 'Unauthorized'], Http::STATUS_UNAUTHORIZED);
}

if (!$jwt || !is_string($jwt)) {
return new DataResponse(['message' => 'Unauthorized'], Http::STATUS_UNAUTHORIZED);
}

Expand Down
53 changes: 39 additions & 14 deletions src/collaboration/Portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import type { ExcalidrawElement } from '@excalidraw/excalidraw/types/element/types'
import { io, type Socket } from 'socket.io-client'
import type { Collab } from './collab'
import type { Gesture } from '@excalidraw/excalidraw/types/types'
import type { AppState, Gesture } from '@excalidraw/excalidraw/types/types'
import axios from '@nextcloud/axios'
import { loadState } from '@nextcloud/initial-state'

Expand Down Expand Up @@ -37,8 +37,8 @@ export class Portal {
const socket = io(collabBackendUrl, {
withCredentials: true,
auth: {
token
}
token,
},
})

this.open(socket)
Expand All @@ -51,12 +51,21 @@ export class Portal {
eventsNeedingTokenRefresh.forEach((event) =>
this.socket?.on(event, async () => {
await this.handleTokenRefresh()
})
}),
)

this.socket.on('read-only', () => this.handleReadOnlySocket())
this.socket.on('init-room', () => this.handleInitRoom())
this.socket.on('room-user-change', (users: any) => this.collab.updateCollaborators(users))
this.socket.on('room-user-change', (users: {
user: {
id: string,
name: string
},
socketId: string,
pointer: { x: number, y: number, tool: 'pointer' | 'laser' },
button: 'down' | 'up',
selectedElementIds: AppState['selectedElementIds']
}[]) => this.collab.updateCollaborators(users))
this.socket.on('client-broadcast', (data) => this.handleClientBroadcast(data))
}

Expand Down Expand Up @@ -85,12 +94,12 @@ export class Portal {
handleClientBroadcast(data: ArrayBuffer) {
const decoded = JSON.parse(new TextDecoder().decode(data))
switch (decoded.type) {
case BroadcastType.SceneInit:
this.handleSceneInit(decoded.payload.elements)
break
case BroadcastType.MouseLocation:
this.collab.updateCursor(decoded.payload)
break
case BroadcastType.SceneInit:
this.handleSceneInit(decoded.payload.elements)
break
case BroadcastType.MouseLocation:
this.collab.updateCursor(decoded.payload)
break
}
}

Expand All @@ -115,10 +124,22 @@ export class Portal {
}
}

async _broadcastSocketData(data: any, volatile: boolean = false, roomId?: string) {
async _broadcastSocketData(data: {
type: string;
payload: {
elements?: readonly ExcalidrawElement[];
socketId?: string;
pointer?: { x: number; y: number; tool: 'pointer' | 'laser' };
button?: 'down' | 'up';
selectedElementIds?: AppState['selectedElementIds'];
username?: string;
};
}, volatile: boolean = false, roomId?: string) {

const json = JSON.stringify(data)
const encryptedBuffer = new TextEncoder().encode(json)
this.socket?.emit(volatile ? 'server-volatile-broadcast' : 'server-broadcast', roomId ?? this.roomId, encryptedBuffer, [])

}

async broadcastScene(updateType: string, elements: readonly ExcalidrawElement[]) {
Expand All @@ -130,16 +151,20 @@ export class Portal {
button: 'down' | 'up';
pointersMap: Gesture['pointers'];
}) {

const data = {
type: BroadcastType.MouseLocation,
payload: {
socketId: this.socket?.id,
pointer: payload.pointer,
button: payload.button || 'up',
selectedElementIds: this.collab.excalidrawAPI.getAppState().selectedElementIds,
username: this.socket?.id
}
username: this.socket?.id,
},
}

await this._broadcastSocketData(data, true)

}

}
27 changes: 18 additions & 9 deletions src/collaboration/collab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export class Collab {

handleRemoteSceneUpdate = (elements: ExcalidrawElement[]) => {
this.excalidrawAPI.updateScene({
elements
}
elements,
},
)
}

Expand All @@ -71,13 +71,22 @@ export class Collab {
payload.pointersMap.size < 2 && this.portal.socket && this.portal.broadcastMouseLocation(payload)
}

updateCollaborators = (users: any[]) => {
updateCollaborators = (users: {
user: {
id: string,
name: string
},
socketId: string,
pointer: { x: number, y: number, tool: 'pointer' | 'laser' },
button: 'down' | 'up',
selectedElementIds: AppState['selectedElementIds']
}[]) => {
const collaborators = new Map<string, Collaborator>()

users.forEach((payload) => {
collaborators.set(payload.user.id, {
username: payload.user.name,
...payload
...payload,
})
})

Expand All @@ -100,8 +109,8 @@ export class Collab {
collaborators: this.collaborators.set(payload.user.id, {
...this.collaborators.get(payload.user.id),
...payload,
username: payload.user.name
})
username: payload.user.name,
}),
})
}

Expand All @@ -111,15 +120,15 @@ export class Collab {
this.excalidrawAPI.scrollToContent(elements, {
fitToContent: true,
animate: true,
duration: 500
duration: 500,
})
}

makeBoardReadOnly = () => {
this.excalidrawAPI.updateScene({
appState: {
viewModeEnabled: true
}
viewModeEnabled: true,
},
})
}

Expand Down
6 changes: 3 additions & 3 deletions websocket_server/roomData.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ dotenv.config()
const {
NEXTCLOUD_URL = 'http://nextcloud.local',
ADMIN_USER = 'admin',
ADMIN_PASS = 'admin'
ADMIN_PASS = 'admin',
} = process.env

export const roomDataStore = {}

const fetchOptions = (method, token, body = null) => {
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
Authorization: `Bearer ${token}`,
}

if (method === 'PUT') {
Expand All @@ -26,7 +26,7 @@ const fetchOptions = (method, token, body = null) => {
return {
method,
headers,
...(body && { body: JSON.stringify(body) })
...(body && { body: JSON.stringify(body) }),
}
}

Expand Down
4 changes: 2 additions & 2 deletions websocket_server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const {
PORT = 3002,
TLS,
TLS_KEY: keyPath,
TLS_CERT: certPath
TLS_CERT: certPath,
} = process.env

const FORCE_CLOSE_TIMEOUT = 60 * 60 * 1000

const readTlsCredentials = (keyPath, certPath) => ({
key: keyPath ? fs.readFileSync(keyPath) : undefined,
cert: certPath ? fs.readFileSync(certPath) : undefined
cert: certPath ? fs.readFileSync(certPath) : undefined,
})

const createConfiguredServer = (app) => {
Expand Down
14 changes: 7 additions & 7 deletions websocket_server/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dotenv.config()

const {
NEXTCLOUD_URL = 'http://nextcloud.local',
JWT_SECRET_KEY
JWT_SECRET_KEY,
} = process.env

const verifyToken = (token) => new Promise((resolve, reject) => {
Expand All @@ -31,8 +31,8 @@ export const initSocket = (server) => {
cors: {
origin: NEXTCLOUD_URL,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
credentials: true
}
credentials: true,
},
})

io.use(socketAuthenticateHandler)
Expand Down Expand Up @@ -90,7 +90,7 @@ const joinRoomHandler = async (socket, io, roomID) => {

io.in(roomID).emit('room-user-change', sockets.map((s) => ({
socketId: s.id,
user: s.decodedData.user
user: s.decodedData.user,
})))
}

Expand All @@ -114,8 +114,8 @@ const serverVolatileBroadcastHandler = (socket, roomID, encryptedData) => {
type: 'MOUSE_LOCATION',
payload: {
...payload.payload,
user: socket.decodedData.user
}
user: socket.decodedData.user,
},
}

const encodedEventData = convertStringToArrayBuffer(JSON.stringify(eventData))
Expand All @@ -139,7 +139,7 @@ const disconnectingHandler = async (socket, io) => {
if (otherClients.length > 0) {
socket.broadcast.to(roomID).emit('room-user-change', otherClients.map((s) => ({
socketId: s.id,
user: s.decodedData.user
user: s.decodedData.user,
})))
}
}
Expand Down

0 comments on commit 5b69076

Please sign in to comment.