From 8e4ac4d600baf8f34ff4c4325b9f42b7530ad675 Mon Sep 17 00:00:00 2001 From: joeyak Date: Thu, 28 Mar 2019 20:01:48 -0400 Subject: [PATCH] Add color cookies closes #6 --- chatclient.go | 5 +++++ chatcommands.go | 8 ++++++-- common/colors.go | 3 +-- common/constants.go | 1 + static/js/chat.js | 25 ++++++++++++++++++++++++- wasm/main.go | 17 ++++++++++++----- 6 files changed, 49 insertions(+), 10 deletions(-) diff --git a/chatclient.go b/chatclient.go index d2b8145..5c0c0c0 100644 --- a/chatclient.go +++ b/chatclient.go @@ -191,6 +191,11 @@ func (cl *Client) setName(s string) error { return nil } +func (cl *Client) setColor(s string) error { + cl.color = s + return cl.SendChatData(common.NewChatHiddenMessage(common.CdColor, cl.color)) +} + func (cl *Client) replaceColorizedName(chatData common.ChatData) common.ChatData { data := chatData.Data.(common.DataMessage) data.Message = cl.regexName.ReplaceAllString(data.Message, `$1`) diff --git a/chatcommands.go b/chatcommands.go index 48f41e6..6678cec 100644 --- a/chatcommands.go +++ b/chatcommands.go @@ -127,7 +127,7 @@ var commands = &CommandControl{ } if len(args) == 0 { - cl.color = common.RandomColor() + cl.setColor(common.RandomColor()) return "Random color chosen: " + cl.color } @@ -136,7 +136,11 @@ var commands = &CommandControl{ return "To choose a specific color use the format /color #c029ce. Hex values expected." } - cl.color = args[0] + err := cl.setColor(args[0]) + if err != nil { + common.LogErrorf("[color] could not send color update to client: %v\n", err) + } + common.LogInfof("[color] %s new color: %s\n", cl.name, cl.color) return "Color changed successfully." }, diff --git a/common/colors.go b/common/colors.go index 57fb7b1..f2c18e1 100644 --- a/common/colors.go +++ b/common/colors.go @@ -47,8 +47,7 @@ var ( ) // IsValidColor takes a string s and compares it against a list of css color names. -// It also accepts hex codes in the form of #000 (RGB), to #00000000 (RRGGBBAA), with A -// being the alpha value +// It also accepts hex codes in the form of #RGB and #RRGGBB func IsValidColor(s string) bool { s = strings.ToLower(s) for _, c := range colors { diff --git a/common/constants.go b/common/constants.go index e52d4d7..305ee9d 100644 --- a/common/constants.go +++ b/common/constants.go @@ -8,6 +8,7 @@ const ( CdUsers // get a list of users CdPing // ping the server to keep the connection alive CdAuth // get the auth levels of the user + CdColor // get the users color ) type DataType int diff --git a/static/js/chat.js b/static/js/chat.js index 9910cd0..f49fd53 100644 --- a/static/js/chat.js +++ b/static/js/chat.js @@ -1,5 +1,21 @@ /// +function getCookie(cname) { + var name = cname + "="; + var decodedCookie = decodeURIComponent(document.cookie); + var ca = decodedCookie.split(';'); + for (var i = 0; i < ca.length; i++) { + var c = ca[i]; + while (c.charAt(0) == ' ') { + c = c.substring(1); + } + if (c.indexOf(name) == 0) { + return c.substring(name.length, c.length); + } + } + return ""; +} + function setPlaying(title, link) { if (title !== "") { $('#playing').text(title); @@ -89,6 +105,14 @@ function join() { } setNotifyBox(); openChat(); + + let color = getCookie("color"); + if (color !== "") { + // Do a timeout because timings + setTimeout(() => { + sendMessage("/color " + color); + }, 250); + } } function websocketSend(data) { @@ -169,7 +193,6 @@ function changeColor() { } } - // Get the websocket setup in a function so it can be recalled function setupWebSocket() { ws = new WebSocket(getWsUri()); diff --git a/wasm/main.go b/wasm/main.go index dab4572..23f0ce2 100644 --- a/wasm/main.go +++ b/wasm/main.go @@ -10,7 +10,10 @@ import ( "github.com/zorchenhimer/MovieNight/common" ) -var auth common.CommandLevel +var ( + auth common.CommandLevel + color string +) func recieve(v []js.Value) { if len(v) == 0 { @@ -41,6 +44,9 @@ func recieve(v []js.Value) { } case common.CdAuth: auth = h.Data.(common.CommandLevel) + case common.CdColor: + color = h.Data.(string) + js.Get("document").Set("cookie", fmt.Sprintf("color=%s;", color)) } case common.DTEvent: d := chat.Data.(common.DataEvent) @@ -101,19 +107,19 @@ func websocketSend(msg string, dataType common.ClientDataType) error { func send(this js.Value, v []js.Value) interface{} { if len(v) != 1 { - showSendError(fmt.Errorf("expected 1 parameter, got %d", len(v))) + showChatError(fmt.Errorf("expected 1 parameter, got %d", len(v))) return false } err := websocketSend(v[0].String(), common.CdMessage) if err != nil { - showSendError(err) + showChatError(err) return false } return true } -func showSendError(err error) { +func showChatError(err error) { if err != nil { fmt.Printf("Could not send: %v\n", err) js.Call("appendMessages", `
Could not send message
`) @@ -135,8 +141,9 @@ func isValidName(this js.Value, v []js.Value) interface{} { } func debugValues(v []js.Value) { - fmt.Printf("currentName %#v\n", currentName) fmt.Printf("auth %#v\n", auth) + fmt.Printf("color %#v\n", color) + fmt.Printf("currentName %#v\n", currentName) fmt.Printf("names %#v\n", names) fmt.Printf("filteredNames %#v\n", filteredNames) }