Add support for changing user color in name modal. Closes #1805

This commit is contained in:
Gabe Kangas
2022-08-09 19:56:45 -07:00
parent 9187a7a435
commit 68414445c2
22 changed files with 171 additions and 55 deletions

View File

@@ -6,6 +6,7 @@ import (
"strings"
"time"
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/core/chat/events"
"github.com/owncast/owncast/core/data"
"github.com/owncast/owncast/core/user"
@@ -91,6 +92,25 @@ func (s *Server) userNameChanged(eventData chatClientEvent) {
eventData.client.sendConnectedClientInfo()
}
func (s *Server) userColorChanged(eventData chatClientEvent) {
var receivedEvent events.ColorChangeEvent
if err := json.Unmarshal(eventData.data, &receivedEvent); err != nil {
log.Errorln("error unmarshalling to ColorChangeEvent", err)
return
}
// Verify this color is valid
if receivedEvent.NewColor > config.MaxUserColor {
log.Errorln("invalid color requested when changing user display color")
return
}
// Save the new color
if err := user.ChangeUserColor(eventData.client.User.ID, receivedEvent.NewColor); err != nil {
log.Errorln("error changing user display color", err)
}
}
func (s *Server) userMessageSent(eventData chatClientEvent) {
var event events.UserMessageEvent
if err := json.Unmarshal(eventData.data, &event); err != nil {

View File

@@ -10,6 +10,8 @@ const (
UserJoined EventType = "USER_JOINED"
// UserNameChanged is the event sent when a chat username change takes place.
UserNameChanged EventType = "NAME_CHANGE"
// UserColorChanged is the event sent when a chat user color change takes place.
UserColorChanged EventType = "COLOR_CHANGE"
// VisibiltyUpdate is the event sent when a chat message's visibility changes.
VisibiltyUpdate EventType = "VISIBILITY-UPDATE"
// PING is a ping message.

View File

@@ -7,6 +7,13 @@ type NameChangeEvent struct {
NewName string `json:"newName"`
}
// ColorChangeEvent is received when a user changes their chat display color.
type ColorChangeEvent struct {
Event
UserEvent
NewColor int `json:"newColor"`
}
// NameChangeBroadcast represents a user changing their chat display name.
type NameChangeBroadcast struct {
Event

View File

@@ -359,6 +359,8 @@ func (s *Server) eventReceived(event chatClientEvent) {
case events.UserNameChanged:
s.userNameChanged(event)
case events.UserColorChanged:
s.userColorChanged(event)
default:
log.Debugln(eventType, "event not found:", typecheck)
}

View File

@@ -291,7 +291,7 @@ func migrateToSchema1(db *sql.DB) {
// Recreate them as users
for _, token := range oldAccessTokens {
color := utils.GenerateRandomDisplayColor()
color := utils.GenerateRandomDisplayColor(config.MaxUserColor)
if err := insertAPIToken(db, token.accessToken, token.displayName, color, token.scopes); err != nil {
log.Errorln("Error migrating access token", err)
}

View File

@@ -8,6 +8,7 @@ import (
"strings"
"time"
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/core/data"
"github.com/owncast/owncast/db"
"github.com/owncast/owncast/utils"
@@ -70,7 +71,7 @@ func CreateAnonymousUser(displayName string) (*User, string, error) {
}
}
displayColor := utils.GenerateRandomDisplayColor()
displayColor := utils.GenerateRandomDisplayColor(config.MaxUserColor)
user := &User{
ID: id,
@@ -125,6 +126,21 @@ func ChangeUsername(userID string, username string) error {
return nil
}
// ChangeUserColor will change the user associated to userID from one display name to another.
func ChangeUserColor(userID string, color int) error {
_datastore.DbLock.Lock()
defer _datastore.DbLock.Unlock()
if err := _datastore.GetQueries().ChangeDisplayColor(context.Background(), db.ChangeDisplayColorParams{
DisplayColor: int32(color),
ID: userID,
}); err != nil {
return errors.Wrap(err, "unable to change display color")
}
return nil
}
func addAccessTokenForUser(accessToken, userID string) error {
return _datastore.GetQueries().AddAccessTokenForUser(context.Background(), db.AddAccessTokenForUserParams{
Token: accessToken,