From 197ba81529d5c71d90b22bfb64f7e425fe07fa7a Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Tue, 16 Apr 2024 23:03:16 +0800 Subject: [PATCH] Feat: ban user and kick user from hub --- internal/op/hub.go | 16 ++++++++++++++++ internal/op/room.go | 5 ++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/internal/op/hub.go b/internal/op/hub.go index ae37715..1475085 100644 --- a/internal/op/hub.go +++ b/internal/op/hub.go @@ -246,3 +246,19 @@ func (h *Hub) IsOnline(userID string) bool { _, ok := h.clients.Load(userID) return ok } + +func (h *Hub) KickUser(userID string) error { + if h.Closed() { + return ErrAlreadyClosed + } + cli, ok := h.clients.Load(userID) + if !ok { + return nil + } + cli.lock.RLock() + defer cli.lock.RUnlock() + for c := range cli.m { + c.Close() + } + return nil +} diff --git a/internal/op/room.go b/internal/op/room.go index bf77861..963283a 100644 --- a/internal/op/room.go +++ b/internal/op/room.go @@ -448,7 +448,10 @@ func (r *Room) BanMember(userID string) error { if r.IsCreator(userID) { return errors.New("you are creator, cannot ban") } - defer r.members.Delete(userID) + defer func() { + r.members.Delete(userID) + _ = r.hub.KickUser(userID) + }() return db.RoomBanMember(r.ID, userID) }