mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
Merge branch 'main' into ts-analytics
This commit is contained in:
23 files changed
+504
-180
No files matched your search
@@ -67,7 +67,7 @@ export function socialLoginHandler(req: Request, res: Response) {
|
||||
res.redirect('/choose-username')
|
||||
return
|
||||
}
|
||||
res.redirect('/')
|
||||
res.redirect('/map')
|
||||
}
|
||||
|
||||
export const registerFromSession = asyncHandler(
|
||||
|
||||
@@ -6,8 +6,6 @@ import { pushToSession } from '../socket.js'
|
||||
import { finishSession } from '../../models/sessionManager.js'
|
||||
import { asyncHandler } from '../../middleware/error.js'
|
||||
|
||||
const commandCompleteCallbacks = new Map()
|
||||
|
||||
export const newCommand = asyncHandler(async (req: Request, res: Response) => {
|
||||
const { command, pwd, output, error, params } = matchedData(req)
|
||||
|
||||
@@ -51,42 +49,14 @@ export const newCommand = asyncHandler(async (req: Request, res: Response) => {
|
||||
c.stage = response.stage
|
||||
await c.save()
|
||||
|
||||
if (response.stage === 'END') {
|
||||
session.responses.push(response.responses)
|
||||
|
||||
if ((session.status as string) === 'finished') {
|
||||
await finishSession(session)
|
||||
}
|
||||
commandCompleteCallbacks.set(session.id, response.callback)
|
||||
pushToSession(session.id, 'status', 'pending')
|
||||
res.status(201).json(response)
|
||||
} else {
|
||||
res.status(200).end()
|
||||
pushToSession(session.id, 'response', response)
|
||||
}
|
||||
|
||||
res.status(200).end()
|
||||
await session.save()
|
||||
})
|
||||
|
||||
export const completedCommand = asyncHandler(
|
||||
async (req: Request, res: Response) => {
|
||||
const { sessionId } = req.user as any
|
||||
|
||||
const session = await Session.findById(sessionId)
|
||||
|
||||
if (!session) {
|
||||
res.status(404).json({ message: 'session not found' })
|
||||
return
|
||||
}
|
||||
|
||||
if (session.status === 'inactive') {
|
||||
res.status(400).json({ message: 'session is not active' })
|
||||
return
|
||||
}
|
||||
|
||||
const callback = commandCompleteCallbacks.get(session.id)
|
||||
if (!callback) {
|
||||
res.status(200).json({ message: 'no callback found' })
|
||||
return
|
||||
}
|
||||
callback()
|
||||
commandCompleteCallbacks.delete(session.id)
|
||||
res.json({ message: 'ok' })
|
||||
}
|
||||
)
|
||||
@@ -25,6 +25,7 @@ function sessionDetail(session: ISession) {
|
||||
...sessionSummary(session),
|
||||
hints: session.hints,
|
||||
tasks: session.tasks,
|
||||
responses: session.responses,
|
||||
graph: session.graph,
|
||||
}
|
||||
}
|
||||
@@ -55,6 +56,7 @@ export const createSession = asyncHandler(
|
||||
export const getSessionById = asyncHandler(
|
||||
async (req: Request, res: Response) => {
|
||||
const { sessionId } = matchedData(req)
|
||||
console.log(sessionId)
|
||||
const session = await Session.findOne({
|
||||
_id: sessionId,
|
||||
user: (req.user as Express.ExistingUser).id,
|
||||
|
||||
@@ -5,6 +5,5 @@ import { newCommandValidator } from '../validators/commandValidator.js'
|
||||
const router = Router()
|
||||
|
||||
router.post('/', newCommandValidator, commandController.newCommand)
|
||||
router.post('/completed', commandController.completedCommand)
|
||||
|
||||
export default router
|
||||
@@ -22,6 +22,10 @@ router.post(
|
||||
activeSessionValidator,
|
||||
sessionController.getOrCreateSession
|
||||
)
|
||||
router.get('/:id', sessionDetailValidator, sessionController.getSessionById)
|
||||
router.get(
|
||||
'/:sessionId',
|
||||
sessionDetailValidator,
|
||||
sessionController.getSessionById
|
||||
)
|
||||
|
||||
export default router
|
||||
@@ -9,6 +9,6 @@ export const checkSessionId = () =>
|
||||
check('sessionId').notEmpty().isString().isMongoId()
|
||||
|
||||
export const sessionListValidator = [checkQuestId(), checkSessionStatus()]
|
||||
export const sessionDetailValidator = [checkSessionId(), noError]
|
||||
export const sessionDetailValidator = [checkSessionId()]
|
||||
export const createSessionValidator = [checkQuestId(), noError]
|
||||
export const activeSessionValidator = [checkQuestId(), noError]
|
||||
@@ -8,12 +8,12 @@ import type {
|
||||
IStage,
|
||||
ITask,
|
||||
} from '@linux-odyssey/models'
|
||||
import { pushToSession } from '../api/socket.js'
|
||||
|
||||
export interface ExecuteResult {
|
||||
responses: IResponse[]
|
||||
hints: string[]
|
||||
callback: () => void
|
||||
tasks?: ITask[]
|
||||
status: string
|
||||
stage?: string
|
||||
end?: boolean
|
||||
}
|
||||
@@ -79,15 +79,17 @@ export default class SessionHandler {
|
||||
|
||||
this.addNewTasks()
|
||||
|
||||
this.session.responses.push(stage.responses)
|
||||
|
||||
if (stage.id === 'END') {
|
||||
this.session.status = 'finished'
|
||||
}
|
||||
|
||||
return {
|
||||
responses: stage.responses,
|
||||
hints: stage.hints,
|
||||
callback: () => {
|
||||
pushToSession(this.session.id, 'tasks', this.session.tasks)
|
||||
if (stage.hints && stage.hints.length > 0)
|
||||
pushToSession(this.session.id, 'hints', stage.hints)
|
||||
pushToSession(this.session.id, 'status', this.session.status)
|
||||
},
|
||||
tasks: this.session.tasks,
|
||||
status: this.session.status,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,11 +100,7 @@ export default class SessionHandler {
|
||||
return {
|
||||
responses,
|
||||
hints,
|
||||
callback: () => {
|
||||
if (hints && hints.length > 0)
|
||||
pushToSession(this.session.id, 'hints', hints)
|
||||
pushToSession(this.session.id, 'status', this.session.status)
|
||||
},
|
||||
status: this.session.status,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user