Added event type to chat event

This commit is contained in:
tyler 2024-01-27 15:12:28 -05:00
parent 9e821689cf
commit 20e55ff1d6

12
chat.go
View file

@ -233,6 +233,7 @@ type ChatEvent struct {
} }
type ChatEventDataNoChannels struct { type ChatEventDataNoChannels struct {
// TODO: change [][]string to [][]any and test
Channels [][]string `json:"channels"` Channels [][]string `json:"channels"`
Messages []ChatEventMessage `json:"messages"` Messages []ChatEventMessage `json:"messages"`
Users []ChatEventUser `json:"users"` Users []ChatEventUser `json:"users"`
@ -307,9 +308,11 @@ type ChatView struct {
Badges []string Badges []string
Color string Color string
ImageUrl string ImageUrl string
Init bool
IsFollower bool IsFollower bool
Rant int Rant int
Text string Text string
Type string
Username string Username string
} }
@ -325,18 +328,18 @@ func parseEvent(event []byte) ([]ChatView, error) {
ce.Data.Messages = cenc.Data.Messages ce.Data.Messages = cenc.Data.Messages
ce.Data.Users = cenc.Data.Users ce.Data.Users = cenc.Data.Users
ce.Type = cenc.Type
} }
users := chatUsers(ce.Data.Users) users := chatUsers(ce.Data.Users)
channels := chatChannels(ce.Data.Channels) channels := chatChannels(ce.Data.Channels)
messages, err := parseMessages(ce.Data.Messages, users, channels) messages, err := parseMessages(ce.Type, ce.Data.Messages, users, channels)
if err != nil { if err != nil {
return nil, fmt.Errorf("error parsing messages: %v", err) return nil, fmt.Errorf("error parsing messages: %v", err)
} }
return messages, nil return messages, nil
} }
func chatUsers(users []ChatEventUser) map[string]ChatEventUser { func chatUsers(users []ChatEventUser) map[string]ChatEventUser {
@ -357,7 +360,7 @@ func chatChannels(channels []ChatEventChannel) map[string]ChatEventChannel {
return channelsMap return channelsMap
} }
func parseMessages(messages []ChatEventMessage, users map[string]ChatEventUser, channels map[string]ChatEventChannel) ([]ChatView, error) { func parseMessages(eventType string, messages []ChatEventMessage, users map[string]ChatEventUser, channels map[string]ChatEventChannel) ([]ChatView, error) {
views := []ChatView{} views := []ChatView{}
for _, message := range messages { for _, message := range messages {
var view ChatView var view ChatView
@ -370,10 +373,11 @@ func parseMessages(messages []ChatEventMessage, users map[string]ChatEventUser,
view.Color = user.Color view.Color = user.Color
view.ImageUrl = user.Image1 view.ImageUrl = user.Image1
view.IsFollower = user.IsFollower view.IsFollower = user.IsFollower
view.Text = message.Text
if message.Rant != nil { if message.Rant != nil {
view.Rant = message.Rant.PriceCents view.Rant = message.Rant.PriceCents
} }
view.Text = message.Text
view.Type = eventType
view.Username = user.Username view.Username = user.Username
if message.ChannelID != nil { if message.ChannelID != nil {