recoder-code
Version:
Complete AI-powered development platform with ML model training, plugin registry, real-time collaboration, monitoring, infrastructure automation, and enterprise deployment capabilities
204 lines (203 loc) • 6.79 kB
JavaScript
"use strict";
/**
* Session Management API Routes
* Handle collaboration session creation and management
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const uuid_1 = require("uuid");
const logger_1 = __importDefault(require("../utils/logger"));
const router = (0, express_1.Router)();
const sessions = new Map();
// Create a new collaboration session
router.post('/', async (req, res) => {
try {
const { name = 'Untitled Session' } = req.body;
const userId = req.headers['x-user-id'] || 'anonymous';
const sessionId = (0, uuid_1.v4)();
const session = {
id: sessionId,
name,
createdAt: new Date(),
createdBy: userId,
participants: [],
isActive: true
};
sessions.set(sessionId, session);
logger_1.default.info(`Session created: ${sessionId} by ${userId}`);
res.status(201).json({
sessionId,
name: session.name,
createdAt: session.createdAt,
message: 'Session created successfully'
});
}
catch (error) {
logger_1.default.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, res) => {
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_1.default.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, res) => {
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_1.default.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, res) => {
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)) {
session.participants.push(userId);
}
logger_1.default.info(`User ${userId} joined session ${sessionId}`);
res.json({
sessionId,
message: 'Successfully joined session',
participants: session.participants
});
}
catch (error) {
logger_1.default.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, res) => {
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);
if (index > -1) {
session.participants.splice(index, 1);
}
logger_1.default.info(`User ${userId} left session ${sessionId}`);
res.json({
sessionId,
message: 'Successfully left session',
participants: session.participants
});
}
catch (error) {
logger_1.default.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, res) => {
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_1.default.info(`Session ${sessionId} deleted by ${userId}`);
res.json({
sessionId,
message: 'Session deleted successfully'
});
}
catch (error) {
logger_1.default.error('Error deleting session:', error);
res.status(500).json({
error: 'Failed to delete session',
message: error instanceof Error ? error.message : 'Unknown error'
});
}
});
exports.default = router;