fix: fix null conversation problems

This commit is contained in:
ikechan8370 2023-02-22 12:01:14 +08:00
parent cffda09847
commit a2c4d516a3

View file

@ -45,7 +45,7 @@ export async function getConversations (qq = '') {
logger.mark('conversation detail for conversation ' + item.id, conversationDetail) logger.mark('conversation detail for conversation ' + item.id, conversationDetail)
} }
conversationDetail = JSON.parse(conversationDetail) conversationDetail = JSON.parse(conversationDetail)
let messages = Object.values(conversationDetail.mapping) let messages = Object.values(conversationDetail.mapping || {})
messages = messages messages = messages
.filter(message => message.message) .filter(message => message.message)
@ -60,12 +60,12 @@ export async function getConversations (qq = '') {
if (messagesUser.length > 0) { if (messagesUser.length > 0) {
lastMessage = messagesUser[0].content.parts[0] lastMessage = messagesUser[0].content.parts[0]
await redis.set(`CHATGPT:CONVERSATION_LAST_MESSAGE_PROMPT:${item.id}`, lastMessage) await redis.set(`CHATGPT:CONVERSATION_LAST_MESSAGE_PROMPT:${item.id}`, lastMessage)
map[item.id] = lastMessage
} }
if (messagesAssistant.length > 0) { if (messagesAssistant.length > 0) {
await redis.set(`CHATGPT:CONVERSATION_LAST_MESSAGE_ID:${item.id}`, messagesAssistant[0].id) await redis.set(`CHATGPT:CONVERSATION_LAST_MESSAGE_ID:${item.id}`, messagesAssistant[0].id)
} }
await redis.set(`CHATGPT:CONVERSATION_CREATE_TIME:${item.id}`, new Date(conversationDetail.create_time * 1000).toLocaleString()) await redis.set(`CHATGPT:CONVERSATION_CREATE_TIME:${item.id}`, new Date(conversationDetail.create_time * 1000).toLocaleString())
map[item.id] = lastMessage
} }
} }
let res = [] let res = []
@ -73,23 +73,25 @@ export async function getConversations (qq = '') {
if (qq) { if (qq) {
usingConversationId = await redis.get(`CHATGPT:QQ_CONVERSATION:${qq}`) usingConversationId = await redis.get(`CHATGPT:QQ_CONVERSATION:${qq}`)
} }
let promisesPostProcess = result.map(async conversation => { let promisesPostProcess = result
conversation.lastPrompt = map[conversation.id] .filter(conversation => map[conversation.id])
conversation.create_time = new Date(conversation.create_time).toLocaleString() .map(async conversation => {
// 这里的时间格式还可以。不用管了。conversation.create_time = conversation.lastPrompt = map[conversation.id]
// title 全是 New chat不要了 conversation.create_time = new Date(conversation.create_time).toLocaleString()
delete conversation.title // 这里的时间格式还可以。不用管了。conversation.create_time =
conversation.creater = await redis.get(`CHATGPT:CONVERSATION_CREATER_NICK_NAME:${conversation.id}`) // title 全是 New chat不要了
if (qq && conversation.id === usingConversationId) { delete conversation.title
conversation.status = 'using' conversation.creater = await redis.get(`CHATGPT:CONVERSATION_CREATER_NICK_NAME:${conversation.id}`)
} else { if (qq && conversation.id === usingConversationId) {
conversation.status = 'normal' conversation.status = 'using'
} } else {
if (conversation.lastPrompt?.length > 80) { conversation.status = 'normal'
conversation.lastPrompt = conversation.lastPrompt.slice(0, 80) + '......' }
} if (conversation.lastPrompt?.length > 80) {
res.push(conversation) conversation.lastPrompt = conversation.lastPrompt.slice(0, 80) + '......'
}) }
res.push(conversation)
})
await Promise.all(promisesPostProcess) await Promise.all(promisesPostProcess)
return res return res
} }