From 0fa99f7ab47a61dd1b8e2a8bd63615a2b4341d4f Mon Sep 17 00:00:00 2001 From: Zorglube Date: Wed, 11 Nov 2020 20:51:29 +0100 Subject: [PATCH] Templated the chat messages. --- common/chatdata.go | 106 +++++++++++++++++++++++++++------------------ 1 file changed, 65 insertions(+), 41 deletions(-) diff --git a/common/chatdata.go b/common/chatdata.go index 1fd77c7..bc7a191 100644 --- a/common/chatdata.go +++ b/common/chatdata.go @@ -1,10 +1,12 @@ package common import ( + "bytes" "encoding/json" "errors" "fmt" "strings" + "text/template" ) type DataInterface interface { @@ -89,38 +91,51 @@ type DataMessage struct { Type MessageType } +var ( + cmdme = template.Must(template.New("cmdme").Parse(`{{.From}} {{.Message}}`)) + announcement = template.Must(template.New("announcement").Parse(`{{.Message}}`)) + errormsg = template.Must(template.New("error").Parse(`{{.Message}}`)) + notice = template.Must(template.New("notice").Parse(`{{.Message}}`)) + command = template.Must(template.New("command").Parse(`{{.Message}}`)) + commanderror = template.Must(template.New("commanderror").Parse(`{{.Message}}`)) + cmdlMod = template.Must(template.New("cmdlMod").Parse(`{{.From}}: {{.Message}}`)) + cmdlAdmin = template.Must(template.New("CmdlAdmin").Parse(`{{.From}}: {{.Message}}`)) + defaultMsg = template.Must(template.New("defaultMsg").Parse(`{{.From}}: {{.Message}}`)) +) + // TODO: Read this HTML from a template somewhere func (dc DataMessage) HTML() string { + buf := &bytes.Buffer{} switch dc.Type { case MsgAction: - return `` + dc.From + - ` ` + dc.Message + `` - + cmdme.Execute(buf, dc) + return buf.String() case MsgServer: - return `` + dc.Message + `` - + announcement.Execute(buf, dc) + return buf.String() case MsgError: - return `` + dc.Message + `` - + errormsg.Execute(buf, dc) + return buf.String() case MsgNotice: - return `` + dc.Message + `` - + notice.Execute(buf, dc) + return buf.String() case MsgCommandResponse: - return `` + dc.Message + `` - + command.Execute(buf, dc) + return buf.String() case MsgCommandError: - return `` + dc.Message + `` + commanderror.Execute(buf, dc) + return buf.String() default: - badge := "" switch dc.Level { case CmdlMod: - badge = `` + cmdlMod.Execute(buf, dc) case CmdlAdmin: - badge = `` + cmdlAdmin.Execute(buf, dc) + default: + defaultMsg.Execute(buf, dc) } - return `` + badge + `` + dc.From + - `: ` + dc.Message + `` + return buf.String() } } @@ -165,42 +180,51 @@ type DataEvent struct { Event EventType User string Color string + Users []string } +var ( + evKick = template.Must(template.New("evKick").Parse(`{{.User}} has been kicked.`)) + evLeave = template.Must(template.New("evLeave").Parse(`{{.User}} has left the chat.`)) + evBan = template.Must(template.New("evBan").Parse(`{{.User}} has been banned.`)) + evJoin = template.Must(template.New("evJoin").Parse(`{{.User}} has joined the chat.`)) + evNameChangeWC = template.Must(template.New("evNameChangeWC").Parse(`Somebody changed their name, but IDK who {{.}}.`)) + evNameChange = template.Must(template.New("evNameChange").Parse(`{{index .Users 0}} has changed their name to {{index .Users 1}}.`)) + evNameChangeForced = template.Must(template.New("evNameChangeForced").Parse(`{{index .Users 0}} has had their name changed to {{index .Users 1}} by an admin.`)) + evNameChangeForcedWC = template.Must(template.New("evNameChangeForcedWC").Parse(`An admin changed somebody's name, but IDK who {{.}}.`)) +) + func (de DataEvent) HTML() string { + buf := &bytes.Buffer{} switch de.Event { case EvKick: - return `` + - de.User + ` has been kicked.` + evKick.Execute(buf, de) + return buf.String() case EvLeave: - return `` + - de.User + ` has left the chat.` + evLeave.Execute(buf, de) + return buf.String() case EvBan: - return `` + - de.User + ` has been banned.` + evBan.Execute(buf, de) + return buf.String() case EvJoin: - return `` + - de.User + ` has joined the chat.` + evJoin.Execute(buf, de) + return buf.String() case EvNameChange: - names := strings.Split(de.User, ":") - if len(names) != 2 { - return `Somebody changed their name, but IDK who ` + - ParseEmotes("Jebaited") + `.` + de.Users = strings.Split(de.User, ":") + if len(de.Users) < 2 { + evNameChangeWC.Execute(buf, ParseEmotes("Jebaited")) + } else { + evNameChange.Execute(buf, de) } - - return `` + - names[0] + ` has changed their name to ` + names[1] + `.` + return buf.String() case EvNameChangeForced: - names := strings.Split(de.User, ":") - if len(names) != 2 { - return `An admin changed somebody's name, but IDK who ` + - ParseEmotes("Jebaited") + `.` + de.Users = strings.Split(de.User, ":") + if len(de.Users) < 2 { + evNameChangeForcedWC.Execute(buf, ParseEmotes("Jebaited")) + } else { + evNameChangeForced.Execute(buf, de) } - - return `` + - names[0] + ` has had their name changed to ` + names[1] + ` by an admin.` + return buf.String() } return "" }