recoder-code
Version:
Complete AI-powered development platform with ML model training, plugin registry, real-time collaboration, monitoring, infrastructure automation, and enterprise deployment capabilities
239 lines (201 loc) • 6.18 kB
text/typescript
/**
* Session Management API Routes
* Handle collaboration session creation and management
*/
import { Router, Request, Response } from 'express';
import { v4 as uuidv4 } from 'uuid';
import logger from '../utils/logger';
const router = Router();
// In-memory session storage (in production, use Redis or database)
interface Session {
id: string;
name: string;
createdAt: Date;
createdBy: string;
participants: string[];
isActive: boolean;
}
const sessions = new Map<string, Session>();
// Create a new collaboration session
router.post('/', async (req: Request, res: Response) => {
try {
const { name = 'Untitled Session' } = req.body;
const userId = req.headers['x-user-id'] || 'anonymous';
const sessionId = uuidv4();
const session: Session = {
id: sessionId,
name,
createdAt: new Date(),
createdBy: userId as string,
participants: [],
isActive: true
};
sessions.set(sessionId, session);
logger.info(`Session created: ${sessionId} by ${userId}`);
res.status(201).json({
sessionId,
name: session.name,
createdAt: session.createdAt,
message: 'Session created successfully'
});
} catch (error) {
logger.error('Error creating session:', error);
res.status(500).json({
error: 'Failed to create session',
message: error instanceof Error ? error.message : 'Unknown error'
});
}
});
// Get session information
router.get('/:sessionId', async (req: Request, res: Response) => {
try {
const { sessionId } = req.params;
const session = sessions.get(sessionId);
if (!session) {
return res.status(404).json({
error: 'Session not found',
sessionId
});
}
res.json({
sessionId: session.id,
name: session.name,
createdAt: session.createdAt,
createdBy: session.createdBy,
participants: session.participants,
isActive: session.isActive,
participantCount: session.participants.length
});
} catch (error) {
logger.error('Error getting session:', error);
res.status(500).json({
error: 'Failed to get session',
message: error instanceof Error ? error.message : 'Unknown error'
});
}
});
// List all active sessions
router.get('/', async (req: Request, res: Response) => {
try {
const activeSessions = Array.from(sessions.values())
.filter(session => session.isActive)
.map(session => ({
sessionId: session.id,
name: session.name,
createdAt: session.createdAt,
participantCount: session.participants.length
}));
res.json({
sessions: activeSessions,
total: activeSessions.length
});
} catch (error) {
logger.error('Error listing sessions:', error);
res.status(500).json({
error: 'Failed to list sessions',
message: error instanceof Error ? error.message : 'Unknown error'
});
}
});
// Join a session
router.post('/:sessionId/join', async (req: Request, res: Response) => {
try {
const { sessionId } = req.params;
const userId = req.headers['x-user-id'] || 'anonymous';
const session = sessions.get(sessionId);
if (!session) {
return res.status(404).json({
error: 'Session not found',
sessionId
});
}
if (!session.isActive) {
return res.status(410).json({
error: 'Session is no longer active',
sessionId
});
}
// Add user to participants if not already present
if (!session.participants.includes(userId as string)) {
session.participants.push(userId as string);
}
logger.info(`User ${userId} joined session ${sessionId}`);
res.json({
sessionId,
message: 'Successfully joined session',
participants: session.participants
});
} catch (error) {
logger.error('Error joining session:', error);
res.status(500).json({
error: 'Failed to join session',
message: error instanceof Error ? error.message : 'Unknown error'
});
}
});
// Leave a session
router.post('/:sessionId/leave', async (req: Request, res: Response) => {
try {
const { sessionId } = req.params;
const userId = req.headers['x-user-id'] || 'anonymous';
const session = sessions.get(sessionId);
if (!session) {
return res.status(404).json({
error: 'Session not found',
sessionId
});
}
// Remove user from participants
const index = session.participants.indexOf(userId as string);
if (index > -1) {
session.participants.splice(index, 1);
}
logger.info(`User ${userId} left session ${sessionId}`);
res.json({
sessionId,
message: 'Successfully left session',
participants: session.participants
});
} catch (error) {
logger.error('Error leaving session:', error);
res.status(500).json({
error: 'Failed to leave session',
message: error instanceof Error ? error.message : 'Unknown error'
});
}
});
// Delete/close a session
router.delete('/:sessionId', async (req: Request, res: Response) => {
try {
const { sessionId } = req.params;
const userId = req.headers['x-user-id'] || 'anonymous';
const session = sessions.get(sessionId);
if (!session) {
return res.status(404).json({
error: 'Session not found',
sessionId
});
}
// Only creator can delete session
if (session.createdBy !== userId) {
return res.status(403).json({
error: 'Only session creator can delete the session',
sessionId
});
}
session.isActive = false;
sessions.delete(sessionId);
logger.info(`Session ${sessionId} deleted by ${userId}`);
res.json({
sessionId,
message: 'Session deleted successfully'
});
} catch (error) {
logger.error('Error deleting session:', error);
res.status(500).json({
error: 'Failed to delete session',
message: error instanceof Error ? error.message : 'Unknown error'
});
}
});
export default router;