From ef98a6bc54af5081b43d50d9ad7b465fa1407783 Mon Sep 17 00:00:00 2001 From: mountain Date: Wed, 14 Feb 2024 17:20:05 +0900 Subject: [PATCH] =?UTF-8?q?EnterPublicChannel=EC=9D=B4=20=EC=99=9C=20?= =?UTF-8?q?=EC=97=AC=EB=9F=AC=EB=B2=88=20=EB=B6=88=EB=A6=BC=3F=3F=3F=20?= =?UTF-8?q?=EB=B0=A9=EC=96=B4=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/group_chat.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/core/group_chat.go b/core/group_chat.go index e5b0edf..24e1724 100644 --- a/core/group_chat.go +++ b/core/group_chat.go @@ -111,6 +111,7 @@ func (gc *groupChat) ClientConnected(conn *websocket.Conn, callby *wshandler.Sen func (gc *groupChat) ClientDisconnected(msg string, callby *wshandler.Sender) { docs, _ := gc.rh.JSONGetDocuments(callby.Accid.Hex(), "$.channel") + logger.Println("groupchat ClientDisconnected docs :", callby.Alias, docs) if len(docs) > 0 { for k, v := range docs[0] { @@ -118,14 +119,18 @@ func (gc *groupChat) ClientDisconnected(msg string, callby *wshandler.Sender) { chanid := v.(string) gc.leaveRoom(chanid, callby.Accid) if k == "public" { - cnt, _ := gc.rh.JSONDel(chanid, "$.members."+callby.Alias) + cnt, err := gc.rh.JSONDel(chanid, "$.members."+callby.Alias) if cnt > 0 { - gc.rh.JSONNumIncrBy(chanid, "$.size", -1) if cfg, ok := gc.chatConfig.Channels[chanid]; ok { cfg.inoutChan <- "-" + callby.Alias } + } else if err != nil { + logger.Println("groupchat ClientDisconnected JSONDel err :", err, callby.Alias) + } else { + logger.Println("groupchat ClientDisconnected JSONDel cnt 0 :", callby.Alias) } } else { + logger.Println("groupchat ClientDisconnected leave private channel :", chanid, callby.Alias) gc.sendUpstreamMessage(&wshandler.UpstreamMessage{ Target: "#" + chanid, Body: map[string]any{"sender": callby.Alias}, @@ -137,6 +142,8 @@ func (gc *groupChat) ClientDisconnected(msg string, callby *wshandler.Sender) { } func (gc *groupChat) EnterPublicChannel(ctx wshandler.ApiCallContext) { + gc.LeavePublicChannel(ctx) + chanid := ctx.Arguments[0].(string) if cfg, ok := gc.chatConfig.Channels[chanid]; ok { size, err := gc.rh.JSONObjLen(chanid, "$.members") @@ -146,6 +153,7 @@ func (gc *groupChat) EnterPublicChannel(ctx wshandler.ApiCallContext) { // 입장 _, err := gc.rh.JSONSet(chanid, "$.members."+ctx.CallBy.Alias, 1) if err == nil { + logger.Println("groupchat EnterPublicChannel JSONSet :", chanid, ctx.CallBy.Alias) gc.enterRoom(chanid, ctx.CallBy.Accid) gc.rh.JSONSet(ctx.CallBy.Accid.Hex(), "$.channel.public", chanid) cfg.inoutChan <- "+" + ctx.CallBy.Alias @@ -179,18 +187,22 @@ func (gc *groupChat) EnterPublicChannel(ctx wshandler.ApiCallContext) { } func (gc *groupChat) LeavePublicChannel(ctx wshandler.ApiCallContext) { - chanid := ctx.Arguments[0].(string) - cnt, _ := gc.rh.JSONDel(ctx.CallBy.Accid.Hex(), "$.channel.public") - if cnt > 0 { - cnt, _ = gc.rh.JSONDel(chanid, "$.members."+ctx.CallBy.Alias) + oldchans, _ := gc.rh.JSONGetString(ctx.CallBy.Accid.Hex(), "$.channel.public") + for _, oldchan := range oldchans { + cnt, err := gc.rh.JSONDel(oldchan, "$.members."+ctx.CallBy.Alias) if cnt > 0 { - gc.leaveRoom(chanid, ctx.CallBy.Accid) - gc.rh.JSONNumIncrBy(chanid, "$.size", -1) - if cfg, ok := gc.chatConfig.Channels[chanid]; ok { + logger.Println("groupchat LeavePublicChannel JSONDel :", oldchan, ctx.CallBy.Alias) + gc.leaveRoom(oldchan, ctx.CallBy.Accid) + if cfg, ok := gc.chatConfig.Channels[oldchan]; ok { cfg.inoutChan <- "-" + ctx.CallBy.Alias } + } else if err != nil { + logger.Println("groupchat LeavePublicChannel JSONDel err :", err, oldchan, ctx.CallBy.Alias) + } else { + logger.Println("groupchat LeavePublicChannel JSONDel cnt 0 :", oldchan, ctx.CallBy.Alias) } } + gc.rh.JSONDel(ctx.CallBy.Accid.Hex(), "$.channel.public") } func (gc *groupChat) TextMessage(ctx wshandler.ApiCallContext) {