优化了消息数量,筛选和顶部部分
This commit is contained in:
@@ -51,6 +51,10 @@ type MessageDTO struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type UnreadCountDTO struct {
|
||||
UnreadCount int64 `json:"unread_count"`
|
||||
}
|
||||
|
||||
type SendMessageRequest struct {
|
||||
Content string `json:"content"`
|
||||
AttachmentURLS []string `json:"attachment_urls"`
|
||||
|
||||
@@ -15,6 +15,20 @@ func (h *Handler) List(c *gin.Context) {
|
||||
h.list(c, Principal{Type: "user", ID: userID})
|
||||
}
|
||||
|
||||
func (h *Handler) UnreadCount(c *gin.Context) {
|
||||
userID, ok := currentUserID(c)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "缺少用户上下文")
|
||||
return
|
||||
}
|
||||
result, err := h.service.CountUnreadMessages(c.Request.Context(), Principal{Type: "user", ID: userID})
|
||||
if err != nil {
|
||||
writeChatError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, result)
|
||||
}
|
||||
|
||||
func (h *Handler) Detail(c *gin.Context) {
|
||||
userID, ok := currentUserID(c)
|
||||
if !ok {
|
||||
|
||||
@@ -40,6 +40,18 @@ func (r *Repository) conversationQuery(ctx context.Context, principal Principal)
|
||||
Joins("JOIN chat_participants AS cp ON cp.conversation_id = c.id").
|
||||
Where("cp.participant_type = ? AND cp.participant_id = ?", principal.Type, principal.ID)
|
||||
}
|
||||
|
||||
func (r *Repository) CountUnreadMessages(ctx context.Context, principal Principal) (int64, error) {
|
||||
var total int64
|
||||
err := r.db.WithContext(ctx).Table("chat_messages AS cm").
|
||||
Joins("JOIN chat_participants AS cp ON cp.conversation_id = cm.conversation_id").
|
||||
Where("cp.participant_type = ? AND cp.participant_id = ?", principal.Type, principal.ID).
|
||||
Where("NOT (cm.sender_type = ? AND cm.sender_id = ?)", principal.Type, principal.ID).
|
||||
Where("(cp.last_read_at IS NULL OR cm.created_at > cp.last_read_at)").
|
||||
Count(&total).Error
|
||||
return total, err
|
||||
}
|
||||
|
||||
func (r *Repository) findParticipant(tx *gorm.DB, principal Principal, conversationID uint64, lock bool) (*model.ChatParticipant, error) {
|
||||
var participant model.ChatParticipant
|
||||
db := tx
|
||||
|
||||
@@ -29,6 +29,17 @@ func (s *Service) ListConversations(ctx context.Context, principal Principal, pa
|
||||
return s.repo.ListConversations(ctx, principal, page, pageSize)
|
||||
}
|
||||
|
||||
func (s *Service) CountUnreadMessages(ctx context.Context, principal Principal) (*UnreadCountDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
total, err := s.repo.CountUnreadMessages(ctx, principal)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &UnreadCountDTO{UnreadCount: total}, nil
|
||||
}
|
||||
|
||||
func (s *Service) FindConversation(ctx context.Context, principal Principal, id uint64) (*ConversationDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
|
||||
@@ -403,6 +403,7 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
|
||||
chatRoutes.GET("/events", chatHubHandler.UserEvents)
|
||||
}
|
||||
chatRoutes.POST("/support", chatHandler.EnsureSupportConversation)
|
||||
chatRoutes.GET("/unread-count", chatHandler.UnreadCount)
|
||||
chatRoutes.GET("", chatHandler.List)
|
||||
chatRoutes.GET("/:id", chatHandler.Detail)
|
||||
chatRoutes.GET("/:id/messages", chatHandler.Messages)
|
||||
|
||||
Reference in New Issue
Block a user