修复聊天消息不显示最新数据:所有列表查询排序从 created_at DESC 改为 id DESC,记录创建函数显式设置 CreatedAt,后端 v1.2.1 / 前端 v0.3.1

This commit is contained in:
2026-08-07 22:21:26 +08:00
parent b6fabf59b7
commit 92e766dfa8
11 changed files with 19 additions and 12 deletions
+2 -1
View File
@@ -416,8 +416,9 @@ func (s *Service) recordOutboundDirectMessage(bot *storepkg.BotNodeRecord, msg *
BotMessageID: botMessageID,
CreatedBy: createdByPtr,
PublishedAt: msg.PublishedAt,
// 出向消息从产生那一刻起就视为已读,未读计数只关心 inbound。
// 出向消息从产生那一刻起就视为"已读",未读计数只关心 inbound。
ReadAt: &now,
CreatedAt: now,
}
if err := s.store.InsertBotDirectMessage(dm); err != nil {
printJSON(map[string]any{
+1 -2
View File
@@ -69,7 +69,6 @@ func (s *Store) ListBotDirectMessagesByConversation(opts BotDirectMessageListOpt
var rows []BotDirectMessageRecord
q := s.db.Model(&BotDirectMessageRecord{}).
Where("bot_id = ? AND peer_node_num = ?", opts.BotID, opts.PeerNodeNum).
Order("created_at DESC").
Order("id DESC").
Limit(opts.Limit).
Offset(opts.Offset)
@@ -159,7 +158,6 @@ func (s *Store) ListBotDirectConversations(botID uint64, opts ListOptions) ([]Bo
q := s.db.Table("(?) AS agg", subLast).
Select("agg.bot_id AS bot_id, agg.peer_node_id AS peer_node_id, agg.peer_node_num AS peer_node_num, m.created_at AS last_message_at, m.text AS last_text, m.direction AS last_direction, agg.unread_count AS unread_count, agg.total_count AS total_count").
Joins("JOIN bot_direct_messages m ON m.id = agg.last_id").
Order("m.created_at DESC").
Order("m.id DESC").
Limit(opts.Limit).
Offset(opts.Offset)
@@ -284,6 +282,7 @@ func insertInboundBotDirectMessage(s *Store, record map[string]any, clientInfo M
Status: BotMessageStatusPublished,
ReceivedAt: &now,
ContentJSON: contentPtr,
CreatedAt: now,
}
if err := s.InsertBotDirectMessage(dm); err != nil {
return fmt.Errorf("insert bot direct message from %s: %w", peerNodeID, err)
-1
View File
@@ -211,7 +211,6 @@ func (s *Store) ListBotMessages(opts BotMessageListOptions) ([]BotMessageRecord,
opts.ListOptions = NormalizeListOptions(opts.ListOptions)
var rows []BotMessageRecord
q := applyBotMessageFilters(s.db.Model(&BotMessageRecord{}), opts).
Order("created_at DESC").
Order("id DESC").
Limit(opts.Limit).
Offset(opts.Offset)
+2
View File
@@ -1291,6 +1291,7 @@ func textMessageFromRecord(record map[string]any, clientInfo MQTTClientInfo) (*T
MQTTRemoteHost: clientFields.MQTTRemoteHost,
MQTTRemotePort: clientFields.MQTTRemotePort,
ContentJSON: common.ContentJSON,
CreatedAt: time.Now(),
}, nil
}
@@ -1401,6 +1402,7 @@ func AppendPacketFieldsFromRecord(record map[string]any, wantType string, client
DecryptSuccess: nullableBool(record["decrypt_success"]),
DecryptStatus: NullableString(record["decrypt_status"]),
ContentJSON: string(contentJSON),
CreatedAt: time.Now(),
}, MQTTClientRecordFields{
MQTTClientID: NullableString(clientInfo.ClientID),
MQTTUsername: NullableString(clientInfo.Username),
+2
View File
@@ -4,6 +4,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"time"
)
func (s *Store) InsertDiscardDetails(record map[string]any, raw []byte, clientInfo MQTTClientInfo) error {
@@ -34,6 +35,7 @@ func discardDetailsFromRecord(record map[string]any, raw []byte, clientInfo MQTT
MQTTRemoteAddr: NullableStringValue(clientInfo.RemoteAddr),
MQTTRemoteHost: NullableStringValue(clientInfo.RemoteHost),
MQTTRemotePort: NullableStringValue(clientInfo.RemotePort),
CreatedAt: time.Now(),
}, nil
}
+2 -1
View File
@@ -369,6 +369,7 @@ func (s *Store) EnqueueLLMMessage(input LLMMessageQueueInput) (*LLMMessageQueueR
Status: LLMMessageStatusPending,
ReceivedAt: now,
ContentJSON: input.ContentJSON,
CreatedAt: now,
}
if err := s.db.Create(record).Error; err != nil {
@@ -396,7 +397,7 @@ func (s *Store) ListLLMMessages(opts ListOptions, botID uint64, includeDeleted b
}
// 排序和分页
query = query.Order("created_at DESC")
query = query.Order("id DESC")
if opts.Limit > 0 {
query = query.Limit(opts.Limit)
}
+6 -1
View File
@@ -1,13 +1,18 @@
package store
import "time"
func (s *Store) InsertLoginLog(log LoginLogRecord) error {
if log.CreatedAt.IsZero() {
log.CreatedAt = time.Now()
}
return s.db.Create(&log).Error
}
func (s *Store) ListLoginLogs(opts ListOptions) ([]LoginLogRecord, error) {
opts = NormalizeListOptions(opts)
var rows []LoginLogRecord
q := s.db.Order("created_at DESC").Order("id DESC").Limit(opts.Limit).Offset(opts.Offset)
q := s.db.Order("id DESC").Limit(opts.Limit).Offset(opts.Offset)
if opts.Since != nil {
q = q.Where("created_at >= ?", *opts.Since)
}
+1 -2
View File
@@ -316,7 +316,6 @@ func (s *Store) ListDiscardDetails(opts ListOptions) ([]DiscardDetailsRecord, er
opts = NormalizeListOptions(opts)
var rows []DiscardDetailsRecord
q := applyDiscardDetailsFilters(s.db.Model(&DiscardDetailsRecord{}), opts).
Order("created_at DESC").
Order("id DESC").
Limit(opts.Limit).
Offset(opts.Offset)
@@ -385,7 +384,7 @@ func (s *Store) ListTraceroute(opts ListOptions) ([]TracerouteRecord, error) {
func (s *Store) listAppendRows(opts ListOptions, dest any) *gorm.DB {
opts = NormalizeListOptions(opts)
q := s.db.Order("created_at DESC").Order("id DESC").Limit(opts.Limit).Offset(opts.Offset)
q := s.db.Order("id DESC").Limit(opts.Limit).Offset(opts.Offset)
if opts.NodeID != "" {
q = q.Where("from_id = ?", opts.NodeID)
}
+1 -1
View File
@@ -84,7 +84,7 @@ func NewRouter(cfg configpkg.WebConfig, consoleLog bool, store *storepkg.Store,
return r
}
const BackendVersion = "1.2.0"
const BackendVersion = "1.2.1"
var CommitVersion = "dev"
+1 -2
View File
@@ -257,8 +257,7 @@ function toChronological(items: TextMessage[]): TextMessage[] {
}
function compareMessages(a: TextMessage, b: TextMessage): number {
const timeDiff = Date.parse(a.created_at) - Date.parse(b.created_at)
return timeDiff !== 0 ? timeDiff : a.id - b.id
return a.id - b.id
}
function mergeMessages(existing: TextMessage[], incoming: TextMessage[]): TextMessage[] {
+1 -1
View File
@@ -1 +1 @@
export const FRONTEND_VERSION = '0.3.0'
export const FRONTEND_VERSION = '0.3.1'