Skip to content

Commit

Permalink
reorganize some code & add more emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
CubicrootXYZ committed May 27, 2024
1 parent 0bc8c4d commit 262bbf3
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 172 deletions.
140 changes: 140 additions & 0 deletions internal/shiftnotifier/formatting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package shiftnotifier

import (
"sort"
"strconv"
"strings"
"time"
)

func (service *service) diffToMessage(diffs *shiftDiffs) (string, string) {
msg := strings.Builder{}
msgHTML := strings.Builder{}

defaultTZ, err := time.LoadLocation("Europe/Berlin")
if err != nil {
defaultTZ = time.Local
}

timeStr := diffs.ReferenceTime.
Add(service.config.NotifyBeforeShiftStart).
In(defaultTZ).
Format("Mon, 15:04")

// Title.
msg.WriteString("TROLL CHANGES FOR ")
msg.WriteString(timeStr)
msg.WriteString("\n\n")

msgHTML.WriteString("<h1>Troll Changes for ")
msgHTML.WriteString(timeStr)
msgHTML.WriteString("</h1><br>\n")

// Sort by mapkey to have deterministic order.
locations := make([]string, 0, len(diffs.DiffsInLocations))
for k := range diffs.DiffsInLocations {
locations = append(locations, k)
}
sort.Strings(locations)

for _, loc := range locations {
// Location.
msg.WriteString("πŸ“ ")
msg.WriteString(loc)
msg.WriteString("\n")

msgHTML.WriteString("πŸ“ <b>")
msgHTML.WriteString(loc)
msgHTML.WriteString("</b><br>\n")

// Troll lists.
msg.WriteString("Arriving Trolls πŸ”œ:\n")
msgHTML.WriteString("Arriving Trolls πŸ”œ:<br>\n")
usersToList(diffs.DiffsInLocations[loc].UsersArriving, &msg, &msgHTML)

msg.WriteString("Staying Trolls πŸ”„:\n")
msgHTML.WriteString("Staying Trolls πŸ”„:<br>\n")
usersToList(diffs.DiffsInLocations[loc].UsersWorking, &msg, &msgHTML)

msg.WriteString("Leaving Trolls πŸ”š:\n")
msgHTML.WriteString("Leaving Trolls πŸ”š:<br>\n")
usersToList(diffs.DiffsInLocations[loc].UsersLeaving, &msg, &msgHTML)

// Summary.
msg.WriteString("\nExpecting ")
msg.WriteString(strconv.Itoa(int(diffs.DiffsInLocations[loc].ExpectedUsers)))
msg.WriteString(" trolls total\n")
msgHTML.WriteString("<br>\nExpecting ")
msgHTML.WriteString(strconv.Itoa(int(diffs.DiffsInLocations[loc].ExpectedUsers)))
msgHTML.WriteString(" trolls total<br>\n")

if len(diffs.DiffsInLocations[loc].OpenUsers) > 0 {
msg.WriteString("🚨 Open positions:\n")
for shiftType, amount := range diffs.DiffsInLocations[loc].OpenUsers {
msg.WriteString("- ")
msg.WriteString(strconv.Itoa(int(amount)))
msg.WriteString("x ")
msg.WriteString(shiftType)
msg.WriteString("\n")
}

msgHTML.WriteString("🚨 Open positions:<br>\n")
for shiftType, amount := range diffs.DiffsInLocations[loc].OpenUsers {
msgHTML.WriteString("- ")
msgHTML.WriteString(strconv.Itoa(int(amount)))
msgHTML.WriteString("x ")
msgHTML.WriteString(shiftType)
msgHTML.WriteString("<br>\n")
}
}

msg.WriteString("\n")
msgHTML.WriteString("<br>\n")
}

return msg.String(), msgHTML.String()
}

func usersToList(users []shiftUser, msg *strings.Builder, msgHTML *strings.Builder) {
if len(users) == 0 {
msg.WriteString(" _none_\n")
msgHTML.WriteString("&nbsp;&nbsp;<i>none</i><br>\n")
return
}

for _, user := range users {
msg.WriteString(" - ")
msg.WriteString(user.Nickname)
msg.WriteString(" (")
msg.WriteString(user.ShiftName)
msg.WriteString(shiftNameToEmoji(user.ShiftName))
msg.WriteString(")\n")

msgHTML.WriteString("&nbsp;&nbsp;- ")
msgHTML.WriteString(user.Nickname)
msgHTML.WriteString(" <i>(")
msgHTML.WriteString(user.ShiftName)
msgHTML.WriteString(shiftNameToEmoji(user.ShiftName))
msgHTML.WriteString(")</i><br>\n")
}
}

func shiftNameToEmoji(shiftName string) string {
shiftName = strings.TrimSpace(strings.ToLower(shiftName))
switch {
case strings.Contains(shiftName, "orga"):
return " πŸ‘‘"
case strings.Contains(shiftName, "tschunk"):
return " πŸ₯ƒ"
case strings.Contains(shiftName, "kaffee"):
return " 🍫"
case strings.Contains(shiftName, "runner"):
return " πŸƒβ€β™€οΈ"
case strings.Contains(shiftName, "bottle"):
return " ♻️"
case strings.Contains(shiftName, "bar-theke"):
return " πŸ’Ά"
default:
return ""
}
}
172 changes: 0 additions & 172 deletions internal/shiftnotifier/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ package shiftnotifier

import (
"context"
"encoding/json"
"errors"
"log/slog"
"net/http"
"os"
"sort"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -64,56 +60,6 @@ func New(config *Config, angelAPI angelapi.Service, messenger matrixmessenger.Me
return s
}

func (service *service) serveJSONData(w http.ResponseWriter, r *http.Request) {
err := service.requireToken(r)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte("unauthorized"))
return
}

data, err := json.Marshal(service.latestDiffs)
if err != nil {
slog.Error("failed marshaling data", "error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("internal server error"))
return
}
_, _ = w.Write(data)
}

func (service *service) serveHumanData(w http.ResponseWriter, r *http.Request) {
err := service.requireToken(r)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte("unauthorized"))
return
}

if service.latestDiffs == nil {
_, _ = w.Write([]byte("no data"))
return
}

_, html := service.diffToMessage(service.latestDiffs)

if refreshSeconds := r.URL.Query().Get("refresh_seconds"); refreshSeconds != "" {
html = `<meta http-equiv="refresh" content="` + refreshSeconds + `">` + html
}

html = `<html>
<style>
body {
background:#111;
color:darkgrey;
font-family: sans-serif;
}
</style>
` + html + "</html>"

_, _ = w.Write([]byte(html))
}

func (service *service) Start() error {
go func(config Config) {
err := http.ListenAndServe(config.ListenAddr, nil)
Expand Down Expand Up @@ -392,121 +338,3 @@ func (service *service) cleanUpDiffs(diffs map[string]shiftDiff) map[string]shif

return newDiffs
}

func (service *service) diffToMessage(diffs *shiftDiffs) (string, string) {
msg := strings.Builder{}
msgHTML := strings.Builder{}

defaultTZ, err := time.LoadLocation("Europe/Berlin")
if err != nil {
defaultTZ = time.Local
}

timeStr := diffs.ReferenceTime.
Add(service.config.NotifyBeforeShiftStart).
In(defaultTZ).
Format("Mon, 15:04")

// Title.
msg.WriteString("TROLL CHANGES FOR ")
msg.WriteString(timeStr)
msg.WriteString("\n\n")

msgHTML.WriteString("<h1>Troll Changes for ")
msgHTML.WriteString(timeStr)
msgHTML.WriteString("</h1><br>\n")

// Sort by mapkey to have deterministic order.
locations := make([]string, 0, len(diffs.DiffsInLocations))
for k := range diffs.DiffsInLocations {
locations = append(locations, k)
}
sort.Strings(locations)

for _, loc := range locations {
// Location.
msg.WriteString("πŸ“ ")
msg.WriteString(loc)
msg.WriteString("\n")

msgHTML.WriteString("πŸ“ <b>")
msgHTML.WriteString(loc)
msgHTML.WriteString("</b><br>\n")

// Troll lists.
msg.WriteString("Arriving Trolls πŸ”œ:\n")
msgHTML.WriteString("Arriving Trolls πŸ”œ:<br>\n")
usersToList(diffs.DiffsInLocations[loc].UsersArriving, &msg, &msgHTML)

msg.WriteString("Staying Trolls πŸ”„:\n")
msgHTML.WriteString("Staying Trolls πŸ”„:<br>\n")
usersToList(diffs.DiffsInLocations[loc].UsersWorking, &msg, &msgHTML)

msg.WriteString("Leaving Trolls πŸ”š:\n")
msgHTML.WriteString("Leaving Trolls πŸ”š:<br>\n")
usersToList(diffs.DiffsInLocations[loc].UsersLeaving, &msg, &msgHTML)

// Summary.
msg.WriteString("\nExpecting ")
msg.WriteString(strconv.Itoa(int(diffs.DiffsInLocations[loc].ExpectedUsers)))
msg.WriteString(" trolls total\n")
msgHTML.WriteString("<br>\nExpecting ")
msgHTML.WriteString(strconv.Itoa(int(diffs.DiffsInLocations[loc].ExpectedUsers)))
msgHTML.WriteString(" trolls total<br>\n")

if len(diffs.DiffsInLocations[loc].OpenUsers) > 0 {
msg.WriteString("🚨 Open positions:\n")
for shiftType, amount := range diffs.DiffsInLocations[loc].OpenUsers {
msg.WriteString("- ")
msg.WriteString(strconv.Itoa(int(amount)))
msg.WriteString("x ")
msg.WriteString(shiftType)
msg.WriteString("\n")
}

msgHTML.WriteString("🚨 Open positions:<br>\n")
for shiftType, amount := range diffs.DiffsInLocations[loc].OpenUsers {
msgHTML.WriteString("- ")
msgHTML.WriteString(strconv.Itoa(int(amount)))
msgHTML.WriteString("x ")
msgHTML.WriteString(shiftType)
msgHTML.WriteString("<br>\n")
}
}

msg.WriteString("\n")
msgHTML.WriteString("<br>\n")
}

return msg.String(), msgHTML.String()
}

func usersToList(users []shiftUser, msg *strings.Builder, msgHTML *strings.Builder) {
if len(users) == 0 {
msg.WriteString(" _none_\n")
msgHTML.WriteString("&nbsp;&nbsp;<i>none</i><br>\n")
return
}

for _, user := range users {
msg.WriteString(" - ")
msg.WriteString(user.Nickname)
msg.WriteString(" (")
msg.WriteString(user.ShiftName)
msg.WriteString(")\n")

msgHTML.WriteString("&nbsp;&nbsp;- ")
msgHTML.WriteString(user.Nickname)
msgHTML.WriteString(" <i>(")
msgHTML.WriteString(user.ShiftName)
msgHTML.WriteString(")</i><br>\n")
}
}

func (service *service) requireToken(r *http.Request) error {
t := r.URL.Query().Get("token")
if strings.TrimSpace(t) != service.config.Token {
return errors.New("invalid auth")
}
return nil
}
Loading

0 comments on commit 262bbf3

Please sign in to comment.