Templated the chat messages.

This commit is contained in:
Zorglube 2020-11-11 20:51:29 +01:00
parent e26ae33758
commit 0fa99f7ab4

View File

@ -1,10 +1,12 @@
package common package common
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
"text/template"
) )
type DataInterface interface { type DataInterface interface {
@ -89,38 +91,51 @@ type DataMessage struct {
Type MessageType Type MessageType
} }
var (
cmdme = template.Must(template.New("cmdme").Parse(`<span style="color:{{.Color}}"><span class="name">{{.From}}</span> <span class="cmdme">{{.Message}}</span></span>`))
announcement = template.Must(template.New("announcement").Parse(`<span class="announcement">{{.Message}}</span>`))
errormsg = template.Must(template.New("error").Parse(`<span class="error">{{.Message}}</span>`))
notice = template.Must(template.New("notice").Parse(`<span class="notice">{{.Message}}</span>`))
command = template.Must(template.New("command").Parse(`<span class="command">{{.Message}}</span>`))
commanderror = template.Must(template.New("commanderror").Parse(`<span class="commanderror">{{.Message}}</span>`))
cmdlMod = template.Must(template.New("cmdlMod").Parse(`<span><img src="/static/img/mod.png" class="badge" /><span class="name" style="color:{{.Color}}">{{.From}}</span><b>:</b> <span class="msg">{{.Message}}</span></span>`))
cmdlAdmin = template.Must(template.New("CmdlAdmin").Parse(`<span><img src="/static/img/admin.png" class="badge" /><span class="name" style="color:{{.Color}}">{{.From}}</span><b>:</b> <span class="msg">{{.Message}}</span></span>`))
defaultMsg = template.Must(template.New("defaultMsg").Parse(`<span><span class="name" style="color:{{.Color}}">{{.From}}</span><b>:</b> <span class="msg">{{.Message}}</span></span>`))
)
// TODO: Read this HTML from a template somewhere // TODO: Read this HTML from a template somewhere
func (dc DataMessage) HTML() string { func (dc DataMessage) HTML() string {
buf := &bytes.Buffer{}
switch dc.Type { switch dc.Type {
case MsgAction: case MsgAction:
return `<span style="color:` + dc.Color + `"><span class="name">` + dc.From + cmdme.Execute(buf, dc)
`</span> <span class="cmdme">` + dc.Message + `</span></span>` return buf.String()
case MsgServer: case MsgServer:
return `<span class="announcement">` + dc.Message + `</span>` announcement.Execute(buf, dc)
return buf.String()
case MsgError: case MsgError:
return `<span class="error">` + dc.Message + `</span>` errormsg.Execute(buf, dc)
return buf.String()
case MsgNotice: case MsgNotice:
return `<span class="notice">` + dc.Message + `</span>` notice.Execute(buf, dc)
return buf.String()
case MsgCommandResponse: case MsgCommandResponse:
return `<span class="command">` + dc.Message + `</span>` command.Execute(buf, dc)
return buf.String()
case MsgCommandError: case MsgCommandError:
return `<span class="commanderror">` + dc.Message + `</span>` commanderror.Execute(buf, dc)
return buf.String()
default: default:
badge := ""
switch dc.Level { switch dc.Level {
case CmdlMod: case CmdlMod:
badge = `<img src="/static/img/mod.png" class="badge" />` cmdlMod.Execute(buf, dc)
case CmdlAdmin: case CmdlAdmin:
badge = `<img src="/static/img/admin.png" class="badge" />` cmdlAdmin.Execute(buf, dc)
default:
defaultMsg.Execute(buf, dc)
} }
return `<span>` + badge + `<span class="name" style="color:` + dc.Color + `">` + dc.From + return buf.String()
`</span><b>:</b> <span class="msg">` + dc.Message + `</span></span>`
} }
} }
@ -165,42 +180,51 @@ type DataEvent struct {
Event EventType Event EventType
User string User string
Color string Color string
Users []string
} }
var (
evKick = template.Must(template.New("evKick").Parse(`<span class="event"><span class="name" style="color:{{.Color}}">{{.User}}</span> has been kicked.</span>`))
evLeave = template.Must(template.New("evLeave").Parse(`<span class="event"><span class="name" style="color:{{.Color}}">{{.User}}</span> has left the chat.</span>`))
evBan = template.Must(template.New("evBan").Parse(`<span class="event"><span class="name" style="color:{{.Color}}">{{.User}}</span> has been banned.</span>`))
evJoin = template.Must(template.New("evJoin").Parse(`<span class="event"><span class="name" style="color:{{.Color}}">{{.User}}</span> has joined the chat.</span>`))
evNameChangeWC = template.Must(template.New("evNameChangeWC").Parse(`<span class="event">Somebody changed their name, but IDK who {{.}}.</span>`))
evNameChange = template.Must(template.New("evNameChange").Parse(`<span class="event"><span class="name" style="color:{{.Color}}">{{index .Users 0}}</span> has changed their name to <span class="name" style="color:{{.Color}}">{{index .Users 1}}</span>.</span>`))
evNameChangeForced = template.Must(template.New("evNameChangeForced").Parse(`<span class="event"><span class="name" style="color:{{.Color}}">{{index .Users 0}}</span> has had their name changed to <span class="name" style="color:{{.Color}}">{{index .Users 1}}</span> by an admin.</span>`))
evNameChangeForcedWC = template.Must(template.New("evNameChangeForcedWC").Parse(`<span class="event">An admin changed somebody's name, but IDK who {{.}}.</span>`))
)
func (de DataEvent) HTML() string { func (de DataEvent) HTML() string {
buf := &bytes.Buffer{}
switch de.Event { switch de.Event {
case EvKick: case EvKick:
return `<span class="event"><span class="name" style="color:` + de.Color + `">` + evKick.Execute(buf, de)
de.User + `</span> has been kicked.</span>` return buf.String()
case EvLeave: case EvLeave:
return `<span class="event"><span class="name" style="color:` + de.Color + `">` + evLeave.Execute(buf, de)
de.User + `</span> has left the chat.</span>` return buf.String()
case EvBan: case EvBan:
return `<span class="event"><span class="name" style="color:` + de.Color + `">` + evBan.Execute(buf, de)
de.User + `</span> has been banned.</span>` return buf.String()
case EvJoin: case EvJoin:
return `<span class="event"><span class="name" style="color:` + de.Color + `">` + evJoin.Execute(buf, de)
de.User + `</span> has joined the chat.</span>` return buf.String()
case EvNameChange: case EvNameChange:
names := strings.Split(de.User, ":") de.Users = strings.Split(de.User, ":")
if len(names) != 2 { if len(de.Users) < 2 {
return `<span class="event">Somebody changed their name, but IDK who ` + evNameChangeWC.Execute(buf, ParseEmotes("Jebaited"))
ParseEmotes("Jebaited") + `.</span>` } else {
evNameChange.Execute(buf, de)
} }
return buf.String()
return `<span class="event"><span class="name" style="color:` + de.Color + `">` +
names[0] + `</span> has changed their name to <span class="name" style="color:` +
de.Color + `">` + names[1] + `</span>.</span>`
case EvNameChangeForced: case EvNameChangeForced:
names := strings.Split(de.User, ":") de.Users = strings.Split(de.User, ":")
if len(names) != 2 { if len(de.Users) < 2 {
return `<span class="event">An admin changed somebody's name, but IDK who ` + evNameChangeForcedWC.Execute(buf, ParseEmotes("Jebaited"))
ParseEmotes("Jebaited") + `.</span>` } else {
evNameChangeForced.Execute(buf, de)
} }
return buf.String()
return `<span class="event"><span class="name" style="color:` + de.Color + `">` +
names[0] + `</span> has had their name changed to <span class="name" style="color:` +
de.Color + `">` + names[1] + `</span> by an admin.</span>`
} }
return "" return ""
} }