Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

feat(plugins): carry full sender identity on chat events

Gabe Kangasd4f94923cb2d
diff --git a/pluginhost/pluginevents.go b/pluginhost/pluginevents.go
index 1e1be5f3e6..e8249216a8 100644
--- a/pluginhost/pluginevents.go
+++ b/pluginhost/pluginevents.go
@@ -15,11 +15,18 @@ import (
// TypeScript interfaces (ChatMessage, ChatUser, …) so a plugin's typed
// handlers receive exactly the documented JSON.
+// pluginChatMessage is the chat.message.received (and chat.filter) payload.
+// It carries the full sender identity as a nested object plus the originating
+// connection's clientId so plugins can do reliable per-user state and
+// moderation (e.g. gate on scopes) and reply privately to the sender via
+// owncast.chat.sendTo / replyTo. User is nil for the rare message with no
+// associated account.
type pluginChatMessage struct {
- ID string `json:"id"`
- User string `json:"user"`
- Body string `json:"body"`
- Timestamp string `json:"timestamp"`
+ ID string `json:"id"`
+ User *pluginChatUser `json:"user,omitempty"`
+ ClientID uint `json:"clientId,omitempty"`
+ Body string `json:"body"`
+ Timestamp string `json:"timestamp"`
}
type pluginChatUser struct {
@@ -70,10 +77,6 @@ func newPluginChatFilter(pluginDispatcher *plugins.Dispatcher) dispatcher.Filter
if !ok {
return true // not a chat message we filter; let it pass
}
- user := ""
- if msg.User != nil {
- user = msg.User.DisplayName
- }
// Carry the timestamp through so filter plugins that gate on it
// (slow-mode, rate-limit, etc.) can compare elapsed time. Nano-
// precision matters: extism-js's Date.now() returns a frozen
@@ -85,7 +88,7 @@ func newPluginChatFilter(pluginDispatcher *plugins.Dispatcher) dispatcher.Filter
if !msg.Timestamp.IsZero() {
timestamp = msg.Timestamp.UTC().Format(time.RFC3339Nano)
}
- final, allowed, _ := pluginDispatcher.Filter(ctx, plugins.EventChatMessageReceived, pluginChatMessage{ID: msg.ID, User: user, Body: msg.Body, Timestamp: timestamp})
+ final, allowed, _ := pluginDispatcher.Filter(ctx, plugins.EventChatMessageReceived, pluginChatMessage{ID: msg.ID, User: toPluginChatUserPtr(msg.User), ClientID: msg.ClientID, Body: msg.Body, Timestamp: timestamp})
if !allowed {
return false
}
@@ -241,9 +244,12 @@ func chatMessageEvent(evt webhooks.WebhookEvent) []pluginEvent {
// Use RawBody (what the user actually typed), not Body (the HTML-rendered
// form like `<p>!broadcaster</p>`). Plugins doing command matching or
// content analysis want the raw text; the chat client handles rendering.
- msg := pluginChatMessage{ID: data.ID, Body: data.RawBody, Timestamp: formatTimePtr(data.Timestamp)}
- if data.User != nil {
- msg.User = data.User.DisplayName
+ msg := pluginChatMessage{
+ ID: data.ID,
+ User: toPluginChatUserPtr(data.User),
+ ClientID: data.ClientID,
+ Body: data.RawBody,
+ Timestamp: formatTimePtr(data.Timestamp),
}
return []pluginEvent{{plugins.EventChatMessageReceived, msg}}
}
@@ -268,6 +274,16 @@ func translateStreamEvent(evt webhooks.WebhookEvent) []pluginEvent {
return nil
}
+// toPluginChatUserPtr is the pointer form used by payloads where the sender
+// is optional (chat.message.received): nil user in, nil pointer out.
+func toPluginChatUserPtr(u *models.User) *pluginChatUser {
+ if u == nil {
+ return nil
+ }
+ pu := toPluginChatUser(u)
+ return &pu
+}
+
func toPluginChatUser(u *models.User) pluginChatUser {
if u == nil {
return pluginChatUser{}
diff --git a/pluginhost/pluginevents_test.go b/pluginhost/pluginevents_test.go
index cb2cffdf38..958f6763a7 100644
--- a/pluginhost/pluginevents_test.go
+++ b/pluginhost/pluginevents_test.go
@@ -20,7 +20,8 @@ func TestTranslateWebhookEvent_ChatMessageOnlyForUserMessages(t *testing.T) {
Body: "<p>hello</p>",
RawBody: "hello",
Timestamp: &ts,
- User: &models.User{ID: "u1", DisplayName: "alice"},
+ User: &models.User{ID: "u1", DisplayName: "alice", Scopes: []string{"MODERATOR"}, Authenticated: true},
+ ClientID: 42,
},
}
out := translateWebhookEvent(evt)
@@ -34,9 +35,17 @@ func TestTranslateWebhookEvent_ChatMessageOnlyForUserMessages(t *testing.T) {
if !ok {
t.Fatalf("payload type = %T want pluginChatMessage", out[0].payload)
}
- if msg.ID != "m1" || msg.Body != "hello" || msg.User != "alice" {
+ if msg.ID != "m1" || msg.Body != "hello" || msg.ClientID != 42 {
t.Errorf("unexpected message payload: %+v", msg)
}
+ // The full sender identity must ride along so plugins can do reliable
+ // per-user state and scope-gated moderation without a display-name lookup.
+ if msg.User == nil {
+ t.Fatalf("message user identity was dropped")
+ }
+ if msg.User.ID != "u1" || msg.User.DisplayName != "alice" || !msg.User.IsAuthenticated || len(msg.User.Scopes) != 1 {
+ t.Errorf("unexpected user identity: %+v", msg.User)
+ }
if msg.Timestamp != "2026-05-27T12:00:00Z" {
t.Errorf("timestamp = %q", msg.Timestamp)
}