Skip to content

Commit ced3648

Browse files
author
Kerwin
committed
fix: 清理聊天记录的时候无效
1 parent de336b5 commit ced3648

File tree

8 files changed

+165
-88
lines changed

8 files changed

+165
-88
lines changed

README.en.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ version: '3'
213213
services:
214214
app:
215215
image: kerwin1202/chatgpt-web # always use latest, pull the tag image again when updating
216+
container_name: chatgptweb
217+
restart: unless-stopped
216218
ports:
217219
- 3002:3002
218220
depends_on:
@@ -264,6 +266,7 @@ services:
264266
database:
265267
image: mongo
266268
container_name: chatgptweb-database
269+
restart: unless-stopped
267270
ports:
268271
- '27017:27017'
269272
expose:

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ services:
220220
app:
221221
image: kerwin1202/chatgpt-web # 总是使用latest,更新时重新pull该tag镜像即可
222222
container_name: chatgptweb
223+
restart: unless-stopped
223224
ports:
224225
- 3002:3002
225226
depends_on:
@@ -273,6 +274,7 @@ services:
273274
database:
274275
image: mongo
275276
container_name: chatgptweb-database
277+
restart: unless-stopped
276278
ports:
277279
- '27017:27017'
278280
expose:

docker-compose/docker-compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ version: '3'
33
services:
44
app:
55
image: chenzhaoyu94/chatgpt-web # 总是使用latest,更新时重新pull该tag镜像即可
6+
container_name: chatgptweb
7+
restart: unless-stopped
68
ports:
79
- 3002:3002
810
depends_on:
@@ -68,6 +70,8 @@ services:
6870

6971
nginx:
7072
image: nginx:alpine
73+
container_name: chatgptweb-database
74+
restart: unless-stopped
7175
ports:
7276
- '80:80'
7377
expose:

service/src/index.ts

Lines changed: 139 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { auth } from './middleware/auth'
88
import { clearConfigCache, getCacheConfig, getOriginConfig } from './storage/config'
99
import type { ChatOptions, Config, MailConfig, SiteConfig, UserInfo } from './storage/model'
1010
import { Status } from './storage/model'
11-
import { clearChat, createChatRoom, createUser, deleteChat, deleteChatRoom, existsChatRoom, getChat, getChatRooms, getChats, getUser, getUserById, insertChat, renameChatRoom, updateChat, updateConfig, updateUserInfo, verifyUser } from './storage/mongo'
11+
import { clearChat, createChatRoom, createUser, deleteAllChatRooms, deleteChat, deleteChatRoom, existsChatRoom, getChat, getChatRooms, getChats, getUser, getUserById, insertChat, renameChatRoom, updateChat, updateConfig, updateUserInfo, verifyUser } from './storage/mongo'
1212
import { limiter } from './middleware/limiter'
1313
import { isNotEmptyString } from './utils/is'
1414
import { sendTestMail, sendVerifyMail } from './utils/mail'
@@ -29,111 +29,165 @@ app.all('*', (_, res, next) => {
2929
})
3030

3131
router.get('/chatrooms', auth, async (req, res) => {
32-
const userId = req.headers.userId
33-
const rooms = await getChatRooms(userId)
34-
const result = []
35-
rooms.forEach((r) => {
36-
result.push({
37-
uuid: r.roomId,
38-
title: r.title,
39-
isEdit: false,
32+
try {
33+
const userId = new ObjectId(req.headers.userId as string)
34+
const rooms = await getChatRooms(userId)
35+
const result = []
36+
rooms.forEach((r) => {
37+
result.push({
38+
uuid: r.roomId,
39+
title: r.title,
40+
isEdit: false,
41+
})
4042
})
41-
})
42-
res.send({ status: 'Success', message: null, data: result })
43+
res.send({ status: 'Success', message: null, data: result })
44+
}
45+
catch (error) {
46+
console.error(error)
47+
res.send({ status: 'Fail', message: 'Load error', data: [] })
48+
}
4349
})
4450

4551
router.post('/room-create', auth, async (req, res) => {
46-
const userId = req.headers.userId
47-
const { title, roomId } = req.body as { title: string; roomId: number }
48-
const room = await createChatRoom(userId, title, roomId)
49-
res.send({ status: 'Success', message: null, data: room })
52+
try {
53+
const userId = new ObjectId(req.headers.userId as string)
54+
const { title, roomId } = req.body as { title: string; roomId: number }
55+
const room = await createChatRoom(userId, title, roomId)
56+
res.send({ status: 'Success', message: null, data: room })
57+
}
58+
catch (error) {
59+
console.error(error)
60+
res.send({ status: 'Fail', message: 'Create error', data: null })
61+
}
5062
})
5163

5264
router.post('/room-rename', auth, async (req, res) => {
53-
const userId = req.headers.userId
54-
const { title, roomId } = req.body as { title: string; roomId: number }
55-
const room = await renameChatRoom(userId, title, roomId)
56-
res.send({ status: 'Success', message: null, data: room })
65+
try {
66+
const userId = new ObjectId(req.headers.userId as string)
67+
const { title, roomId } = req.body as { title: string; roomId: number }
68+
const room = await renameChatRoom(userId, title, roomId)
69+
res.send({ status: 'Success', message: null, data: room })
70+
}
71+
catch (error) {
72+
console.error(error)
73+
res.send({ status: 'Fail', message: 'Rename error', data: null })
74+
}
5775
})
5876

5977
router.post('/room-delete', auth, async (req, res) => {
60-
const userId = req.headers.userId
61-
const { roomId } = req.body as { roomId: number }
62-
if (!roomId || !await existsChatRoom(userId, roomId)) {
63-
res.send({ status: 'Fail', message: 'Unknow room', data: null })
64-
return
65-
}
66-
await deleteChatRoom(userId, roomId)
67-
res.send({ status: 'Success', message: null })
78+
try {
79+
const userId = new ObjectId(req.headers.userId as string)
80+
const { roomId } = req.body as { roomId: number }
81+
if (!roomId || !await existsChatRoom(userId, roomId)) {
82+
res.send({ status: 'Fail', message: 'Unknow room', data: null })
83+
return
84+
}
85+
await deleteChatRoom(userId, roomId)
86+
res.send({ status: 'Success', message: null })
87+
}
88+
catch (error) {
89+
console.error(error)
90+
res.send({ status: 'Fail', message: 'Delete error', data: null })
91+
}
6892
})
6993

7094
router.get('/chat-hisroty', auth, async (req, res) => {
71-
const userId = req.headers.userId
72-
const roomId = +req.query.roomid
73-
const lastTime = req.query.lasttime
74-
if (!roomId || !await existsChatRoom(userId, roomId)) {
75-
res.send({ status: 'Success', message: null, data: [] })
76-
// res.send({ status: 'Fail', message: 'Unknow room', data: null })
77-
return
78-
}
79-
const chats = await getChats(roomId, !lastTime ? null : parseInt(lastTime))
80-
81-
const result = []
82-
chats.forEach((c) => {
83-
if (c.status !== Status.InversionDeleted) {
84-
result.push({
85-
dateTime: new Date(c.dateTime).toLocaleString(),
86-
text: c.prompt,
87-
inversion: true,
88-
error: false,
89-
conversationOptions: null,
90-
requestOptions: {
91-
prompt: c.prompt,
92-
options: null,
93-
},
94-
})
95-
}
96-
if (c.status !== Status.ResponseDeleted) {
97-
result.push({
98-
dateTime: new Date(c.dateTime).toLocaleString(),
99-
text: c.response,
100-
inversion: false,
101-
error: false,
102-
loading: false,
103-
conversationOptions: {
104-
parentMessageId: c.options.messageId,
105-
},
106-
requestOptions: {
107-
prompt: c.prompt,
108-
parentMessageId: c.options.parentMessageId,
109-
},
110-
})
95+
try {
96+
const userId = new ObjectId(req.headers.userId as string)
97+
const roomId = +req.query.roomid
98+
const lastTime = req.query.lasttime as string
99+
if (!roomId || !await existsChatRoom(userId, roomId)) {
100+
res.send({ status: 'Success', message: null, data: [] })
101+
// res.send({ status: 'Fail', message: 'Unknow room', data: null })
102+
return
111103
}
112-
})
104+
const chats = await getChats(roomId, !lastTime ? null : parseInt(lastTime))
105+
106+
const result = []
107+
chats.forEach((c) => {
108+
if (c.status !== Status.InversionDeleted) {
109+
result.push({
110+
dateTime: new Date(c.dateTime).toLocaleString(),
111+
text: c.prompt,
112+
inversion: true,
113+
error: false,
114+
conversationOptions: null,
115+
requestOptions: {
116+
prompt: c.prompt,
117+
options: null,
118+
},
119+
})
120+
}
121+
if (c.status !== Status.ResponseDeleted) {
122+
result.push({
123+
dateTime: new Date(c.dateTime).toLocaleString(),
124+
text: c.response,
125+
inversion: false,
126+
error: false,
127+
loading: false,
128+
conversationOptions: {
129+
parentMessageId: c.options.messageId,
130+
},
131+
requestOptions: {
132+
prompt: c.prompt,
133+
parentMessageId: c.options.parentMessageId,
134+
},
135+
})
136+
}
137+
})
113138

114-
res.send({ status: 'Success', message: null, data: result })
139+
res.send({ status: 'Success', message: null, data: result })
140+
}
141+
catch (error) {
142+
console.error(error)
143+
res.send({ status: 'Fail', message: 'Load error', data: null })
144+
}
115145
})
116146

117147
router.post('/chat-delete', auth, async (req, res) => {
118-
const userId = req.headers.userId
119-
const { roomId, uuid, inversion } = req.body as { roomId: number; uuid: number; inversion: boolean }
120-
if (!roomId || !await existsChatRoom(userId, roomId)) {
121-
res.send({ status: 'Fail', message: 'Unknow room', data: null })
122-
return
123-
}
124-
await deleteChat(roomId, uuid, inversion)
125-
res.send({ status: 'Success', message: null, data: null })
148+
try {
149+
const userId = new ObjectId(req.headers.userId as string)
150+
const { roomId, uuid, inversion } = req.body as { roomId: number; uuid: number; inversion: boolean }
151+
if (!roomId || !await existsChatRoom(userId, roomId)) {
152+
res.send({ status: 'Fail', message: 'Unknow room', data: null })
153+
return
154+
}
155+
await deleteChat(roomId, uuid, inversion)
156+
res.send({ status: 'Success', message: null, data: null })
157+
}
158+
catch (error) {
159+
console.error(error)
160+
res.send({ status: 'Fail', message: 'Delete error', data: null })
161+
}
162+
})
163+
164+
router.post('/chat-clear-all', auth, async (req, res) => {
165+
try {
166+
const userId = new ObjectId(req.headers.userId as string)
167+
await deleteAllChatRooms(userId)
168+
res.send({ status: 'Success', message: null, data: null })
169+
}
170+
catch (error) {
171+
console.error(error)
172+
res.send({ status: 'Fail', message: 'Delete error', data: null })
173+
}
126174
})
127175

128176
router.post('/chat-clear', auth, async (req, res) => {
129-
const userId = req.headers.userId
130-
const { roomId } = req.body as { roomId: number }
131-
if (!roomId || !await existsChatRoom(userId, roomId)) {
132-
res.send({ status: 'Fail', message: 'Unknow room', data: null })
133-
return
134-
}
135-
await clearChat(roomId)
136-
res.send({ status: 'Success', message: null, data: null })
177+
try {
178+
const userId = new ObjectId(req.headers.userId as string)
179+
const { roomId } = req.body as { roomId: number }
180+
if (!roomId || !await existsChatRoom(userId, roomId)) {
181+
res.send({ status: 'Fail', message: 'Unknow room', data: null })
182+
return
183+
}
184+
await clearChat(roomId)
185+
res.send({ status: 'Success', message: null, data: null })
186+
}
187+
catch (error) {
188+
console.error(error)
189+
res.send({ status: 'Fail', message: 'Delete error', data: null })
190+
}
137191
})
138192

139193
router.post('/chat', auth, async (req, res) => {

service/src/storage/mongo.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ export async function existsChatRoom(userId: ObjectId, roomId: number) {
6868
return !!room
6969
}
7070

71+
export async function deleteAllChatRooms(userId: ObjectId) {
72+
await roomCol.updateMany({ userId, status: Status.Normal }, { $set: { status: Status.Deleted } })
73+
await chatCol.updateMany({ userId, status: Status.Normal }, { $set: { status: Status.Deleted } })
74+
}
75+
7176
export async function getChats(roomId: number, lastTime?: number) {
7277
if (!lastTime)
7378
lastTime = new Date().getTime()

src/api/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ export function fetchGetChatHistory<T = any>(roomId: number) {
108108
})
109109
}
110110

111+
export function fetchClearAllChat<T = any>() {
112+
return post<T>({
113+
url: '/chat-clear-all',
114+
data: { },
115+
})
116+
}
117+
111118
export function fetchClearChat<T = any>(roomId: number) {
112119
return post<T>({
113120
url: '/chat-clear',

src/components/common/Setting/General.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { UserInfo } from '@/store/modules/user/helper'
88
import { getCurrentDate } from '@/utils/functions'
99
import { useBasicLayout } from '@/hooks/useBasicLayout'
1010
import { t } from '@/locales'
11+
import { fetchClearAllChat } from '@/api'
1112
1213
const appStore = useAppStore()
1314
const userStore = useUserStore()
@@ -103,7 +104,8 @@ function importData(event: Event): void {
103104
reader.readAsText(file)
104105
}
105106
106-
function clearData(): void {
107+
async function clearData(): Promise<void> {
108+
await fetchClearAllChat()
107109
localStorage.removeItem('chatStorage')
108110
location.reload()
109111
}

src/components/common/Setting/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ const show = computed({
6969
</NTabPane>
7070
<NTabPane v-if="userStore.userInfo.root" name="SiteConfig" tab="SiteConfig">
7171
<template #tab>
72-
<SvgIcon class="text-lg" icon="ri:list-settings-line" />
72+
<SvgIcon class="text-lg" icon="ri:settings-line" />
7373
<span class="ml-2">{{ $t('setting.siteConfig') }}</span>
7474
</template>
7575
<Site />
7676
</NTabPane>
7777
<NTabPane v-if="userStore.userInfo.root" name="MailConfig" tab="MailConfig">
7878
<template #tab>
79-
<SvgIcon class="text-lg" icon="ri:list-settings-line" />
79+
<SvgIcon class="text-lg" icon="ri:mail-line" />
8080
<span class="ml-2">{{ $t('setting.mailConfig') }}</span>
8181
</template>
8282
<Mail />

0 commit comments

Comments
 (0)