Skip to content

refactor: split room routes file #608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"file-type": "^19.0.0",
"https-proxy-agent": "^7.0.6",
"jsonwebtoken": "^9.0.0",
"mongodb": "^6.6.2",
"multer": "1.4.5-lts.1",
"mongodb": "^6.16.0",
"multer": "^2.0.0",
"node-fetch": "^3.3.0",
"nodemailer": "^6.9.13",
"openai": "^4.96.0",
Expand Down
33 changes: 16 additions & 17 deletions service/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

170 changes: 4 additions & 166 deletions service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,17 @@ import { clearApiKeyCache, clearConfigCache, getApiKeys, getCacheApiKeys, getCac
import type { AnnounceConfig, AuditConfig, Config, GiftCard, KeyConfig, MailConfig, SiteConfig, UserConfig, UserInfo } from './storage/model'
import { AdvancedConfig, Status, UserRole } from './storage/model'
import {
createChatRoom,
createUser,
deleteChatRoom,
disableUser2FA,
existsChatRoom,
getAmtByCardNo,
getChatRooms,
getChatRoomsCount,
getUser,
getUserById,
getUserStatisticsByDay,
getUsers,
renameChatRoom,
updateApiKeyStatus,
updateConfig,
updateGiftCard,
updateGiftCards,
updateRoomChatModel,
updateRoomPrompt,
updateRoomUsingContext,
updateUser,
updateUser2FA,
updateUserAdvancedConfig,
Expand All @@ -52,6 +43,7 @@ import { isAdmin, rootAuth } from './middleware/rootAuth'

import { router as chatRouter } from './routes/chat'
import { router as promptRouter } from './routes/prompt'
import { router as roomRouter } from './routes/room'
import { router as uploadRouter } from './routes/upload'

dotenv.config()
Expand Down Expand Up @@ -79,163 +71,6 @@ app.all('/', (_, res, next) => {
next()
})

router.get('/chatrooms', auth, async (req, res) => {
try {
const userId = req.headers.userId as string
const rooms = await getChatRooms(userId)
const result = []
rooms.forEach((r) => {
result.push({
uuid: r.roomId,
title: r.title,
isEdit: false,
prompt: r.prompt,
usingContext: r.usingContext === undefined ? true : r.usingContext,
chatModel: r.chatModel,
})
})
res.send({ status: 'Success', message: null, data: result })
}
catch (error) {
console.error(error)
res.send({ status: 'Fail', message: 'Load error', data: [] })
}
})

function formatTimestamp(timestamp: number) {
const date = new Date(timestamp)
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
const hours = String(date.getHours()).padStart(2, '0')
const minutes = String(date.getMinutes()).padStart(2, '0')
const seconds = String(date.getSeconds()).padStart(2, '0')

return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

router.get('/chatrooms-count', auth, async (req, res) => {
try {
const userId = req.query.userId as string
const page = +req.query.page
const size = +req.query.size
const rooms = await getChatRoomsCount(userId, page, size)
const result = []
rooms.data.forEach((r) => {
result.push({
uuid: r.roomId,
title: r.title,
userId: r.userId,
name: r.username,
lastTime: formatTimestamp(r.dateTime),
chatCount: r.chatCount,
})
})
res.send({ status: 'Success', message: null, data: { data: result, total: rooms.total } })
}
catch (error) {
console.error(error)
res.send({ status: 'Fail', message: 'Load error', data: [] })
}
})

router.post('/room-create', auth, async (req, res) => {
try {
const userId = req.headers.userId as string
const { title, roomId, chatModel } = req.body as { title: string; roomId: number; chatModel: string }
const room = await createChatRoom(userId, title, roomId, chatModel)
res.send({ status: 'Success', message: null, data: room })
}
catch (error) {
console.error(error)
res.send({ status: 'Fail', message: 'Create error', data: null })
}
})

router.post('/room-rename', auth, async (req, res) => {
try {
const userId = req.headers.userId as string
const { title, roomId } = req.body as { title: string; roomId: number }
const success = await renameChatRoom(userId, title, roomId)
if (success)
res.send({ status: 'Success', message: null, data: null })
else
res.send({ status: 'Fail', message: 'Saved Failed', data: null })
}
catch (error) {
console.error(error)
res.send({ status: 'Fail', message: 'Rename error', data: null })
}
})

router.post('/room-prompt', auth, async (req, res) => {
try {
const userId = req.headers.userId as string
const { prompt, roomId } = req.body as { prompt: string; roomId: number }
const success = await updateRoomPrompt(userId, roomId, prompt)
if (success)
res.send({ status: 'Success', message: 'Saved successfully', data: null })
else
res.send({ status: 'Fail', message: 'Saved Failed', data: null })
}
catch (error) {
console.error(error)
res.send({ status: 'Fail', message: 'Rename error', data: null })
}
})

router.post('/room-chatmodel', auth, async (req, res) => {
try {
const userId = req.headers.userId as string
const { chatModel, roomId } = req.body as { chatModel: string; roomId: number }
const success = await updateRoomChatModel(userId, roomId, chatModel)
if (success)
res.send({ status: 'Success', message: 'Saved successfully', data: null })
else
res.send({ status: 'Fail', message: 'Saved Failed', data: null })
}
catch (error) {
console.error(error)
res.send({ status: 'Fail', message: 'Rename error', data: null })
}
})

router.post('/room-context', auth, async (req, res) => {
try {
const userId = req.headers.userId as string
const { using, roomId } = req.body as { using: boolean; roomId: number }
const success = await updateRoomUsingContext(userId, roomId, using)
if (success)
res.send({ status: 'Success', message: 'Saved successfully', data: null })
else
res.send({ status: 'Fail', message: 'Saved Failed', data: null })
}
catch (error) {
console.error(error)
res.send({ status: 'Fail', message: 'Rename error', data: null })
}
})

router.post('/room-delete', auth, async (req, res) => {
try {
const userId = req.headers.userId as string
const { roomId } = req.body as { roomId: number }
if (!roomId || !await existsChatRoom(userId, roomId)) {
res.send({ status: 'Fail', message: 'Unknown room', data: null })
return
}
const success = await deleteChatRoom(userId, roomId)
if (success)
res.send({ status: 'Success', message: null, data: null })
else
res.send({ status: 'Fail', message: 'Saved Failed', data: null })
}
catch (error) {
console.error(error)
res.send({ status: 'Fail', message: 'Delete error', data: null })
}
})

router.post('/user-register', authLimiter, async (req, res) => {
try {
const { username, password } = req.body as { username: string; password: string }
Expand Down Expand Up @@ -1053,6 +888,9 @@ app.use('/api', chatRouter)
app.use('', promptRouter)
app.use('/api', promptRouter)

app.use('', roomRouter)
app.use('/api', roomRouter)

app.use('', uploadRouter)
app.use('/api', uploadRouter)

Expand Down
Loading