Skip to content

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

feat: unify plugin ChatUser into User and add displayColor

Gabe Kangasfbd5736a2de5
diff --git a/pluginhost/pluginevents.go b/pluginhost/pluginevents.go
index 0a675117ad..3efd575222 100644
--- a/pluginhost/pluginevents.go
+++ b/pluginhost/pluginevents.go
@@ -12,7 +12,7 @@ import (
)
// Plugin-facing event payload shapes. These mirror the plugin SDK's
-// TypeScript interfaces (ChatMessage, ChatUser, …) so a plugin's typed
+// TypeScript interfaces (ChatMessage, User, …) so a plugin's typed
// handlers receive exactly the documented JSON.
// pluginChatMessage is the chat.message.received (and chat.filter) payload.
@@ -22,27 +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 *plugins.HostChatUser `json:"user,omitempty"`
- ClientID uint `json:"clientId,omitempty"`
- Body string `json:"body"`
- Timestamp string `json:"timestamp"`
+ ID string `json:"id"`
+ User *plugins.HostUser `json:"user,omitempty"`
+ ClientID uint `json:"clientId,omitempty"`
+ Body string `json:"body"`
+ Timestamp string `json:"timestamp"`
}
-// 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.
+// The user wire shape lives once, as plugins.HostUser (the type chat.history()
+// and the users.list()/get() directory also return). 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 user.
type pluginChatUserRename struct {
- User plugins.HostChatUser `json:"user"`
- PreviousName string `json:"previousName"`
+ User plugins.HostUser `json:"user"`
+ PreviousName string `json:"previousName"`
}
type pluginChatMessageModeration struct {
- MessageID string `json:"messageId"`
- Visible bool `json:"visible"`
- Moderator *plugins.HostChatUser `json:"moderator,omitempty"`
+ MessageID string `json:"messageId"`
+ Visible bool `json:"visible"`
+ Moderator *plugins.HostUser `json:"moderator,omitempty"`
}
type pluginStreamLifecycleEvent struct {
@@ -85,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: chatUserPtr(msg.User), ClientID: msg.ClientID, Body: msg.Body, Timestamp: timestamp})
+ final, allowed, _ := pluginDispatcher.Filter(ctx, plugins.EventChatMessageReceived, pluginChatMessage{ID: msg.ID, User: toHostUserPtr(msg.User), ClientID: msg.ClientID, Body: msg.Body, Timestamp: timestamp})
if !allowed {
return false
}
@@ -223,21 +223,21 @@ func translateChatEvent(evt webhooks.WebhookEvent) []pluginEvent {
if !ok {
return nil
}
- return []pluginEvent{{plugins.EventChatUserJoined, chatUser(data.User)}}
+ return []pluginEvent{{plugins.EventChatUserJoined, toHostUser(data.User)}}
case models.UserParted:
data, ok := evt.EventData.(*webhooks.WebhookUserPartEventData)
if !ok {
return nil
}
- return []pluginEvent{{plugins.EventChatUserParted, chatUser(data.User)}}
+ return []pluginEvent{{plugins.EventChatUserParted, toHostUser(data.User)}}
case models.UserNameChanged:
data, ok := evt.EventData.(*webhooks.WebhookNameChangeEventData)
if !ok {
return nil
}
- user := chatUser(data.User)
+ user := toHostUser(data.User)
user.DisplayName = data.NewName
return []pluginEvent{{plugins.EventChatUserRenamed, pluginChatUserRename{
User: user,
@@ -249,11 +249,7 @@ func translateChatEvent(evt webhooks.WebhookEvent) []pluginEvent {
if !ok {
return nil
}
- var moderator *plugins.HostChatUser
- if data.User != nil {
- m := chatUser(data.User)
- moderator = &m
- }
+ moderator := toHostUserPtr(data.User)
// Owncast toggles a set of IDs at once; the SDK payload is
// per-message, so fan one event out per affected message.
out := make([]pluginEvent, 0, len(data.MessageIDs))
@@ -286,7 +282,7 @@ func chatMessageEvent(evt webhooks.WebhookEvent) []pluginEvent {
// content analysis want the raw text; the chat client handles rendering.
msg := pluginChatMessage{
ID: data.ID,
- User: chatUserPtr(data.User),
+ User: toHostUserPtr(data.User),
ClientID: data.ClientID,
Body: data.RawBody,
Timestamp: formatTimePtr(data.Timestamp),
@@ -314,34 +310,6 @@ func translateStreamEvent(evt webhooks.WebhookEvent) []pluginEvent {
return nil
}
-// 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 plugins.HostChatUser{}
- }
- return plugins.HostChatUser{
- ID: u.ID,
- DisplayName: u.DisplayName,
- IsBot: u.IsBot,
- IsAuthenticated: u.Authenticated,
- Scopes: u.Scopes,
- }
-}
-
-// 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 1c3641378d..41b015ba48 100644
--- a/pluginhost/pluginevents_test.go
+++ b/pluginhost/pluginevents_test.go
@@ -83,18 +83,18 @@ func TestTranslateWebhookEvent_UserJoined(t *testing.T) {
evt := webhooks.WebhookEvent{
Type: models.UserJoined,
EventData: &webhooks.WebhookUserJoinedEventData{
- User: &models.User{ID: "u1", DisplayName: "bob", IsBot: true, Scopes: []string{"MODERATOR"}},
+ User: &models.User{ID: "u1", DisplayName: "bob", DisplayColor: 5, IsBot: true, Scopes: []string{"MODERATOR"}},
},
}
out := translateWebhookEvent(evt)
if len(out) != 1 || out[0].eventType != plugins.EventChatUserJoined {
t.Fatalf("unexpected events: %+v", out)
}
- user, ok := out[0].payload.(plugins.HostChatUser)
+ user, ok := out[0].payload.(plugins.HostUser)
if !ok {
- t.Fatalf("payload type = %T want plugins.HostChatUser", out[0].payload)
+ t.Fatalf("payload type = %T want plugins.HostUser", out[0].payload)
}
- if user.ID != "u1" || user.DisplayName != "bob" || !user.IsBot || len(user.Scopes) != 1 {
+ if user.ID != "u1" || user.DisplayName != "bob" || user.DisplayColor != 5 || !user.IsBot || len(user.Scopes) != 1 {
t.Errorf("unexpected user payload: %+v", user)
}
}
diff --git a/pluginhost/pluginhost.go b/pluginhost/pluginhost.go
index f5c6667503..4be458079a 100644
--- a/pluginhost/pluginhost.go
+++ b/pluginhost/pluginhost.go
@@ -1403,9 +1403,7 @@ func wireChatReadHostFns(env *plugins.HostEnv, deps Deps) {
Body: msg.Body,
Timestamp: msg.Timestamp.UTC().Format(time.RFC3339Nano),
}
- if msg.User != nil {
- hm.User = chatUserPtr(msg.User)
- }
+ hm.User = toHostUserPtr(msg.User)
out = append(out, hm)
}
if limit > 0 && len(out) > limit {
@@ -1851,16 +1849,27 @@ func wireRequestHostFns(env *plugins.HostEnv, deps Deps) {
}
}
-// toHostUser maps an Owncast user model onto the plugin-facing HostUser.
+// toHostUser maps an Owncast user model onto the plugin-facing HostUser. It is
+// the single projection of an Owncast user to what a plugin sees: the sender
+// identity on every chat payload (chat.message.received, joins/parts/renames/
+// moderation, chat.history()) and the record returned by Users() / UserGet(),
+// so the exposed surface can't drift between them. A nil user maps to the zero
+// HostUser.
func toHostUser(u *models.User) plugins.HostUser {
+ if u == nil {
+ return plugins.HostUser{}
+ }
hu := plugins.HostUser{
ID: u.ID,
DisplayName: u.DisplayName,
+ DisplayColor: u.DisplayColor,
PreviousNames: u.PreviousNames,
Scopes: u.Scopes,
IsBot: u.IsBot,
IsAuthenticated: u.Authenticated,
- CreatedAt: u.CreatedAt.UTC().Format(time.RFC3339Nano),
+ }
+ if !u.CreatedAt.IsZero() {
+ hu.CreatedAt = u.CreatedAt.UTC().Format(time.RFC3339Nano)
}
if u.DisabledAt != nil {
hu.DisabledAt = u.DisabledAt.UTC().Format(time.RFC3339Nano)
@@ -1868,6 +1877,16 @@ func toHostUser(u *models.User) plugins.HostUser {
return hu
}
+// toHostUserPtr is the pointer form for payloads where the user is optional
+// (chat.message.received, moderation, chat.history()): nil user in, nil out.
+func toHostUserPtr(u *models.User) *plugins.HostUser {
+ if u == nil {
+ return nil
+ }
+ hu := toHostUser(u)
+ return &hu
+}
+
// uploadPluginAsset writes a plugin upload under the public files directory
// and returns the URL it is served at. Names are flattened to their base to
// prevent path traversal. S3-backed storage is a follow-up.
diff --git a/services/plugins/hostfns.go b/services/plugins/hostfns.go
index 679dde65de..e05fd23d04 100644
--- a/services/plugins/hostfns.go
+++ b/services/plugins/hostfns.go
@@ -190,28 +190,16 @@ type FediversePayload struct {
Link string `json:"link,omitempty"`
}
-// 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.
+// onChatMessage / chat.message.received event payload: User is the full User
+// 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 *HostChatUser `json:"user,omitempty"`
- Body string `json:"body"`
- Timestamp string `json:"timestamp"`
+ ID string `json:"id"`
+ User *HostUser `json:"user,omitempty"`
+ Body string `json:"body"`
+ Timestamp string `json:"timestamp"`
}
// BrowserPushPayload is what a plugin asks Owncast to send via the
@@ -222,10 +210,14 @@ type BrowserPushPayload struct {
URL string `json:"url,omitempty"`
}
-// HostUser is the shape returned by Users() / UserGet().
+// HostUser is the single plugin-facing user shape: the sender identity carried
+// by every chat payload (chat.message.received, joins/parts/renames/moderation,
+// chat.history()) and the record returned by Users() / UserGet(). It mirrors
+// the SDK's User TypeScript interface.
type HostUser struct {
ID string `json:"id"`
DisplayName string `json:"displayName"`
+ DisplayColor int `json:"displayColor"`
PreviousNames []string `json:"previousNames,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
DisabledAt string `json:"disabledAt,omitempty"` // ISO-8601 if banned, empty otherwise
diff --git a/services/plugins/plugin-contract.json b/services/plugins/plugin-contract.json
index 113364916e..cbea706ec5 100644
--- a/services/plugins/plugin-contract.json
+++ b/services/plugins/plugin-contract.json
@@ -106,18 +106,12 @@
"body": "string",
"id": "string",
"timestamp": "string",
- "user": "*HostChatUser"
- },
- "HostChatUser": {
- "displayName": "string",
- "id": "string",
- "isAuthenticated": "bool",
- "isBot": "bool",
- "scopes": "[]string"
+ "user": "*HostUser"
},
"HostUser": {
"createdAt": "string",
"disabledAt": "string",
+ "displayColor": "int",
"displayName": "string",
"id": "string",
"isAuthenticated": "bool",