From 27a2e45500627f7f97fff582265143e34026c292 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Wed, 2 Dec 2020 19:47:32 +0100 Subject: [PATCH] Implement /subgroups. --- group/group.go | 24 ++++++++++++++++++++++++ rtpconn/webclient.go | 21 +++++++++++++++++++++ static/sfu.js | 8 ++++++++ 3 files changed, 53 insertions(+) diff --git a/group/group.go b/group/group.go index e766aab..f3b5f57 100644 --- a/group/group.go +++ b/group/group.go @@ -259,6 +259,30 @@ func GetNames() []string { return names } +type SubGroup struct { + Name string + Clients int +} + +func GetSubGroups(parent string) []SubGroup { + prefix := parent + "/" + subgroups := make([]SubGroup, 0) + + Range(func(g *Group) bool { + if strings.HasPrefix(g.name, prefix) { + g.mu.Lock() + count := len(g.clients) + g.mu.Unlock() + if count > 0 { + subgroups = append(subgroups, + SubGroup{g.name, count}) + } + } + return true + }) + return subgroups +} + func Get(name string) *Group { groups.mu.Lock() defer groups.mu.Unlock() diff --git a/rtpconn/webclient.go b/rtpconn/webclient.go index 30c83ca..389e27f 100644 --- a/rtpconn/webclient.go +++ b/rtpconn/webclient.go @@ -7,6 +7,7 @@ import ( "os" "sync" "time" + "fmt" "github.com/gorilla/websocket" "github.com/pion/webrtc/v3" @@ -1218,6 +1219,26 @@ func handleClientMessage(c *webClient, m clientMessage) error { group.DelClient(disk) } } + case "subgroups": + if !c.permissions.Op { + return c.error(group.UserError("not authorised")) + } + s := "" + for _, sg := range group.GetSubGroups(g.Name()) { + plural := "" + if sg.Clients > 1 { + plural = "s" + } + s = s + fmt.Sprintf("%v (%v client%v)", + sg.Name, sg.Clients, plural) + } + c.write(clientMessage{ + Type: "chat", + Dest: c.id, + Username: "Server", + Time: group.ToJSTime(time.Now()), + Value: &s, + }) default: return group.ProtocolError("unknown group action") } diff --git a/static/sfu.js b/static/sfu.js index c48bc41..7872ef0 100644 --- a/static/sfu.js +++ b/static/sfu.js @@ -1759,6 +1759,14 @@ commands.unrecord = { } }; +commands.subgroups = { + predicate: operatorPredicate, + description: 'list subgroups', + f: (c, r) => { + serverConnection.groupAction(getUsername(), 'subgroups'); + } +}; + /** * parseCommand splits a string into two space-separated parts. The first * part may be quoted and may include backslash escapes.