Skip to content
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
3 changes: 3 additions & 0 deletions service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ router.post('/session', async (req, res) => {
chatModels,
allChatModels: chatModelOptions,
showWatermark: config.siteConfig?.showWatermark,
adminViewChatHistoryEnabled: process.env.ADMIN_VIEW_CHAT_HISTORY_ENABLED === 'true',
},
})
return
Expand Down Expand Up @@ -244,6 +245,7 @@ router.post('/session', async (req, res) => {
allChatModels: chatModelOptions,
usageCountLimit: config.siteConfig?.usageCountLimit,
showWatermark: config.siteConfig?.showWatermark,
adminViewChatHistoryEnabled: process.env.ADMIN_VIEW_CHAT_HISTORY_ENABLED === 'true',
userInfo,
},
})
Expand All @@ -261,6 +263,7 @@ router.post('/session', async (req, res) => {
chatModels: chatModelOptions,
allChatModels: chatModelOptions,
showWatermark: config.siteConfig?.showWatermark,
adminViewChatHistoryEnabled: process.env.ADMIN_VIEW_CHAT_HISTORY_ENABLED === 'true',
userInfo,
},
})
Expand Down
5 changes: 4 additions & 1 deletion service/src/routes/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ResponseChunk } from '../chatgpt/types'
import type { ChatInfo, ChatOptions, UsageResponse, UserInfo } from '../storage/model'
import type { RequestProps } from '../types'
import * as console from 'node:console'
import * as process from 'node:process'
import Router from 'express'
import { ObjectId } from 'mongodb'
import { abortChatProcess, chatReplyProcess, containsSensitiveWords } from '../chatgpt'
Expand Down Expand Up @@ -38,12 +39,14 @@ router.get('/chat-history', auth, async (req, res) => {
return
}

// When 'all' parameter is not empty, it means requesting to view all users' chat history
// This requires: 1) user must be an admin, 2) ADMIN_VIEW_CHAT_HISTORY_ENABLED environment variable must be set to 'true'
if (all !== null && all !== 'undefined' && all !== undefined && all.trim().length !== 0) {
const config = await getCacheConfig()
if (config.siteConfig.loginEnabled) {
try {
const user = await getUserById(userId)
if (user == null || user.status !== Status.Normal || !user.roles.includes(UserRole.Admin)) {
if (user == null || user.status !== Status.Normal || !user.roles.includes(UserRole.Admin) || process.env.ADMIN_VIEW_CHAT_HISTORY_ENABLED !== 'true') {
res.send({ status: 'Fail', message: '无权限 | No permission.', data: null })
return
}
Expand Down
68 changes: 39 additions & 29 deletions service/src/storage/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,40 @@ export async function getChatRoomsCount(userId: string, page: number, size: numb
{
$lookup: {
from: 'chat',
localField: 'roomId',
foreignField: 'roomId',
as: 'chat',
let: { roomId: '$roomId' },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$roomId', '$$roomId'] },
{ $ne: ['$status', Status.InversionDeleted] },
],
},
},
},
{
$sort: { dateTime: -1 },
},
{
$group: {
_id: null,
chatCount: { $sum: 1 },
lastChat: { $first: '$$ROOT' },
},
},
],
as: 'chatInfo',
},
},
{
$addFields: {
title: '$chat.prompt',
chatCount: {
$ifNull: [{ $arrayElemAt: ['$chatInfo.chatCount', 0] }, 0],
},
lastChat: {
$arrayElemAt: ['$chatInfo.lastChat', 0],
},
user_ObjectId: {
$toObjectId: '$userId',
},
Expand All @@ -450,6 +476,13 @@ export async function getChatRoomsCount(userId: string, page: number, size: numb
localField: 'user_ObjectId',
foreignField: '_id',
as: 'user',
pipeline: [
{
$project: {
name: 1,
},
},
],
},
},
{
Expand All @@ -458,37 +491,14 @@ export async function getChatRoomsCount(userId: string, page: number, size: numb
preserveNullAndEmptyArrays: false,
},
},
{
$sort: {
'chat.dateTime': -1,
},
},
{
$addFields: {
chatCount: {
$size: '$chat',
},
chat: {
$arrayElemAt: [
{
$slice: [
'$chat',
-1,
],
},
0,
],
},
},
},
{
$project: {
userId: 1,
title: '$chat.prompt',
title: { $ifNull: ['$lastChat.prompt', ''] },
username: '$user.name',
roomId: 1,
chatCount: 1,
dateTime: '$chat.dateTime',
dateTime: { $ifNull: ['$lastChat.dateTime', null] },
},
},
{
Expand Down
9 changes: 8 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,15 @@ export function fetchDeleteChatRoom<T = any>(roomId: number) {
}

export function fetchGetChatHistory<T = any>(roomId: number, lastId?: number, all?: string) {
let url = `/chat-history?roomId=${roomId}`
if (lastId !== undefined && lastId !== null) {
url += `&lastId=${lastId}`
}
if (all !== undefined && all !== null) {
url += `&all=${all}`
}
return get<T>({
url: `/chat-history?roomId=${roomId}&lastId=${lastId}&all=${all}`,
url,
})
}

Expand Down
7 changes: 5 additions & 2 deletions src/components/common/Setting/ChatRecord.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { SvgIcon } from '@/components/common'
import { useBasicLayout } from '@/hooks/useBasicLayout'
import Message from '@/views/chat/components/Message/index.vue'

const { t } = useI18n()

interface HistoryChat {
uuid?: number
model?: string
Expand Down Expand Up @@ -70,13 +72,13 @@ const columns = [{
show.value = true
dataSources.value.length = 0
chatLoading.value = true
fetchGetChatHistory(row.uuid, undefined, 'all').then((res: any) => {
fetchGetChatHistory(row.roomId, undefined, 'all').then((res: any) => {
dataSources.value = res.data as HistoryChat[]
chatLoading.value = false
})
},
},
{ default: () => 'view' },
{ default: () => t('setting.view') },
))
return actions
},
Expand Down Expand Up @@ -144,6 +146,7 @@ onMounted(async () => {
<NSelect
v-model:value="selectUserId"
style="width: 250px"
filterable
:options="userOptions"
@update:value="handleSelectUser"
/>
Expand Down
10 changes: 8 additions & 2 deletions src/components/common/Setting/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { SvgIcon } from '@/components/common'
import ChatRecord from '@/components/common/Setting/ChatRecord.vue'
import { useBasicLayout } from '@/hooks/useBasicLayout'
import { useUserStore } from '@/store'
import { useAuthStore, useUserStore } from '@/store'
import About from './About.vue'
import Advanced from './Advanced.vue'
import Announcement from './Anonuncement.vue'
Expand All @@ -25,6 +25,7 @@ const emit = defineEmits<Emit>()
const { t } = useI18n()

const userStore = useUserStore()
const authStore = useAuthStore()
const { isMobile } = useBasicLayout()

interface Props {
Expand All @@ -37,6 +38,11 @@ interface Emit {

const active = ref('General')

// Check if admin view chat history is enabled
const showChatRecordTab = computed(() => {
return userStore.userInfo.root && authStore.session?.adminViewChatHistoryEnabled === true
})

const show = computed({
get() {
return props.visible
Expand Down Expand Up @@ -99,7 +105,7 @@ const show = computed({
</template>
<About />
</NTabPane>
<NTabPane v-if="userStore.userInfo.root" name="ChatRecord" tab="ChatRecord">
<NTabPane v-if="showChatRecordTab" name="ChatRecord" tab="ChatRecord">
<template #tab>
<SvgIcon class="text-lg" icon="ic:outline-chat" />
<span class="ml-2">{{ t('setting.chatRecord') }}</span>
Expand Down
1 change: 1 addition & 0 deletions src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"statistics": "Statistics",
"config": "Base Config",
"chatRecord": "Chat History",
"view": "View",
"siteConfig": "Site Config",
"mailConfig": "Mail Config",
"auditConfig": "Audit Config",
Expand Down
1 change: 1 addition & 0 deletions src/locales/ko-KR.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"config": "기본 구성",
"statistics": "통계학",
"chatRecord": "채팅 기록",
"view": "보기",
"siteConfig": "사이트 구성",
"mailConfig": "메일 구성",
"auditConfig": "감사 구성",
Expand Down
1 change: 1 addition & 0 deletions src/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"statistics": "统计",
"config": "基本配置",
"chatRecord": "聊天记录",
"view": "查看",
"siteConfig": "网站配置",
"mailConfig": "邮箱配置",
"auditConfig": "敏感词审核",
Expand Down
1 change: 1 addition & 0 deletions src/locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"statistics": "統計",
"config": "基本設定",
"chatRecord": "聊天紀錄",
"view": "查看",
"siteConfig": "网站配置",
"mailConfig": "邮箱配置",
"auditConfig": "敏感词审核",
Expand Down
1 change: 1 addition & 0 deletions src/store/modules/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface SessionResponse {
}[]
usageCountLimit: boolean
showWatermark: boolean
adminViewChatHistoryEnabled?: boolean
userInfo: { name: string, description: string, avatar: string, userId: string, root: boolean, config: UserConfig }
}

Expand Down