@@ -5,10 +5,10 @@ import { ObjectId } from 'mongodb'
55import type { RequestProps } from './types'
66import type { ChatContext , ChatMessage } from './chatgpt'
77import { chatConfig , chatReplyProcess , containsSensitiveWords , getRandomApiKey , initAuditService } from './chatgpt'
8- import { auth } from './middleware/auth'
9- import { clearConfigCache , getApiKeys , getCacheConfig , getOriginConfig } from './storage/config'
8+ import { auth , getUserId } from './middleware/auth'
9+ import { clearApiKeyCache , clearConfigCache , getApiKeys , getCacheApiKeys , getCacheConfig , getOriginConfig } from './storage/config'
1010import type { AuditConfig , CHATMODEL , ChatInfo , ChatOptions , Config , KeyConfig , MailConfig , SiteConfig , UsageResponse , UserInfo } from './storage/model'
11- import { Status , UserRole } from './storage/model'
11+ import { Status , UserRole , chatModelOptions } from './storage/model'
1212import {
1313 clearChat ,
1414 createChatRoom ,
@@ -36,12 +36,13 @@ import {
3636 updateUserChatModel ,
3737 updateUserInfo ,
3838 updateUserPassword ,
39+ updateUserRole ,
3940 updateUserStatus ,
4041 upsertKey ,
4142 verifyUser ,
4243} from './storage/mongo'
4344import { limiter } from './middleware/limiter'
44- import { isEmail , isNotEmptyString } from './utils/is'
45+ import { hasAnyRole , isEmail , isNotEmptyString } from './utils/is'
4546import { sendNoticeMail , sendResetPasswordMail , sendTestMail , sendVerifyMail , sendVerifyMailAdmin } from './utils/mail'
4647import { checkUserResetPassword , checkUserVerify , checkUserVerifyAdmin , getUserResetPasswordUrl , getUserVerifyUrl , getUserVerifyUrlAdmin , md5 } from './utils/security'
4748import { rootAuth } from './middleware/rootAuth'
@@ -558,8 +559,51 @@ router.post('/session', async (req, res) => {
558559 const allowRegister = ( await getCacheConfig ( ) ) . siteConfig . registerEnabled
559560 if ( config . apiModel !== 'ChatGPTAPI' && config . apiModel !== 'ChatGPTUnofficialProxyAPI' )
560561 config . apiModel = 'ChatGPTAPI'
562+ const userId = await getUserId ( req )
563+ const chatModels : {
564+ label
565+ key : string
566+ value : string
567+ } [ ] = [ ]
568+ if ( userId != null ) {
569+ const user = await getUserById ( userId )
570+ const keys = ( await getCacheApiKeys ( ) ) . filter ( d => hasAnyRole ( d . userRoles , user . roles ) )
571+
572+ const count : { key : string ; count : number } [ ] = [ ]
573+ chatModelOptions . forEach ( ( chatModel ) => {
574+ keys . forEach ( ( key ) => {
575+ if ( key . chatModels . includes ( chatModel . value ) ) {
576+ if ( count . filter ( d => d . key === chatModel . value ) . length <= 0 ) {
577+ count . push ( { key : chatModel . value , count : 1 } )
578+ }
579+ else {
580+ const thisCount = count . filter ( d => d . key === chatModel . value ) [ 0 ]
581+ thisCount . count ++
582+ }
583+ }
584+ } )
585+ } )
586+ count . forEach ( ( c ) => {
587+ const thisChatModel = chatModelOptions . filter ( d => d . value === c . key ) [ 0 ]
588+ chatModels . push ( {
589+ label : `${ thisChatModel . label } (${ c . count } )` ,
590+ key : c . key ,
591+ value : c . key ,
592+ } )
593+ } )
594+ }
561595
562- res . send ( { status : 'Success' , message : '' , data : { auth : hasAuth , allowRegister, model : config . apiModel , title : config . siteConfig . siteTitle } } )
596+ res . send ( {
597+ status : 'Success' ,
598+ message : '' ,
599+ data : {
600+ auth : hasAuth ,
601+ allowRegister,
602+ model : config . apiModel ,
603+ title : config . siteConfig . siteTitle ,
604+ chatModels,
605+ } ,
606+ } )
563607 }
564608 catch ( error ) {
565609 res . send ( { status : 'Fail' , message : error . message , data : null } )
@@ -693,6 +737,17 @@ router.post('/user-status', rootAuth, async (req, res) => {
693737 }
694738} )
695739
740+ router . post ( '/user-role' , rootAuth , async ( req , res ) => {
741+ try {
742+ const { userId, roles } = req . body as { userId : string ; roles : UserRole [ ] }
743+ await updateUserRole ( userId , roles )
744+ res . send ( { status : 'Success' , message : '更新成功 | Update successfully' } )
745+ }
746+ catch ( error ) {
747+ res . send ( { status : 'Fail' , message : error . message , data : null } )
748+ }
749+ } )
750+
696751router . post ( '/verify' , async ( req , res ) => {
697752 try {
698753 const { token } = req . body as { token : string }
@@ -873,6 +928,7 @@ router.post('/setting-key-upsert', rootAuth, async (req, res) => {
873928 if ( keyConfig . _id !== undefined )
874929 keyConfig . _id = new ObjectId ( keyConfig . _id )
875930 await upsertKey ( keyConfig )
931+ clearApiKeyCache ( )
876932 res . send ( { status : 'Success' , message : '成功 | Successfully' } )
877933 }
878934 catch ( error ) {
0 commit comments