Skip to content

Commit 359de28

Browse files
committed
refactor: split room routes file
Signed-off-by: Bob Du <[email protected]>
1 parent e9f4aad commit 359de28

File tree

5 files changed

+196
-185
lines changed

5 files changed

+196
-185
lines changed

service/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
"file-type": "^19.0.0",
3737
"https-proxy-agent": "^7.0.6",
3838
"jsonwebtoken": "^9.0.0",
39-
"mongodb": "^6.6.2",
40-
"multer": "1.4.5-lts.1",
39+
"mongodb": "^6.16.0",
40+
"multer": "^2.0.0",
4141
"node-fetch": "^3.3.0",
4242
"nodemailer": "^6.9.13",
4343
"openai": "^4.96.0",

service/pnpm-lock.yaml

Lines changed: 16 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service/src/index.ts

Lines changed: 4 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,17 @@ import { clearApiKeyCache, clearConfigCache, getApiKeys, getCacheApiKeys, getCac
1212
import type { AnnounceConfig, AuditConfig, Config, GiftCard, KeyConfig, MailConfig, SiteConfig, UserConfig, UserInfo } from './storage/model'
1313
import { AdvancedConfig, Status, UserRole } from './storage/model'
1414
import {
15-
createChatRoom,
1615
createUser,
17-
deleteChatRoom,
1816
disableUser2FA,
19-
existsChatRoom,
2017
getAmtByCardNo,
21-
getChatRooms,
22-
getChatRoomsCount,
2318
getUser,
2419
getUserById,
2520
getUserStatisticsByDay,
2621
getUsers,
27-
renameChatRoom,
2822
updateApiKeyStatus,
2923
updateConfig,
3024
updateGiftCard,
3125
updateGiftCards,
32-
updateRoomChatModel,
33-
updateRoomPrompt,
34-
updateRoomUsingContext,
3526
updateUser,
3627
updateUser2FA,
3728
updateUserAdvancedConfig,
@@ -52,6 +43,7 @@ import { isAdmin, rootAuth } from './middleware/rootAuth'
5243

5344
import { router as chatRouter } from './routes/chat'
5445
import { router as promptRouter } from './routes/prompt'
46+
import { router as roomRouter } from './routes/room'
5547
import { router as uploadRouter } from './routes/upload'
5648

5749
dotenv.config()
@@ -79,163 +71,6 @@ app.all('/', (_, res, next) => {
7971
next()
8072
})
8173

82-
router.get('/chatrooms', auth, async (req, res) => {
83-
try {
84-
const userId = req.headers.userId as string
85-
const rooms = await getChatRooms(userId)
86-
const result = []
87-
rooms.forEach((r) => {
88-
result.push({
89-
uuid: r.roomId,
90-
title: r.title,
91-
isEdit: false,
92-
prompt: r.prompt,
93-
usingContext: r.usingContext === undefined ? true : r.usingContext,
94-
chatModel: r.chatModel,
95-
})
96-
})
97-
res.send({ status: 'Success', message: null, data: result })
98-
}
99-
catch (error) {
100-
console.error(error)
101-
res.send({ status: 'Fail', message: 'Load error', data: [] })
102-
}
103-
})
104-
105-
function formatTimestamp(timestamp: number) {
106-
const date = new Date(timestamp)
107-
const year = date.getFullYear()
108-
const month = String(date.getMonth() + 1).padStart(2, '0')
109-
const day = String(date.getDate()).padStart(2, '0')
110-
const hours = String(date.getHours()).padStart(2, '0')
111-
const minutes = String(date.getMinutes()).padStart(2, '0')
112-
const seconds = String(date.getSeconds()).padStart(2, '0')
113-
114-
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
115-
}
116-
117-
router.get('/chatrooms-count', auth, async (req, res) => {
118-
try {
119-
const userId = req.query.userId as string
120-
const page = +req.query.page
121-
const size = +req.query.size
122-
const rooms = await getChatRoomsCount(userId, page, size)
123-
const result = []
124-
rooms.data.forEach((r) => {
125-
result.push({
126-
uuid: r.roomId,
127-
title: r.title,
128-
userId: r.userId,
129-
name: r.username,
130-
lastTime: formatTimestamp(r.dateTime),
131-
chatCount: r.chatCount,
132-
})
133-
})
134-
res.send({ status: 'Success', message: null, data: { data: result, total: rooms.total } })
135-
}
136-
catch (error) {
137-
console.error(error)
138-
res.send({ status: 'Fail', message: 'Load error', data: [] })
139-
}
140-
})
141-
142-
router.post('/room-create', auth, async (req, res) => {
143-
try {
144-
const userId = req.headers.userId as string
145-
const { title, roomId, chatModel } = req.body as { title: string; roomId: number; chatModel: string }
146-
const room = await createChatRoom(userId, title, roomId, chatModel)
147-
res.send({ status: 'Success', message: null, data: room })
148-
}
149-
catch (error) {
150-
console.error(error)
151-
res.send({ status: 'Fail', message: 'Create error', data: null })
152-
}
153-
})
154-
155-
router.post('/room-rename', auth, async (req, res) => {
156-
try {
157-
const userId = req.headers.userId as string
158-
const { title, roomId } = req.body as { title: string; roomId: number }
159-
const success = await renameChatRoom(userId, title, roomId)
160-
if (success)
161-
res.send({ status: 'Success', message: null, data: null })
162-
else
163-
res.send({ status: 'Fail', message: 'Saved Failed', data: null })
164-
}
165-
catch (error) {
166-
console.error(error)
167-
res.send({ status: 'Fail', message: 'Rename error', data: null })
168-
}
169-
})
170-
171-
router.post('/room-prompt', auth, async (req, res) => {
172-
try {
173-
const userId = req.headers.userId as string
174-
const { prompt, roomId } = req.body as { prompt: string; roomId: number }
175-
const success = await updateRoomPrompt(userId, roomId, prompt)
176-
if (success)
177-
res.send({ status: 'Success', message: 'Saved successfully', data: null })
178-
else
179-
res.send({ status: 'Fail', message: 'Saved Failed', data: null })
180-
}
181-
catch (error) {
182-
console.error(error)
183-
res.send({ status: 'Fail', message: 'Rename error', data: null })
184-
}
185-
})
186-
187-
router.post('/room-chatmodel', auth, async (req, res) => {
188-
try {
189-
const userId = req.headers.userId as string
190-
const { chatModel, roomId } = req.body as { chatModel: string; roomId: number }
191-
const success = await updateRoomChatModel(userId, roomId, chatModel)
192-
if (success)
193-
res.send({ status: 'Success', message: 'Saved successfully', data: null })
194-
else
195-
res.send({ status: 'Fail', message: 'Saved Failed', data: null })
196-
}
197-
catch (error) {
198-
console.error(error)
199-
res.send({ status: 'Fail', message: 'Rename error', data: null })
200-
}
201-
})
202-
203-
router.post('/room-context', auth, async (req, res) => {
204-
try {
205-
const userId = req.headers.userId as string
206-
const { using, roomId } = req.body as { using: boolean; roomId: number }
207-
const success = await updateRoomUsingContext(userId, roomId, using)
208-
if (success)
209-
res.send({ status: 'Success', message: 'Saved successfully', data: null })
210-
else
211-
res.send({ status: 'Fail', message: 'Saved Failed', data: null })
212-
}
213-
catch (error) {
214-
console.error(error)
215-
res.send({ status: 'Fail', message: 'Rename error', data: null })
216-
}
217-
})
218-
219-
router.post('/room-delete', auth, async (req, res) => {
220-
try {
221-
const userId = req.headers.userId as string
222-
const { roomId } = req.body as { roomId: number }
223-
if (!roomId || !await existsChatRoom(userId, roomId)) {
224-
res.send({ status: 'Fail', message: 'Unknown room', data: null })
225-
return
226-
}
227-
const success = await deleteChatRoom(userId, roomId)
228-
if (success)
229-
res.send({ status: 'Success', message: null, data: null })
230-
else
231-
res.send({ status: 'Fail', message: 'Saved Failed', data: null })
232-
}
233-
catch (error) {
234-
console.error(error)
235-
res.send({ status: 'Fail', message: 'Delete error', data: null })
236-
}
237-
})
238-
23974
router.post('/user-register', authLimiter, async (req, res) => {
24075
try {
24176
const { username, password } = req.body as { username: string; password: string }
@@ -1053,6 +888,9 @@ app.use('/api', chatRouter)
1053888
app.use('', promptRouter)
1054889
app.use('/api', promptRouter)
1055890

891+
app.use('', roomRouter)
892+
app.use('/api', roomRouter)
893+
1056894
app.use('', uploadRouter)
1057895
app.use('/api', uploadRouter)
1058896

0 commit comments

Comments
 (0)