Skip to content

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

fix(plugins): chat.history() returns the full ChatUser object

Gabe Kangas2c19f6c86808
diff --git a/pluginhost/pluginevents.go b/pluginhost/pluginevents.go
index e8249216a8..4ba7b335de 100644
--- a/pluginhost/pluginevents.go
+++ b/pluginhost/pluginevents.go
@@ -22,30 +22,27 @@ import (
// owncast.chat.sendTo / replyTo. User is nil for the rare message with no
// associated account.
type pluginChatMessage struct {
- ID string `json:"id"`
- User *pluginChatUser `json:"user,omitempty"`
- ClientID uint `json:"clientId,omitempty"`
- Body string `json:"body"`
- Timestamp string `json:"timestamp"`
+ ID string `json:"id"`
+ User *plugins.HostChatUser `json:"user,omitempty"`
+ ClientID uint `json:"clientId,omitempty"`
+ Body string `json:"body"`
+ Timestamp string `json:"timestamp"`
}
-type pluginChatUser struct {
- ID string `json:"id"`
- DisplayName string `json:"displayName"`
- IsBot bool `json:"isBot,omitempty"`
- IsAuthenticated bool `json:"isAuthenticated,omitempty"`
- Scopes []string `json:"scopes,omitempty"`
-}
+// The chat-user wire shape lives once, as plugins.HostChatUser (the type
+// chat.history() also returns). These event payloads reuse it directly rather
+// than redeclaring it, so there is a single source of truth for what a plugin
+// sees as a chat user.
type pluginChatUserRename struct {
- User pluginChatUser `json:"user"`
- PreviousName string `json:"previousName"`
+ User plugins.HostChatUser `json:"user"`
+ PreviousName string `json:"previousName"`
}
type pluginChatMessageModeration struct {
- MessageID string `json:"messageId"`
- Visible bool `json:"visible"`
- Moderator *pluginChatUser `json:"moderator,omitempty"`
+ MessageID string `json:"messageId"`
+ Visible bool `json:"visible"`
+ Moderator *plugins.HostChatUser `json:"moderator,omitempty"`
}
type pluginStreamLifecycleEvent struct {
@@ -88,7 +85,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: toPluginChatUserPtr(msg.User), ClientID: msg.ClientID, Body: msg.Body, Timestamp: timestamp})
+ final, allowed, _ := pluginDispatcher.Filter(ctx, plugins.EventChatMessageReceived, pluginChatMessage{ID: msg.ID, User: chatUserPtr(msg.User), ClientID: msg.ClientID, Body: msg.Body, Timestamp: timestamp})
if !allowed {
return false
}
@@ -183,21 +180,21 @@ func translateChatEvent(evt webhooks.WebhookEvent) []pluginEvent {
if !ok {
return nil
}
- return []pluginEvent{{plugins.EventChatUserJoined, toPluginChatUser(data.User)}}
+ return []pluginEvent{{plugins.EventChatUserJoined, chatUser(data.User)}}
case models.UserParted:
data, ok := evt.EventData.(*webhooks.WebhookUserPartEventData)
if !ok {
return nil
}
- return []pluginEvent{{plugins.EventChatUserParted, toPluginChatUser(data.User)}}
+ return []pluginEvent{{plugins.EventChatUserParted, chatUser(data.User)}}
case models.UserNameChanged:
data, ok := evt.EventData.(*webhooks.WebhookNameChangeEventData)
if !ok {
return nil
}
- user := toPluginChatUser(data.User)
+ user := chatUser(data.User)
user.DisplayName = data.NewName
return []pluginEvent{{plugins.EventChatUserRenamed, pluginChatUserRename{
User: user,
@@ -209,9 +206,9 @@ func translateChatEvent(evt webhooks.WebhookEvent) []pluginEvent {
if !ok {
return nil
}
- var moderator *pluginChatUser
+ var moderator *plugins.HostChatUser
if data.User != nil {
- m := toPluginChatUser(data.User)
+ m := chatUser(data.User)
moderator = &m
}
// Owncast toggles a set of IDs at once; the SDK payload is
@@ -246,7 +243,7 @@ func chatMessageEvent(evt webhooks.WebhookEvent) []pluginEvent {
// content analysis want the raw text; the chat client handles rendering.
msg := pluginChatMessage{
ID: data.ID,
- User: toPluginChatUserPtr(data.User),
+ User: chatUserPtr(data.User),
ClientID: data.ClientID,
Body: data.RawBody,
Timestamp: formatTimePtr(data.Timestamp),
@@ -274,21 +271,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 {
+// chatUser projects Owncast's models.User (the source of truth, user.go) onto
+// the curated plugins.HostChatUser wire shape. This is the single place that
+// maps an Owncast user to what a plugin sees — every chat payload (the
+// onChatMessage event, joins/parts/renames/moderation, and chat.history())
+// goes through here, so the exposed surface can't drift between them.
+func chatUser(u *models.User) plugins.HostChatUser {
if u == nil {
- return pluginChatUser{}
+ return plugins.HostChatUser{}
}
- return pluginChatUser{
+ return plugins.HostChatUser{
ID: u.ID,
DisplayName: u.DisplayName,
IsBot: u.IsBot,
@@ -297,6 +289,16 @@ func toPluginChatUser(u *models.User) pluginChatUser {
}
}
+// chatUserPtr is the pointer form for payloads where the sender is optional
+// (chat.message.received, moderation, chat.history()): nil user in, nil out.
+func chatUserPtr(u *models.User) *plugins.HostChatUser {
+ if u == nil {
+ return nil
+ }
+ cu := chatUser(u)
+ return &cu
+}
+
// previousName returns the user's most recent prior display name, the closest
// available value for a rename's "from". Empty when there's no history.
func previousName(u *models.User) string {
diff --git a/pluginhost/pluginevents_test.go b/pluginhost/pluginevents_test.go
index 958f6763a7..1c3641378d 100644
--- a/pluginhost/pluginevents_test.go
+++ b/pluginhost/pluginevents_test.go
@@ -90,9 +90,9 @@ func TestTranslateWebhookEvent_UserJoined(t *testing.T) {
if len(out) != 1 || out[0].eventType != plugins.EventChatUserJoined {
t.Fatalf("unexpected events: %+v", out)
}
- user, ok := out[0].payload.(pluginChatUser)
+ user, ok := out[0].payload.(plugins.HostChatUser)
if !ok {
- t.Fatalf("payload type = %T want pluginChatUser", out[0].payload)
+ t.Fatalf("payload type = %T want plugins.HostChatUser", out[0].payload)
}
if user.ID != "u1" || user.DisplayName != "bob" || !user.IsBot || len(user.Scopes) != 1 {
t.Errorf("unexpected user payload: %+v", user)
diff --git a/pluginhost/pluginhost.go b/pluginhost/pluginhost.go
index acdf0574cc..ccdeb8d60b 100644
--- a/pluginhost/pluginhost.go
+++ b/pluginhost/pluginhost.go
@@ -976,7 +976,7 @@ func wireChatReadHostFns(env *plugins.HostEnv, deps Deps) {
Timestamp: msg.Timestamp.UTC().Format(time.RFC3339Nano),
}
if msg.User != nil {
- hm.User = msg.User.DisplayName
+ hm.User = chatUserPtr(msg.User)
}
out = append(out, hm)
}
diff --git a/services/plugins/hostfns.go b/services/plugins/hostfns.go
index 3bf65d1f03..4523d76322 100644
--- a/services/plugins/hostfns.go
+++ b/services/plugins/hostfns.go
@@ -179,14 +179,28 @@ type FediversePayload struct {
Link string `json:"link,omitempty"`
}
-// HostChatMessage is the shape returned by ChatHistory. Wider than the
-// onChatMessage event payload — production wires this to whatever the chat
+// HostChatUser is the sender identity carried by a chat message. It mirrors
+// the SDK's ChatUser TypeScript interface (and pluginhost.pluginChatUser on
+// the event path) so chat.history() hands a plugin the same nested object
+// shape its onChatMessage handler already receives.
+type HostChatUser struct {
+ ID string `json:"id"`
+ DisplayName string `json:"displayName"`
+ IsBot bool `json:"isBot,omitempty"`
+ IsAuthenticated bool `json:"isAuthenticated,omitempty"`
+ Scopes []string `json:"scopes,omitempty"`
+}
+
+// HostChatMessage is the shape returned by ChatHistory. It matches the
+// onChatMessage / chat.message.received event payload: User is the full
+// ChatUser object (nil for the rare message with no associated account),
+// not a bare display-name string. Production wires this to whatever the chat
// repository hands back; tests construct it directly.
type HostChatMessage struct {
- ID string `json:"id"`
- User string `json:"user"`
- Body string `json:"body"`
- Timestamp string `json:"timestamp"`
+ ID string `json:"id"`
+ User *HostChatUser `json:"user,omitempty"`
+ Body string `json:"body"`
+ Timestamp string `json:"timestamp"`
}
// BrowserPushPayload is what a plugin asks Owncast to send via the
diff --git a/services/plugins/plugin-contract.json b/services/plugins/plugin-contract.json
index 6f1d4472b8..0e2bb8f3cf 100644
--- a/services/plugins/plugin-contract.json
+++ b/services/plugins/plugin-contract.json
@@ -97,7 +97,14 @@
"body": "string",
"id": "string",
"timestamp": "string",
- "user": "string"
+ "user": "*HostChatUser"
+ },
+ "HostChatUser": {
+ "displayName": "string",
+ "id": "string",
+ "isAuthenticated": "bool",
+ "isBot": "bool",
+ "scopes": "[]string"
},
"HostUser": {
"createdAt": "string",