From 3215039ff6ebb6d3764f5d4774c164c39186f1e4 Mon Sep 17 00:00:00 2001 From: lafay <2021211506@stu.hit.edu.cn> Date: Sat, 25 Apr 2026 15:03:37 +0800 Subject: [PATCH] fix(conversations): always show group chats regardless of message count Update conversation list queries to distinguish between private and group chats: private conversations are only shown when they have messages (last_seq > 0), while group conversations are always displayed even without messages. This applies to both the paginated GetConversations method and the cursor-based GetConversationsByCursor method. --- internal/repository/message_repo.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/repository/message_repo.go b/internal/repository/message_repo.go index a6b21ba..9c0b29d 100644 --- a/internal/repository/message_repo.go +++ b/internal/repository/message_repo.go @@ -158,10 +158,10 @@ func (r *messageRepository) GetConversations(userID string, page, pageSize int) var convs []*model.Conversation var total int64 - // 获取总数(仅统计有消息的会话) + // 获取总数(私聊仅统计有消息的会话,群聊始终显示) r.db.Model(&model.ConversationParticipant{}). 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). + Where("conversation_participants.user_id = ? AND conversation_participants.hidden_at IS NULL AND (c.type != ? OR c.last_seq > 0)", userID, model.ConversationTypePrivate). Count(&total) if total == 0 { @@ -171,7 +171,7 @@ func (r *messageRepository) GetConversations(userID string, page, pageSize int) offset := (page - 1) * pageSize err := r.db.Model(&model.Conversation{}). Joins("INNER JOIN conversation_participants cp ON conversations.id = cp.conversation_id"). - Where("cp.user_id = ? AND cp.hidden_at IS NULL AND conversations.last_seq > 0", userID). + Where("cp.user_id = ? AND cp.hidden_at IS NULL AND (conversations.type != ? OR conversations.last_seq > 0)", userID, model.ConversationTypePrivate). Preload("Group"). Offset(offset). Limit(pageSize). @@ -798,7 +798,7 @@ func (r *messageRepository) GetConversationsByCursor(ctx context.Context, userID // 构建基础查询 - 关联 conversation_participants 表 query := r.db.WithContext(ctx).Model(&model.Conversation{}). Joins("INNER JOIN conversation_participants cp ON conversations.id = cp.conversation_id"). - Where("cp.user_id = ? AND cp.hidden_at IS NULL AND conversations.last_seq > 0", userID). + Where("cp.user_id = ? AND cp.hidden_at IS NULL AND (conversations.type != ? OR conversations.last_seq > 0)", userID, model.ConversationTypePrivate). Preload("Group") // 会话列表需要特殊处理:先按置顶排序,再按更新时间排序