mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-12 13:13:45 +01:00
* [editor-core] extend coverage of zod schemas * [real-time] adopt strict zod types and extend test coverage * [project-history] make type check happy * [web] make type check happy GitOrigin-RevId: e1a65b9b3560b31ad2af817067a4b549865a5bb1
66 lines
2.0 KiB
JavaScript
66 lines
2.0 KiB
JavaScript
import { z, parseReq } from '@overleaf/validation-tools'
|
|
import { clientIdSchema } from './schemas.js'
|
|
|
|
let HttpController
|
|
|
|
const getConnectedClientSchema = z.object({
|
|
params: z.strictObject({
|
|
client_id: clientIdSchema,
|
|
}),
|
|
})
|
|
|
|
export default HttpController = {
|
|
// The code in this controller is hard to unit test because of a lot of
|
|
// dependencies on internal socket.io methods. It is not critical to the running
|
|
// of Overleaf, and is only used for getting stats about connected clients,
|
|
// and for checking internal state in acceptance tests. The acceptances tests
|
|
// should provide appropriate coverage.
|
|
_getConnectedClientView(ioClient) {
|
|
const clientId = ioClient.id
|
|
const {
|
|
project_id: projectId,
|
|
user_id: userId,
|
|
first_name: firstName,
|
|
last_name: lastName,
|
|
email,
|
|
connected_time: connectedTime,
|
|
} = ioClient.ol_context
|
|
const client = {
|
|
client_id: clientId,
|
|
project_id: projectId,
|
|
user_id: userId,
|
|
first_name: firstName,
|
|
last_name: lastName,
|
|
email,
|
|
connected_time: connectedTime,
|
|
}
|
|
client.rooms = Object.keys(ioClient.manager.roomClients[clientId] || {})
|
|
// drop the namespace
|
|
.filter(room => room !== '')
|
|
// room names are composed as '<NAMESPACE>/<ROOM>' and the default
|
|
// namespace is empty (see comments in RoomManager), just drop the '/'
|
|
.map(fullRoomPath => fullRoomPath.slice(1))
|
|
return client
|
|
},
|
|
|
|
getConnectedClients(req, res) {
|
|
const io = req.app.get('io')
|
|
const ioClients = io.sockets.clients()
|
|
|
|
res.json(ioClients.map(HttpController._getConnectedClientView))
|
|
},
|
|
|
|
getConnectedClient(req, res) {
|
|
const {
|
|
params: { client_id: clientId },
|
|
} = parseReq(req, getConnectedClientSchema, { logOnly: true })
|
|
const io = req.app.get('io')
|
|
const ioClient = io.sockets.sockets[clientId]
|
|
if (!ioClient) {
|
|
res.sendStatus(404)
|
|
return
|
|
}
|
|
res.json(HttpController._getConnectedClientView(ioClient))
|
|
},
|
|
}
|