feat: raising hand endpoint

This commit is contained in:
efim
2023-11-12 08:31:42 +00:00
parent 16df084928
commit 34d610a8c8
5 changed files with 122 additions and 7 deletions

View File

@@ -1,12 +1,14 @@
package routes
import (
"context"
"embed"
"fmt"
"html/template"
"log"
"math/rand"
"net/http"
"strconv"
"time"
"sunshine.industries/some-automoderation/rooms"
@@ -14,6 +16,7 @@ import (
)
const roomPath = "/room/"
const raiseHandPath = "/rooms/raise/"
// registering all routes for page and logic of /room/:roomName
func registerPageRoutes(
@@ -23,11 +26,15 @@ func registerPageRoutes(
) {
http.HandleFunc("/rooms/random", streamingBsRoute())
http.Handle(roomPath,
http.Handle(roomPath, // ending in / captures all following path sections, i.e room name
authedPageMiddleware(
sessionSM,
http.StripPrefix(roomPath, roomPageRoute(templateFs, roomsM))))
http.Handle(raiseHandPath, // ending in / captures all following path sections, i.e gesture num
authedPageMiddleware(
sessionSM,
http.StripPrefix(raiseHandPath, raiseGestureHandRoute(templateFs, roomsM))))
}
func streamingBsRoute() http.HandlerFunc {
@@ -57,6 +64,44 @@ func streamingBsRoute() http.HandlerFunc {
}
}
// if currently speaking? i guess first lower the hand and then raise new
func raiseGestureHandRoute(
templateFs *embed.FS,
roomsM rooms.RoomManager,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
gestureInd, err := strconv.Atoi(r.URL.Path)
gesture, found := rooms.GestureFromInt(gestureInd)
if err != nil || !found {
log.Printf("/rooms/raiseGesture error %s gettin hand symbol index from path %s\n", err, r.URL.Path)
return
}
log.Printf("/rooms/raiseGesture successfully got gesture %d : %s", gesture, gesture.String())
session, found := getContextSession(r.Context())
if !found {
log.Printf("/rooms/raiseGesture session not found, should be impossible")
// TODO return error i guess
return
}
var outerClosureRoom rooms.Room
err = roomsM.Update(context.TODO(), session.RoomId, func(fromRoom rooms.Room) (toRoom rooms.Room) {
toRoom = fromRoom.RaiseHand(session.PersonId, gesture)
outerClosureRoom = toRoom
return toRoom
})
if err != nil {
log.Printf("/rooms/raiseGesture error saving hand: %s\n", err)
return
// TODO return error i guess
}
log.Printf(">> i attempt to get room with closure: %+v\n", outerClosureRoom)
w.WriteHeader(http.StatusNoContent)
// then htmx style i'll need to re-render the room, i suppose
// oh, not really, the SSE should send the updated room state
}
}
func roomPageRoute(
templateFs *embed.FS,
roomsM rooms.RoomManager,
@@ -92,10 +137,24 @@ func roomPageRoute(
templFile := "templates/room.gohtml"
tmpl := template.Must(template.ParseFS(templateFs, templFile))
type GestureData struct {
Name string
Url string
}
var gesturesData []GestureData
for gesture := rooms.ChangeTopic; gesture <= rooms.Meta; gesture++ {
gesturesData = append(gesturesData, GestureData{
Name: gesture.String(),
Url: fmt.Sprintf("%s%d", raiseHandPath, gesture),
})
}
pageData := struct {
RoomName string
Gestures []GestureData
}{
RoomName: roomName,
Gestures: gesturesData,
}
err := tmpl.Execute(w, pageData)