Repository navigation
fix: After posting a new question within an existing conversation in the portal, the conversation thread did not move to the top of the list #7890
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -229,7 +229,8 @@ export function createMessageInputStore(deps: MessageInputDeps) { | |
|
|
||
| const isFirstMessage = messages.value.length === 0 | ||
| const abstract = questionText.substring(0, 256) | ||
| if (isFirstMessage && !conversations.value.some((conversation) => conversation.id === chatId)) { | ||
| const conversation = conversations.value.find((item) => item.id === chatId) | ||
| if (isFirstMessage && !conversation) { | ||
| // 本地新会话尚未由历史接口返回,使用首次发送时间展示相对时间。 | ||
| const createTime = new Date().toISOString() | ||
| conversations.value.unshift({ | ||
|
|
@@ -238,6 +239,14 @@ export function createMessageInputStore(deps: MessageInputDeps) { | |
| create_time: createTime, | ||
| update_time: createTime, | ||
| }) | ||
| } else if (conversation) { | ||
| // 在已有会话中继续提问:移到历史列表顶部并刷新更新时间,与后端按 update_time 倒序展示一致。 | ||
| conversation.update_time = new Date().toISOString() | ||
| const index = conversations.value.indexOf(conversation) | ||
| if (index > 0) { | ||
| conversations.value.splice(index, 1) | ||
| conversations.value.unshift(conversation) | ||
| } | ||
| } | ||
|
|
||
| sendMessage({ | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is an analysis of the provided code snippet, focusing on Reactivity, Performance, Logic, and Best Practices. 1. Reactivity & State Management (Critical Issue)Issue: Direct mutation of object properties (
2. Performance & ImmutabilityIssue: The code uses
3. Code Logic & Edge CasesIssue:
4. Time HandlingIssue:
5. Suggested Refactored Code (Optimized for Readability & Safety)@@ -229,7 +229,8 @@ export function createMessageInputStore(deps: MessageInputDeps) {
const isFirstMessage = messages.value.length === 0;
const abstract = questionText.substring(0, 256);
- if (isFirstMessage && !conversations.value.some((conversation) => conversation.id === chatId)) {
+ // Use findIndex to get both existence check and position in one pass
+ const existingIndex = conversations.value.findIndex((item) => item.id === chatId);
+
+ if (isFirstMessage && existingIndex === -1) {
// 本地新会话尚未由历史接口返回,使用首次发送时间展示相对时间。
const createTime = new Date().toISOString();
conversations.value.unshift({
@@ -238,6 +239,18 @@ export function createMessageInputStore(deps: MessageInputDeps) {
create_time: createTime,
update_time: createTime,
});
+ } else if (existingIndex !== -1) {
+ // 在已有会话中继续提问:移到顶部并更新时间
+ // Note: Accessing the item via index ensures we mutate the correct reactive proxy in the array
+ const conversation = conversations.value[existingIndex];
+
+ // Update time
+ conversation.update_time = new Date().toISOString();
+
+ // Move to top only if not already at top and index > 0
+ if (existingIndex > 0) {
+ conversations.value.splice(existingIndex, 1);
+ conversations.value.unshift(conversation);
+ }
}
sendMessage({Key Improvements in Refactor:
Final Recommendation:If this is Vue 3, the code is logically sound but the refactor above is slightly more performant. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall the change is reasonable: after inserting a new
ChatRecord, updating the parentChat.update_timekeeps history ordering correct. A few points to check:Prefer the model manager instead of
QuerySet(Chat)QuerySet(Chat).filter(...)is not standard Django usage and may bypass custom managers or fail depending on your imports. UseChat.objectsor the project’s established manager.Wrap the two writes in a transaction
This avoids a stale
update_timeif the insert succeeds but the update fails. Checkingupdated == 0also catches an invalid/missingchat_id.Validate
chat_idand scope it by user/tenant if neededIf
chat_idcomes from request data, add ownership/tenant constraints:Otherwise an unexpected ID could update another user’s chat timestamp.
QuerySet.update()bypassessave()and signalsIt does not trigger model
save(),pre_save/post_savesignals, orauto_nowhandling automatically. Since you explicitly setupdate_time=timezone.now(), that is fine, but make sure no audit/cache-invalidation logic depends onChat.save().Consider an index for history ordering
Since the history list sorts by
update_timedescending, ensure there is an appropriate DB index, e.g.(user_id, update_time)or(tenant_id, update_time)depending on filters.Optional: use database time
If app-server vs database clock skew matters:
timezone.now()is usually fine.No major logic problem is visible, but the main improvements are using
Chat.objects, adding transaction safety, and validating the updated row count.