Modify the code logic and add a mongo svc context

This commit is contained in:
wwweww
2026-04-25 08:29:25 +08:00
parent 5a99baafd0
commit 2ec2075c16
6 changed files with 103 additions and 265 deletions
+17 -19
View File
@@ -64,12 +64,6 @@ func (h *Handler) handleJoin(conn protocol.Connection, msg *WsMessage) error {
func (h *Handler) handleLeave(conn protocol.Connection, msg *WsMessage) error {
uid := h.getUserId(conn)
if uid <= 0 {
return conn.SendJSON(context.Background(), WsResponse{
Type: "error",
Content: "authentication required",
})
}
sessionId := msg.SessionId
if sessionId <= 0 {
if sid, ok := conn.Metadata()["sessionId"].(int64); ok {
@@ -79,12 +73,6 @@ func (h *Handler) handleLeave(conn protocol.Connection, msg *WsMessage) error {
if sessionId <= 0 {
return nil
}
if !h.svcCtx.Store.IsParticipant(sessionId, uid) {
return conn.SendJSON(context.Background(), WsResponse{
Type: "error",
Content: "not a member of this session",
})
}
session, err := h.svcCtx.Store.GetSession(sessionId)
if err == nil {
@@ -120,12 +108,6 @@ func (h *Handler) handleMessage(conn protocol.Connection, msg *WsMessage) error
Content: "sessionId is required, join a session first",
})
}
if !h.svcCtx.Store.IsParticipant(sessionId, uid) {
return conn.SendJSON(context.Background(), WsResponse{
Type: "error",
Content: "not a member of this session",
})
}
msgType := chatcore.MessageType(msg.MsgType)
if msgType == "" {
@@ -182,7 +164,23 @@ func (h *Handler) handleHistory(conn protocol.Connection, msg *WsMessage) error
Content: "sessionId is required",
})
}
if !h.svcCtx.Store.IsParticipant(msg.SessionId, uid) {
session, err := h.svcCtx.Store.GetSession(msg.SessionId)
if err != nil {
return conn.SendJSON(context.Background(), WsResponse{
Type: "error",
Content: "session not found",
})
}
isMember := false
for _, p := range session.Participants {
if p == uid {
isMember = true
break
}
}
if !isMember {
return conn.SendJSON(context.Background(), WsResponse{
Type: "error",
Content: "not a member of this session",