feat: impl release hand & next speaker selection

This commit is contained in:
efim
2023-11-09 06:20:43 +00:00
parent aa048efbd3
commit 480d007e6c
5 changed files with 199 additions and 48 deletions

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"html/template"
"log"
"math/rand"
"net/http"
"strconv"
@@ -31,6 +30,7 @@ func registerLoginRoutes(
const authCookieName = "auth"
const loginPath = "/login"
type contextKey string
func getContextSession(ctx context.Context) (sessions.SessionData, bool) {
@@ -49,14 +49,14 @@ func authedPageMiddleware(
sessionsM sessions.SessionManagement, next http.Handler,
) http.Handler {
returnNoAccess := func(w http.ResponseWriter, r *http.Request) {
log.Printf("auth middle > restricting access to %s", r.URL.Path)
w.Header().Add("HX-Replace-Url", loginPath)
renderLoginPage(w)
log.Printf("auth middle > restricting access to %s", r.URL.Path)
w.Header().Add("HX-Replace-Url", loginPath)
renderLoginPage(w)
}
rerturnSuccess := func(w http.ResponseWriter, r *http.Request, session sessions.SessionData) {
ctx := context.WithValue(r.Context(), contextKey("session"), session)
log.Printf("auth middle > allowing access to %s for %+v", r.URL.Path, session)
next.ServeHTTP(w, r.WithContext(ctx))
ctx := context.WithValue(r.Context(), contextKey("session"), session)
log.Printf("auth middle > allowing access to %s for %+v", r.URL.Path, session)
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sessionCookie, err := r.Cookie(authCookieName)
@@ -90,7 +90,7 @@ func renderLoginPage(w http.ResponseWriter) {
}
}
func createRoomHandler( templateFs *embed.FS,
func createRoomHandler(templateFs *embed.FS,
sessionSM sessions.SessionManagement,
roomsM rooms.RoomManager,
) http.HandlerFunc {
@@ -108,32 +108,32 @@ func createRoomHandler( templateFs *embed.FS,
return
}
person := rooms.Person{
PersonId: rand.Int(),
Name: r.PostFormValue("personalName"),
Id: rooms.RandomPersonId(),
Name: r.PostFormValue("personalName"),
PasswordHash: r.PostFormValue("personalPassword"), // TODO hash the password, not to store
}
newRoom := rooms.Room{
Name: roomName,
Name: roomName,
PasswordHash: r.PostFormValue("roomPassword"), // TODO hash the password, not to store
AdminIds: []int{person.PersonId},
Paricipants: []rooms.Person{person},
AdminIds: []rooms.PersonId{person.Id},
Paricipants: []rooms.Person{person},
}
err = roomsM.Save(newRoom)
if err != nil {
log.Printf("what am i to do? error saving room %s", err)
// todo return error notice somehow
}
newSessionId, err := sessionSM.Save(newRoom.Name, person.PersonId)
newSessionId, err := sessionSM.Save(newRoom.Name, person.Id)
if err != nil {
log.Printf("what am i to do? error saving session %s", err)
// todo return error notice somehow
}
http.SetCookie(w, &http.Cookie{
Name: authCookieName,
Value: fmt.Sprint(newSessionId),
Secure: true,
Name: authCookieName,
Value: fmt.Sprint(newSessionId),
Secure: true,
HttpOnly: true,
Path: "/",
Path: "/",
})
var templFile = "templates/index.gohtml"
tmpl := template.Must(template.ParseFS(templateFs, templFile))
@@ -148,7 +148,7 @@ func createRoomHandler( templateFs *embed.FS,
// checking whether the room name already exists
// toggle button between Join or Create
func checkRoomName( templateFs *embed.FS,
func checkRoomName(templateFs *embed.FS,
roomsM rooms.RoomManager,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
@@ -167,7 +167,7 @@ func checkRoomName( templateFs *embed.FS,
}
}
func joinRoomHandler( templateFs *embed.FS,
func joinRoomHandler(templateFs *embed.FS,
sessionSM sessions.SessionManagement,
roomsM rooms.RoomManager,
) http.HandlerFunc {
@@ -221,9 +221,9 @@ func joinRoomHandler( templateFs *embed.FS,
log.Printf("/login/join room pass correct, new person joins")
// creating a new person with provided password hash
person = rooms.Person{
Name: personName,
Name: personName,
PasswordHash: personPass,
PersonId: rand.Int(),
Id: rooms.RandomPersonId(),
}
err := roomsM.Update(context.TODO(), room.Name, func(fromRoom rooms.Room) (toRoom rooms.Room) {
toRoom = fromRoom
@@ -234,7 +234,7 @@ func joinRoomHandler( templateFs *embed.FS,
log.Printf("/login/join problem adding person to room", person.Name)
w.WriteHeader(http.StatusInternalServerError)
// TODO render error to be put in error place
// with message try again
// with message try again
return
}
}
@@ -242,16 +242,16 @@ func joinRoomHandler( templateFs *embed.FS,
// now we have room and person, can create a session
// and we've checked password
newSessionId, err := sessionSM.Save(room.Name, person.PersonId)
newSessionId, err := sessionSM.Save(room.Name, person.Id)
if err != nil {
log.Printf("/login/submit > error saving session %s", err)
}
http.SetCookie(w, &http.Cookie{
Name: authCookieName,
Value: fmt.Sprint(newSessionId),
Secure: true,
Name: authCookieName,
Value: fmt.Sprint(newSessionId),
Secure: true,
HttpOnly: true,
Path: "/",
Path: "/",
})
log.Printf("is is %d. room things %s & %s, personal things %s and %s. \n found room %+v",
newSessionId, roomName, roomPass, personName, personPass, room,
@@ -261,4 +261,3 @@ func joinRoomHandler( templateFs *embed.FS,
w.Header().Add("HX-Redirect", "/")
}
}