Add color cookies

closes #6
This commit is contained in:
joeyak 2019-03-28 20:01:48 -04:00
parent 7a294ea00f
commit 8e4ac4d600
6 changed files with 49 additions and 10 deletions

View File

@ -191,6 +191,11 @@ func (cl *Client) setName(s string) error {
return nil 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 { func (cl *Client) replaceColorizedName(chatData common.ChatData) common.ChatData {
data := chatData.Data.(common.DataMessage) data := chatData.Data.(common.DataMessage)
data.Message = cl.regexName.ReplaceAllString(data.Message, `<span class="mention">$1</span>`) data.Message = cl.regexName.ReplaceAllString(data.Message, `<span class="mention">$1</span>`)

View File

@ -127,7 +127,7 @@ var commands = &CommandControl{
} }
if len(args) == 0 { if len(args) == 0 {
cl.color = common.RandomColor() cl.setColor(common.RandomColor())
return "Random color chosen: " + cl.color return "Random color chosen: " + cl.color
} }
@ -136,7 +136,11 @@ var commands = &CommandControl{
return "To choose a specific color use the format <i>/color #c029ce</i>. Hex values expected." return "To choose a specific color use the format <i>/color #c029ce</i>. 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) common.LogInfof("[color] %s new color: %s\n", cl.name, cl.color)
return "Color changed successfully." return "Color changed successfully."
}, },

View File

@ -47,8 +47,7 @@ var (
) )
// IsValidColor takes a string s and compares it against a list of css color names. // 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 // It also accepts hex codes in the form of #RGB and #RRGGBB
// being the alpha value
func IsValidColor(s string) bool { func IsValidColor(s string) bool {
s = strings.ToLower(s) s = strings.ToLower(s)
for _, c := range colors { for _, c := range colors {

View File

@ -8,6 +8,7 @@ const (
CdUsers // get a list of users CdUsers // get a list of users
CdPing // ping the server to keep the connection alive CdPing // ping the server to keep the connection alive
CdAuth // get the auth levels of the user CdAuth // get the auth levels of the user
CdColor // get the users color
) )
type DataType int type DataType int

View File

@ -1,5 +1,21 @@
/// <reference path="./both.js" /> /// <reference path="./both.js" />
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) { function setPlaying(title, link) {
if (title !== "") { if (title !== "") {
$('#playing').text(title); $('#playing').text(title);
@ -89,6 +105,14 @@ function join() {
} }
setNotifyBox(); setNotifyBox();
openChat(); openChat();
let color = getCookie("color");
if (color !== "") {
// Do a timeout because timings
setTimeout(() => {
sendMessage("/color " + color);
}, 250);
}
} }
function websocketSend(data) { function websocketSend(data) {
@ -169,7 +193,6 @@ function changeColor() {
} }
} }
// Get the websocket setup in a function so it can be recalled // Get the websocket setup in a function so it can be recalled
function setupWebSocket() { function setupWebSocket() {
ws = new WebSocket(getWsUri()); ws = new WebSocket(getWsUri());

View File

@ -10,7 +10,10 @@ import (
"github.com/zorchenhimer/MovieNight/common" "github.com/zorchenhimer/MovieNight/common"
) )
var auth common.CommandLevel var (
auth common.CommandLevel
color string
)
func recieve(v []js.Value) { func recieve(v []js.Value) {
if len(v) == 0 { if len(v) == 0 {
@ -41,6 +44,9 @@ func recieve(v []js.Value) {
} }
case common.CdAuth: case common.CdAuth:
auth = h.Data.(common.CommandLevel) 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: case common.DTEvent:
d := chat.Data.(common.DataEvent) 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{} { func send(this js.Value, v []js.Value) interface{} {
if len(v) != 1 { 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 return false
} }
err := websocketSend(v[0].String(), common.CdMessage) err := websocketSend(v[0].String(), common.CdMessage)
if err != nil { if err != nil {
showSendError(err) showChatError(err)
return false return false
} }
return true return true
} }
func showSendError(err error) { func showChatError(err error) {
if err != nil { if err != nil {
fmt.Printf("Could not send: %v\n", err) fmt.Printf("Could not send: %v\n", err)
js.Call("appendMessages", `<div><span style="color: red;">Could not send message</span></div>`) js.Call("appendMessages", `<div><span style="color: red;">Could not send message</span></div>`)
@ -135,8 +141,9 @@ func isValidName(this js.Value, v []js.Value) interface{} {
} }
func debugValues(v []js.Value) { func debugValues(v []js.Value) {
fmt.Printf("currentName %#v\n", currentName)
fmt.Printf("auth %#v\n", auth) 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("names %#v\n", names)
fmt.Printf("filteredNames %#v\n", filteredNames) fmt.Printf("filteredNames %#v\n", filteredNames)
} }