|
1 | 1 | import express from "express"; |
2 | 2 | import roomController from "../controllers/roomController.js"; |
| 3 | +import { authenticateHttp } from "../middleware/auth.js"; |
3 | 4 |
|
4 | 5 | const router = express.Router(); |
5 | 6 |
|
6 | 7 | /** |
7 | 8 | * POST /api/v1/rooms |
8 | 9 | * Create a new room |
| 10 | + * @deprecated This route is disabled since room creation is now handled by matching service through Kafka |
9 | 11 | */ |
10 | | -router.post("/", async (req, res) => { |
11 | | - try { |
12 | | - const { questionId, userIds, programmingLanguage } = req.body; |
13 | | - |
14 | | - if (!userIds || !Array.isArray(userIds) || userIds.length === 0) { |
15 | | - return res.status(400).json({ |
16 | | - success: false, |
17 | | - error: "userIds is required and must be a non-empty array", |
18 | | - }); |
19 | | - } |
20 | | - |
21 | | - const room = await roomController.create(null, questionId, userIds, programmingLanguage); |
22 | | - res.json({ |
23 | | - success: true, |
24 | | - room, |
25 | | - }); |
26 | | - } catch (error) { |
27 | | - console.error("Failed to create room:", error); |
28 | | - res.status(500).json({ |
29 | | - success: false, |
30 | | - error: error.message, |
31 | | - }); |
32 | | - } |
33 | | -}); |
| 12 | +// router.post("/", async (req, res) => { |
| 13 | +// try { |
| 14 | +// const { questionId, userIds, programmingLanguage } = req.body; |
| 15 | + |
| 16 | +// if (!userIds || !Array.isArray(userIds) || userIds.length === 0) { |
| 17 | +// return res.status(400).json({ |
| 18 | +// success: false, |
| 19 | +// error: "userIds is required and must be a non-empty array", |
| 20 | +// }); |
| 21 | +// } |
| 22 | + |
| 23 | +// const room = await roomController.create(null, questionId, userIds, programmingLanguage); |
| 24 | +// res.json({ |
| 25 | +// success: true, |
| 26 | +// room, |
| 27 | +// }); |
| 28 | +// } catch (error) { |
| 29 | +// console.error("Failed to create room:", error); |
| 30 | +// res.status(500).json({ |
| 31 | +// success: false, |
| 32 | +// error: error.message, |
| 33 | +// }); |
| 34 | +// } |
| 35 | +// }); |
34 | 36 |
|
35 | 37 | /** |
36 | 38 | * GET /api/v1/rooms/:roomId |
37 | 39 | * Get room information and document content |
| 40 | + * Requires authentication and authorization (user must be in room) |
38 | 41 | */ |
39 | | -router.get("/:roomId", async (req, res) => { |
| 42 | +router.get("/:roomId", authenticateHttp, async (req, res) => { |
40 | 43 | try { |
41 | 44 | const { roomId } = req.params; |
| 45 | + const userId = req.userId; // Set by authenticateHttp middleware |
| 46 | + |
42 | 47 | const room = await roomController.get(roomId); |
43 | | - if (room) { |
44 | | - const documentContent = await roomController.getDocumentContent(roomId); |
45 | | - res.json({ |
46 | | - success: true, |
47 | | - room: room, |
48 | | - document: { |
49 | | - content: documentContent, |
50 | | - }, |
51 | | - }); |
52 | | - } else { |
53 | | - res.status(404).json({ |
| 48 | + |
| 49 | + // Check if room exists |
| 50 | + if (!room) { |
| 51 | + return res.status(404).json({ |
54 | 52 | success: false, |
55 | 53 | error: "Room not found", |
56 | 54 | }); |
57 | 55 | } |
58 | | - } catch (error) { |
59 | | - console.error("Failed to get room:", error); |
60 | | - res.status(500).json({ |
61 | | - success: false, |
62 | | - error: error.message, |
63 | | - }); |
64 | | - } |
65 | | -}); |
66 | 56 |
|
67 | | -/** |
68 | | - * PATCH /api/v1/rooms/:roomId/close |
69 | | - * Close (stop) a room for collaboration |
70 | | - */ |
71 | | -router.patch("/:roomId/close", async (req, res) => { |
72 | | - try { |
73 | | - const { roomId } = req.params; |
74 | | - const room = await roomController.get(roomId); |
75 | | - if (!room) { |
| 57 | + // Check if user is authorized (part of the room) |
| 58 | + if (!room.userIds.includes(userId)) { |
| 59 | + // Return 404 to maintain privacy (don't reveal room exists) |
76 | 60 | return res.status(404).json({ |
77 | 61 | success: false, |
78 | 62 | error: "Room not found", |
79 | 63 | }); |
80 | | - } else if (!room.isActive) { |
81 | | - return res.status(400).json({ |
82 | | - success: false, |
83 | | - error: "Room is already closed", |
84 | | - }); |
85 | 64 | } |
86 | | - await roomController.closeRoom(roomId); |
| 65 | + |
| 66 | + const documentContent = await roomController.getDocumentContent(roomId); |
87 | 67 | res.json({ |
88 | 68 | success: true, |
89 | | - message: "Room closed successfully. All clients have been notified.", |
| 69 | + room: room, |
| 70 | + document: { |
| 71 | + content: documentContent, |
| 72 | + }, |
90 | 73 | }); |
91 | 74 | } catch (error) { |
92 | | - console.error("Failed to close room:", error); |
| 75 | + console.error("Failed to get room:", error); |
93 | 76 | res.status(500).json({ |
94 | 77 | success: false, |
95 | 78 | error: error.message, |
96 | 79 | }); |
97 | 80 | } |
98 | 81 | }); |
99 | 82 |
|
| 83 | +/** |
| 84 | + * PATCH /api/v1/rooms/:roomId/close |
| 85 | + * Close (stop) a room for collaboration |
| 86 | + * Requires authentication and authorization (user must be in room) |
| 87 | + * @deprecated This route is disabled since room closing is automated by collaboration service |
| 88 | + * when no users are left in the room for a certain period |
| 89 | + */ |
| 90 | +// router.patch("/:roomId/close", authenticateHttp, async (req, res) => { |
| 91 | +// try { |
| 92 | +// const { roomId } = req.params; |
| 93 | +// const userId = req.userId; // Set by authenticateHttp middleware |
| 94 | + |
| 95 | +// const room = await roomController.get(roomId); |
| 96 | + |
| 97 | +// // Check if room exists |
| 98 | +// if (!room) { |
| 99 | +// return res.status(404).json({ |
| 100 | +// success: false, |
| 101 | +// error: "Room not found", |
| 102 | +// }); |
| 103 | +// } |
| 104 | + |
| 105 | +// // Check if user is authorized (part of the room) |
| 106 | +// if (!room.userIds.includes(userId)) { |
| 107 | +// return res.status(404).json({ |
| 108 | +// success: false, |
| 109 | +// error: "Room not found", |
| 110 | +// }); |
| 111 | +// } |
| 112 | + |
| 113 | +// if (!room.isActive) { |
| 114 | +// return res.status(400).json({ |
| 115 | +// success: false, |
| 116 | +// error: "Room is already closed", |
| 117 | +// }); |
| 118 | +// } |
| 119 | + |
| 120 | +// await roomController.closeRoom(roomId); |
| 121 | +// res.json({ |
| 122 | +// success: true, |
| 123 | +// message: "Room closed successfully. All clients have been notified.", |
| 124 | +// }); |
| 125 | +// } catch (error) { |
| 126 | +// console.error("Failed to close room:", error); |
| 127 | +// res.status(500).json({ |
| 128 | +// success: false, |
| 129 | +// error: error.message, |
| 130 | +// }); |
| 131 | +// } |
| 132 | +// }); |
| 133 | + |
100 | 134 | /** |
101 | 135 | * PATCH /api/v1/rooms/:roomId/language |
102 | 136 | * Set programming language for a room |
| 137 | + * Requires authentication and authorization (user must be in room) |
103 | 138 | */ |
104 | | -router.patch("/:roomId/language", async (req, res) => { |
| 139 | +router.patch("/:roomId/language", authenticateHttp, async (req, res) => { |
105 | 140 | try { |
106 | 141 | const { roomId } = req.params; |
107 | 142 | const { language } = req.body; |
| 143 | + const userId = req.userId; // Set by authenticateHttp middleware |
| 144 | + |
108 | 145 | if (!language) { |
109 | 146 | return res.status(400).json({ |
110 | 147 | success: false, |
111 | 148 | error: "Programming language is required", |
112 | 149 | }); |
113 | 150 | } |
| 151 | + |
114 | 152 | const room = await roomController.get(roomId); |
| 153 | + |
| 154 | + // Check if room exists |
115 | 155 | if (!room) { |
116 | 156 | return res.status(404).json({ |
117 | 157 | success: false, |
118 | 158 | error: "Room not found", |
119 | 159 | }); |
120 | | - } else if (!room.isActive) { |
| 160 | + } |
| 161 | + |
| 162 | + // Check if user is authorized (part of the room) |
| 163 | + if (!room.userIds.includes(userId)) { |
| 164 | + return res.status(404).json({ |
| 165 | + success: false, |
| 166 | + error: "Room not found", |
| 167 | + }); |
| 168 | + } |
| 169 | + |
| 170 | + if (!room.isActive) { |
121 | 171 | return res.status(400).json({ |
122 | 172 | success: false, |
123 | 173 | error: "Cannot set language for a closed room", |
124 | 174 | }); |
125 | 175 | } |
| 176 | + |
126 | 177 | await roomController.setProgrammingLanguage(roomId, language); |
127 | 178 | res.json({ |
128 | 179 | success: true, |
|
0 commit comments