From 940f14e2d6217efc2d09e3951b53a1af6d9246e5 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sun, 3 Sep 2023 12:48:21 -0700 Subject: [PATCH] VIP-only chat channels --- pkg/config/config.go | 19 +++++++++++++++++++ pkg/websocket.go | 6 ++++++ web/static/js/BareRTC.js | 3 +++ 3 files changed, 28 insertions(+) diff --git a/pkg/config/config.go b/pkg/config/config.go index de84ef3..740a73b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -68,11 +68,22 @@ func (c Config) GetChannels() template.JS { return template.JS(data) } +// GetChannel looks up and returns a channel by ID. +func (c Config) GetChannel(id string) (Channel, bool) { + for _, ch := range c.PublicChannels { + if ch.ID == id { + return ch, true + } + } + return Channel{}, false +} + // Channel config for a default public room. type Channel struct { ID string // Like "lobby" Name string // Like "Main Chat Room" Icon string `toml:",omitempty"` // CSS class names for room icon (optional) + VIP bool // For VIP users only // ChatServer messages to send to the user immediately upon connecting. WelcomeMessages []string @@ -118,6 +129,14 @@ func DefaultConfig() Config { "Welcome to the Off Topic channel!", }, }, + { + ID: "vip", + Name: "VIPs Only", + VIP: true, + WelcomeMessages: []string{ + "This channel is only for operators and VIPs.", + }, + }, }, TURN: TurnConfig{ URLs: []string{ diff --git a/pkg/websocket.go b/pkg/websocket.go index 5b5210d..d97f5b3 100644 --- a/pkg/websocket.go +++ b/pkg/websocket.go @@ -336,6 +336,12 @@ func (s *Server) Broadcast(msg messages.Message) { continue } + // VIP channels: only deliver to subscribed VIP users. + if ch, ok := config.Current.GetChannel(msg.Channel); ok && ch.VIP && !sub.IsVIP() { + log.Debug("Do not broadcast message to %s: VIP channel and they are not VIP", sub.Username) + continue + } + sub.SendJSON(msg) } } diff --git a/web/static/js/BareRTC.js b/web/static/js/BareRTC.js index 6b07f4b..cdab1dc 100644 --- a/web/static/js/BareRTC.js +++ b/web/static/js/BareRTC.js @@ -1575,6 +1575,9 @@ const app = Vue.createApp({ // List of current channels, unread indicators etc. let result = []; for (let channel of this.config.channels) { + // VIP room we can't see? + if (channel.VIP && !this.isVIP) continue; + let data = { ID: channel.ID, Name: channel.Name,