fix(conversations): filter out empty conversations from list and count
Only include conversations with messages (last_seq > 0) in both the total count and paginated list queries, improving relevance of conversation lists.
This commit is contained in:
@@ -158,9 +158,10 @@ func (r *messageRepository) GetConversations(userID string, page, pageSize int)
|
|||||||
var convs []*model.Conversation
|
var convs []*model.Conversation
|
||||||
var total int64
|
var total int64
|
||||||
|
|
||||||
// 获取总数
|
// 获取总数(仅统计有消息的会话)
|
||||||
r.db.Model(&model.ConversationParticipant{}).
|
r.db.Model(&model.ConversationParticipant{}).
|
||||||
Where("user_id = ? AND hidden_at IS NULL", userID).
|
Joins("INNER JOIN conversations c ON c.id = conversation_participants.conversation_id").
|
||||||
|
Where("conversation_participants.user_id = ? AND conversation_participants.hidden_at IS NULL AND c.last_seq > 0", userID).
|
||||||
Count(&total)
|
Count(&total)
|
||||||
|
|
||||||
if total == 0 {
|
if total == 0 {
|
||||||
@@ -168,11 +169,9 @@ func (r *messageRepository) GetConversations(userID string, page, pageSize int)
|
|||||||
}
|
}
|
||||||
|
|
||||||
offset := (page - 1) * pageSize
|
offset := (page - 1) * pageSize
|
||||||
// 查询会话列表并预加载关联数据:
|
|
||||||
// 当前用户维度先按置顶排序,再按更新时间排序
|
|
||||||
err := r.db.Model(&model.Conversation{}).
|
err := r.db.Model(&model.Conversation{}).
|
||||||
Joins("INNER JOIN conversation_participants cp ON conversations.id = cp.conversation_id").
|
Joins("INNER JOIN conversation_participants cp ON conversations.id = cp.conversation_id").
|
||||||
Where("cp.user_id = ? AND cp.hidden_at IS NULL", userID).
|
Where("cp.user_id = ? AND cp.hidden_at IS NULL AND conversations.last_seq > 0", userID).
|
||||||
Preload("Group").
|
Preload("Group").
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
Limit(pageSize).
|
Limit(pageSize).
|
||||||
@@ -799,7 +798,7 @@ func (r *messageRepository) GetConversationsByCursor(ctx context.Context, userID
|
|||||||
// 构建基础查询 - 关联 conversation_participants 表
|
// 构建基础查询 - 关联 conversation_participants 表
|
||||||
query := r.db.WithContext(ctx).Model(&model.Conversation{}).
|
query := r.db.WithContext(ctx).Model(&model.Conversation{}).
|
||||||
Joins("INNER JOIN conversation_participants cp ON conversations.id = cp.conversation_id").
|
Joins("INNER JOIN conversation_participants cp ON conversations.id = cp.conversation_id").
|
||||||
Where("cp.user_id = ? AND cp.hidden_at IS NULL", userID).
|
Where("cp.user_id = ? AND cp.hidden_at IS NULL AND conversations.last_seq > 0", userID).
|
||||||
Preload("Group")
|
Preload("Group")
|
||||||
|
|
||||||
// 会话列表需要特殊处理:先按置顶排序,再按更新时间排序
|
// 会话列表需要特殊处理:先按置顶排序,再按更新时间排序
|
||||||
|
|||||||
Reference in New Issue
Block a user