feat: raising hand endpoint
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -598,11 +598,20 @@ video {
|
||||
border-width: 1px;
|
||||
}
|
||||
|
||||
.border-2 {
|
||||
border-width: 2px;
|
||||
}
|
||||
|
||||
.border-black {
|
||||
--tw-border-opacity: 1;
|
||||
border-color: rgb(0 0 0 / var(--tw-border-opacity));
|
||||
}
|
||||
|
||||
.border-blue-700 {
|
||||
--tw-border-opacity: 1;
|
||||
border-color: rgb(29 78 216 / var(--tw-border-opacity));
|
||||
}
|
||||
|
||||
.bg-amber-400 {
|
||||
--tw-bg-opacity: 1;
|
||||
background-color: rgb(251 191 36 / var(--tw-bg-opacity));
|
||||
@@ -627,6 +636,11 @@ video {
|
||||
background-color: rgb(194 65 12 / 0.25);
|
||||
}
|
||||
|
||||
.bg-white {
|
||||
--tw-bg-opacity: 1;
|
||||
background-color: rgb(255 255 255 / var(--tw-bg-opacity));
|
||||
}
|
||||
|
||||
.p-4 {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
@@ -31,12 +31,12 @@
|
||||
<![endif]-->
|
||||
<div class="h-full w-full grid">
|
||||
<script src="https://unpkg.com/htmx.org/dist/ext/sse.js"></script>
|
||||
<div id="state-receival" class="bg-blue-300 hidden md:block"
|
||||
hx-ext="sse" sse-connect="/rooms/random"
|
||||
>
|
||||
<div> yoyo </div>
|
||||
<div sse-swap="message"> qweopop </div>
|
||||
</div>
|
||||
<!-- <div id="state-receival" class="bg-blue-300 hidden md:block" -->
|
||||
<!-- hx-ext="sse" sse-connect="/rooms/random" -->
|
||||
<!-- > -->
|
||||
<!-- <div> yoyo </div> -->
|
||||
<!-- <div sse-swap="message"> qweopop </div> -->
|
||||
<!-- </div> -->
|
||||
<!-- should be done with custom event for screen change -->
|
||||
<!-- <div id="state-receival" class="bg-blue-100 md:hidden" -->
|
||||
<!-- hx-ext="sse" sse-connect="/rooms/random?mobile=true" -->
|
||||
@@ -46,6 +46,14 @@
|
||||
<!-- </div> -->
|
||||
<div id="controls" class="bg-green-300">
|
||||
koko
|
||||
{{ range .Gestures }}
|
||||
<button
|
||||
hx-get="{{ .Url }}"
|
||||
class="bg-white rounded border-blue-700 border-2"
|
||||
>
|
||||
{{ .Name }}
|
||||
</button>
|
||||
{{ end }}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user