fix(server): improve message handling and data privacy
- fix unread count calculation logic in message repository - implement logic to retrieve other participant's last read sequence in conversation handlers - mask real names in verification record responses for privacy - improve error reporting in main entry point by using stderr instead of zap logger during initialization
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
@@ -14,9 +15,8 @@ func main() {
|
|||||||
// 初始化应用程序(Wire 自动生成)
|
// 初始化应用程序(Wire 自动生成)
|
||||||
app, err := InitializeApp()
|
app, err := InitializeApp()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
zap.L().Fatal("failed to initialize app",
|
fmt.Fprintf(os.Stderr, "failed to initialize app: %v\n", err)
|
||||||
zap.Error(err),
|
os.Exit(1)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启动应用程序
|
// 启动应用程序
|
||||||
|
|||||||
@@ -244,6 +244,12 @@ func (h *MessageHandler) GetConversationByID(c *gin.Context) {
|
|||||||
|
|
||||||
// 获取对方用户的已读位置
|
// 获取对方用户的已读位置
|
||||||
otherLastReadSeq := int64(0)
|
otherLastReadSeq := int64(0)
|
||||||
|
for _, p := range allParticipants {
|
||||||
|
if p.UserID != userID {
|
||||||
|
otherLastReadSeq = p.LastReadSeq
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
response.Success(c, dto.ConvertConversationToDetailResponse(conv, participants, unreadCount, nil, myLastReadSeq, otherLastReadSeq, isPinned, notificationMuted))
|
response.Success(c, dto.ConvertConversationToDetailResponse(conv, participants, unreadCount, nil, myLastReadSeq, otherLastReadSeq, isPinned, notificationMuted))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -768,6 +774,12 @@ func (h *MessageHandler) HandleGetConversation(c *gin.Context) {
|
|||||||
|
|
||||||
// 获取对方用户的已读位置
|
// 获取对方用户的已读位置
|
||||||
otherLastReadSeq := int64(0)
|
otherLastReadSeq := int64(0)
|
||||||
|
for _, p := range allParticipants {
|
||||||
|
if p.UserID != userID {
|
||||||
|
otherLastReadSeq = p.LastReadSeq
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
response.Success(c, dto.ConvertConversationToDetailResponse(conv, participants, unreadCount, nil, myLastReadSeq, otherLastReadSeq, isPinned, notificationMuted))
|
response.Success(c, dto.ConvertConversationToDetailResponse(conv, participants, unreadCount, nil, myLastReadSeq, otherLastReadSeq, isPinned, notificationMuted))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"with_you/internal/dto"
|
"with_you/internal/dto"
|
||||||
"with_you/internal/model"
|
"with_you/internal/model"
|
||||||
"with_you/internal/pkg/response"
|
"with_you/internal/pkg/response"
|
||||||
|
"with_you/internal/pkg/sanitizer"
|
||||||
"with_you/internal/service"
|
"with_you/internal/service"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -114,7 +115,7 @@ func convertVerificationRecordToResponse(record *model.VerificationRecord) *dto.
|
|||||||
UserID: record.UserID,
|
UserID: record.UserID,
|
||||||
Identity: string(record.Identity),
|
Identity: string(record.Identity),
|
||||||
Status: string(record.Status),
|
Status: string(record.Status),
|
||||||
RealName: record.RealName,
|
RealName: sanitizer.MaskValue("real_name", record.RealName),
|
||||||
StudentID: record.StudentID,
|
StudentID: record.StudentID,
|
||||||
EmployeeID: record.EmployeeID,
|
EmployeeID: record.EmployeeID,
|
||||||
Department: record.Department,
|
Department: record.Department,
|
||||||
|
|||||||
@@ -896,10 +896,11 @@ func (r *messageRepository) GetUnreadCountBatch(ctx context.Context, userID stri
|
|||||||
}
|
}
|
||||||
var rows []row
|
var rows []row
|
||||||
err := r.db.WithContext(ctx).
|
err := r.db.WithContext(ctx).
|
||||||
Table("conversation_participants cp").
|
Table("messages m").
|
||||||
Select("cp.conversation_id, COALESCE(cnt.unread, 0) as count").
|
Select("m.conversation_id, COUNT(m.id) as count").
|
||||||
Joins("LEFT JOIN (SELECT m.conversation_id, COUNT(m.id) as unread FROM messages m WHERE m.sender_id <> ? AND m.deleted_at IS NULL AND m.conversation_id IN (?) GROUP BY m.conversation_id) cnt ON cnt.conversation_id = cp.conversation_id AND cnt.unread > cp.last_read_seq", userID, convIDs).
|
Joins("JOIN conversation_participants cp ON cp.conversation_id = m.conversation_id AND cp.user_id = ?", userID).
|
||||||
Where("cp.user_id = ? AND cp.conversation_id IN (?)", userID, convIDs).
|
Where("m.sender_id <> ? AND m.deleted_at IS NULL AND m.seq > cp.last_read_seq AND m.conversation_id IN (?)", userID, convIDs).
|
||||||
|
Group("m.conversation_id").
|
||||||
Scan(&rows).Error
|
Scan(&rows).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user