Refactor
This commit is contained in:
+15
-6
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@@ -26,12 +27,17 @@ func handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
sessionID, _ := lib.CreateSession(user.ID)
|
||||
sessionID, err := lib.CreateSession(user.ID)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to create session", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "session_token",
|
||||
Value: sessionID,
|
||||
Expires: time.Now().Add(24 * time.Hour),
|
||||
HttpOnly: true,
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
Path: "/",
|
||||
})
|
||||
|
||||
@@ -68,13 +74,16 @@ func handleRegister(w http.ResponseWriter, r *http.Request) {
|
||||
func handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, err := r.Cookie("session_token")
|
||||
if err == nil {
|
||||
lib.DeleteSession(cookie.Value)
|
||||
if err := lib.DeleteSession(cookie.Value); err != nil {
|
||||
log.Printf("delete session error: %v", err)
|
||||
}
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "session_token",
|
||||
Value: "",
|
||||
Expires: time.Now().Add(-1 * time.Hour),
|
||||
Path: "/",
|
||||
Name: "session_token",
|
||||
Value: "",
|
||||
Expires: time.Now().Add(-1 * time.Hour),
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
Path: "/",
|
||||
})
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
+6
-3
@@ -23,7 +23,11 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
rooms, _ := lib.GetRoomsForUser(user.ID)
|
||||
rooms, err := lib.GetRoomSummariesForUser(user.ID)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load rooms", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data := struct {
|
||||
*lib.User
|
||||
Rooms []RoomView
|
||||
@@ -32,14 +36,13 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
for _, room := range rooms {
|
||||
members, _ := lib.GetRoomMembers(room.ID)
|
||||
data.Rooms = append(data.Rooms, RoomView{
|
||||
ID: room.ID,
|
||||
Name: room.Name,
|
||||
Code: room.Code,
|
||||
Scale: room.Scale,
|
||||
IsOwner: room.OwnerID == user.ID,
|
||||
MemberCount: len(members),
|
||||
MemberCount: room.MemberCount,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+45
-9
@@ -35,37 +35,64 @@ func handleJoinRoom(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "room not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
lib.AddUserToRoom(room.ID, user.ID)
|
||||
if err := lib.AddUserToRoom(room.ID, user.ID); err != nil {
|
||||
http.Error(w, "failed to join room", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, fmt.Sprintf("/rooms/%d", room.ID), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func handleRoom(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getRoomID(r)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
room, err := lib.GetRoomByID(roomID)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
lib.AddUserToRoom(room.ID, user.ID)
|
||||
if err := lib.AddUserToRoom(room.ID, user.ID); err != nil {
|
||||
http.Error(w, "failed to join room", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
renderTemplate(w, "room.html", buildRoomData(room, user))
|
||||
data, err := buildRoomData(room, user)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load room data", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
renderTemplate(w, "room.html", data)
|
||||
}
|
||||
|
||||
func handlePartialStories(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getRoomID(r)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
renderRoomStories(w, roomID, user)
|
||||
}
|
||||
|
||||
func handlePartialMembers(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getRoomID(r)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
renderRoomMembers(w, roomID, user)
|
||||
}
|
||||
|
||||
func handlePartialVoteArea(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getRoomID(r)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
room, err := lib.GetRoomByID(roomID)
|
||||
if err != nil {
|
||||
@@ -83,18 +110,27 @@ func handlePartialVoteArea(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
roomData, err := buildRoomData(room, user)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load room data", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data := struct {
|
||||
RoomData RoomData
|
||||
Story lib.Story
|
||||
}{
|
||||
RoomData: buildRoomData(room, user),
|
||||
RoomData: roomData,
|
||||
Story: *story,
|
||||
}
|
||||
renderTemplate(w, "vote-area", data)
|
||||
}
|
||||
|
||||
func handleDeleteRoom(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getRoomID(r)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
|
||||
room, err := lib.GetRoomByID(roomID)
|
||||
|
||||
+10
-5
@@ -9,17 +9,17 @@ import (
|
||||
)
|
||||
|
||||
func handleSSE(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getPathInt(r, "room_id")
|
||||
roomID, err := getPathInt(r, "room_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user, ok := r.Context().Value(userKey).(*lib.User)
|
||||
if !ok {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
ch := make(chan string, 10)
|
||||
addSSEClient(roomID, user.ID, ch)
|
||||
broadcast(roomID, "members")
|
||||
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
@@ -30,6 +30,11 @@ func handleSSE(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "streaming unsupported", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
ch := make(chan string, 10)
|
||||
addSSEClient(roomID, user.ID, ch)
|
||||
broadcast(roomID, "members")
|
||||
|
||||
notify := r.Context().Done()
|
||||
heartbeat := time.NewTicker(25 * time.Second)
|
||||
defer heartbeat.Stop()
|
||||
|
||||
+137
-37
@@ -9,13 +9,25 @@ import (
|
||||
)
|
||||
|
||||
func handleNewStoryForm(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getRoomID(r)
|
||||
room, _ := lib.GetRoomByID(roomID)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
room, err := lib.GetRoomByID(roomID)
|
||||
if err != nil {
|
||||
http.Error(w, "room not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
renderTemplate(w, "story_form.html", room)
|
||||
}
|
||||
|
||||
func handleAddStory(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
title := r.FormValue("title")
|
||||
if _, err := lib.CreateStory(id, title); err != nil {
|
||||
@@ -32,12 +44,22 @@ func handleAddStory(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleSetActiveStory(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
var sid int
|
||||
fmt.Sscanf(r.FormValue("story_id"), "%d", &sid)
|
||||
sid, err := getFormInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
lib.SetActiveStory(id, sid)
|
||||
if err := lib.SetActiveStory(id, sid); err != nil {
|
||||
http.Error(w, "failed to set active story", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
broadcast(id, "stories")
|
||||
broadcast(id, "members")
|
||||
@@ -49,13 +71,26 @@ func handleSetActiveStory(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleResetStory(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
var sid int
|
||||
fmt.Sscanf(r.PathValue("story_id"), "%d", &sid)
|
||||
sid, err := getPathInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
lib.UnrevealStory(sid)
|
||||
lib.ClearVotesForStory(sid)
|
||||
if err := lib.UnrevealStory(sid); err != nil {
|
||||
http.Error(w, "failed to reset story", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err := lib.ClearVotesForStory(sid); err != nil {
|
||||
http.Error(w, "failed to clear votes", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
broadcast(id, "stories")
|
||||
broadcast(id, "members")
|
||||
@@ -67,13 +102,23 @@ func handleResetStory(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleVote(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
value := r.FormValue("value")
|
||||
var sid int
|
||||
fmt.Sscanf(r.FormValue("story_id"), "%d", &sid)
|
||||
sid, err := getFormInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
lib.VoteOnStory(sid, user.ID, value)
|
||||
if err := lib.VoteOnStory(sid, user.ID, value); err != nil {
|
||||
http.Error(w, "failed to vote", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
broadcast(id, "members")
|
||||
story, storyErr := lib.GetStoryByID(sid)
|
||||
if storyErr == nil && story.Voted {
|
||||
@@ -81,11 +126,15 @@ func handleVote(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if isHTMX(r) {
|
||||
room, _ := lib.GetRoomByID(id)
|
||||
if storyErr != nil {
|
||||
http.Error(w, "story not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
room, err := lib.GetRoomByID(id)
|
||||
if err != nil {
|
||||
http.Error(w, "room not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
tmplData := struct {
|
||||
RoomID int
|
||||
Story lib.Story
|
||||
@@ -105,11 +154,21 @@ func handleVote(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleReveal(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
var sid int
|
||||
fmt.Sscanf(r.FormValue("story_id"), "%d", &sid)
|
||||
lib.RevealVotes(sid)
|
||||
sid, err := getFormInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := lib.RevealVotes(sid); err != nil {
|
||||
http.Error(w, "failed to reveal votes", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
broadcast(id, "stories")
|
||||
if isHTMX(r) {
|
||||
renderRoomStories(w, id, user)
|
||||
@@ -119,11 +178,21 @@ func handleReveal(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleUnrevealStory(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
var sid int
|
||||
fmt.Sscanf(r.PathValue("story_id"), "%d", &sid)
|
||||
lib.UnrevealStory(sid)
|
||||
sid, err := getPathInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := lib.UnrevealStory(sid); err != nil {
|
||||
http.Error(w, "failed to hide votes", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
broadcast(id, "stories")
|
||||
if isHTMX(r) {
|
||||
renderRoomStories(w, id, user)
|
||||
@@ -133,10 +202,21 @@ func handleUnrevealStory(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleEditStoryForm(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getRoomID(r)
|
||||
var storyID int
|
||||
fmt.Sscanf(r.PathValue("story_id"), "%d", &storyID)
|
||||
story, _ := lib.GetStoryByID(storyID)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
storyID, err := getPathInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
story, err := lib.GetStoryByID(storyID)
|
||||
if err != nil {
|
||||
http.Error(w, "story not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
data := struct {
|
||||
RoomID int
|
||||
Story lib.Story
|
||||
@@ -148,12 +228,22 @@ func handleEditStoryForm(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleRenameStory(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
var sid int
|
||||
fmt.Sscanf(r.PathValue("story_id"), "%d", &sid)
|
||||
sid, err := getPathInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
title := r.FormValue("title")
|
||||
lib.RenameStory(sid, title)
|
||||
if err := lib.RenameStory(sid, title); err != nil {
|
||||
http.Error(w, "failed to rename story", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
broadcast(id, "stories")
|
||||
if isHTMX(r) {
|
||||
renderRoomStories(w, id, user)
|
||||
@@ -163,11 +253,21 @@ func handleRenameStory(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleDeleteStory(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
var sid int
|
||||
fmt.Sscanf(r.PathValue("story_id"), "%d", &sid)
|
||||
lib.DeleteStory(sid)
|
||||
sid, err := getPathInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := lib.DeleteStory(sid); err != nil {
|
||||
http.Error(w, "failed to delete story", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
broadcast(id, "stories")
|
||||
if isHTMX(r) {
|
||||
renderRoomStories(w, id, user)
|
||||
|
||||
+12
-2
@@ -72,7 +72,12 @@ func renderRoomStories(w http.ResponseWriter, roomID int, user *lib.User) {
|
||||
http.Error(w, "room not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
renderTemplate(w, "stories-panel", buildRoomData(room, user))
|
||||
data, err := buildRoomData(room, user)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load room data", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
renderTemplate(w, "stories-panel", data)
|
||||
}
|
||||
|
||||
func renderRoomMembers(w http.ResponseWriter, roomID int, user *lib.User) {
|
||||
@@ -81,7 +86,12 @@ func renderRoomMembers(w http.ResponseWriter, roomID int, user *lib.User) {
|
||||
http.Error(w, "room not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
renderTemplate(w, "members-panel", buildRoomData(room, user))
|
||||
data, err := buildRoomData(room, user)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load room data", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
renderTemplate(w, "members-panel", data)
|
||||
}
|
||||
|
||||
func requireAuth(next http.HandlerFunc) http.HandlerFunc {
|
||||
|
||||
+96
-35
@@ -1,8 +1,9 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"sprintpadawan/lib"
|
||||
@@ -35,7 +36,7 @@ type RoomData struct {
|
||||
|
||||
var (
|
||||
roomClients = make(map[int][]*sseClient)
|
||||
clientsMu sync.Mutex
|
||||
clientsMu sync.RWMutex
|
||||
)
|
||||
|
||||
func addSSEClient(roomID, userID int, ch chan string) {
|
||||
@@ -60,23 +61,48 @@ func removeSSEClient(roomID int, ch chan string) {
|
||||
}
|
||||
|
||||
func broadcast(roomID int, event string) {
|
||||
clientsMu.Lock()
|
||||
defer clientsMu.Unlock()
|
||||
alive := roomClients[roomID][:0]
|
||||
for _, c := range roomClients[roomID] {
|
||||
clientsMu.RLock()
|
||||
snapshot := append([]*sseClient(nil), roomClients[roomID]...)
|
||||
clientsMu.RUnlock()
|
||||
|
||||
if len(snapshot) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
deadSet := make(map[*sseClient]struct{})
|
||||
for _, c := range snapshot {
|
||||
select {
|
||||
case c.ch <- event:
|
||||
alive = append(alive, c)
|
||||
default:
|
||||
// drop dead client
|
||||
deadSet[c] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
if len(deadSet) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
clientsMu.Lock()
|
||||
defer clientsMu.Unlock()
|
||||
|
||||
current := roomClients[roomID]
|
||||
alive := current[:0]
|
||||
for _, c := range current {
|
||||
if _, dead := deadSet[c]; dead {
|
||||
continue
|
||||
}
|
||||
alive = append(alive, c)
|
||||
}
|
||||
if len(alive) == 0 {
|
||||
delete(roomClients, roomID)
|
||||
return
|
||||
}
|
||||
roomClients[roomID] = alive
|
||||
}
|
||||
|
||||
func GetConnectedUserIDs(roomID int) []int {
|
||||
clientsMu.Lock()
|
||||
defer clientsMu.Unlock()
|
||||
clientsMu.RLock()
|
||||
defer clientsMu.RUnlock()
|
||||
seen := make(map[int]bool)
|
||||
var ids []int
|
||||
for _, c := range roomClients[roomID] {
|
||||
@@ -101,19 +127,45 @@ func scaleToOptions(scale string) []string {
|
||||
}
|
||||
}
|
||||
|
||||
func getRoomID(r *http.Request) int {
|
||||
func getRoomID(r *http.Request) (int, error) {
|
||||
return getPathInt(r, "id")
|
||||
}
|
||||
|
||||
func getPathInt(r *http.Request, key string) int {
|
||||
var id int
|
||||
fmt.Sscanf(r.PathValue(key), "%d", &id)
|
||||
return id
|
||||
func getPathInt(r *http.Request, key string) (int, error) {
|
||||
raw := r.PathValue(key)
|
||||
if raw == "" {
|
||||
return 0, errors.New("missing path parameter")
|
||||
}
|
||||
id, err := strconv.Atoi(raw)
|
||||
if err != nil || id <= 0 {
|
||||
return 0, errors.New("invalid path parameter")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func buildRoomData(room *lib.Room, user *lib.User) RoomData {
|
||||
members, _ := lib.GetRoomMembers(room.ID)
|
||||
stories, _ := lib.GetStoriesForRoom(room.ID)
|
||||
func getFormInt(r *http.Request, key string) (int, error) {
|
||||
raw := r.FormValue(key)
|
||||
if raw == "" {
|
||||
return 0, errors.New("missing form value")
|
||||
}
|
||||
id, err := strconv.Atoi(raw)
|
||||
if err != nil || id <= 0 {
|
||||
return 0, errors.New("invalid form value")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func buildRoomData(room *lib.Room, user *lib.User) (RoomData, error) {
|
||||
members, err := lib.GetRoomMembers(room.ID)
|
||||
if err != nil {
|
||||
return RoomData{}, err
|
||||
}
|
||||
|
||||
stories, err := lib.GetStoriesForRoom(room.ID)
|
||||
if err != nil {
|
||||
return RoomData{}, err
|
||||
}
|
||||
|
||||
connectedIDs := GetConnectedUserIDs(room.ID)
|
||||
connectedMap := make(map[int]bool)
|
||||
for _, cid := range connectedIDs {
|
||||
@@ -121,9 +173,30 @@ func buildRoomData(room *lib.Room, user *lib.User) RoomData {
|
||||
}
|
||||
connectedMap[user.ID] = true
|
||||
|
||||
var activeVotes []lib.Vote
|
||||
storyIDs := make([]int, 0, len(stories))
|
||||
revealedStoryIDs := make([]int, 0, len(stories))
|
||||
for _, s := range stories {
|
||||
storyIDs = append(storyIDs, s.ID)
|
||||
if s.Voted {
|
||||
revealedStoryIDs = append(revealedStoryIDs, s.ID)
|
||||
}
|
||||
}
|
||||
|
||||
votesByStory, err := lib.GetVotesForStories(storyIDs)
|
||||
if err != nil {
|
||||
return RoomData{}, err
|
||||
}
|
||||
|
||||
storyVotes, err := lib.GetVoteViewsForStories(revealedStoryIDs)
|
||||
if err != nil {
|
||||
return RoomData{}, err
|
||||
}
|
||||
|
||||
activeVotesByUser := make(map[int]bool)
|
||||
if room.ActiveStoryID != nil {
|
||||
activeVotes, _ = lib.GetVotesForStory(*room.ActiveStoryID)
|
||||
for _, v := range votesByStory[*room.ActiveStoryID] {
|
||||
activeVotesByUser[v.UserID] = true
|
||||
}
|
||||
}
|
||||
|
||||
var memberViews []MemberView
|
||||
@@ -131,13 +204,7 @@ func buildRoomData(room *lib.Room, user *lib.User) RoomData {
|
||||
if !connectedMap[m.ID] {
|
||||
continue
|
||||
}
|
||||
hasVoted := false
|
||||
for _, v := range activeVotes {
|
||||
if v.UserID == m.ID {
|
||||
hasVoted = true
|
||||
break
|
||||
}
|
||||
}
|
||||
hasVoted := activeVotesByUser[m.ID]
|
||||
memberViews = append(memberViews, MemberView{
|
||||
Username: m.Username,
|
||||
HasVoted: hasVoted,
|
||||
@@ -146,18 +213,12 @@ func buildRoomData(room *lib.Room, user *lib.User) RoomData {
|
||||
}
|
||||
|
||||
userVotes := make(map[int]string)
|
||||
storyVotes := make(map[int][]lib.VoteView)
|
||||
for _, s := range stories {
|
||||
votes, _ := lib.GetVotesForStory(s.ID)
|
||||
for _, v := range votes {
|
||||
for _, v := range votesByStory[s.ID] {
|
||||
if v.UserID == user.ID {
|
||||
userVotes[s.ID] = v.Value
|
||||
}
|
||||
}
|
||||
if s.Voted {
|
||||
vv, _ := lib.GetVotesWithUsernames(s.ID)
|
||||
storyVotes[s.ID] = vv
|
||||
}
|
||||
}
|
||||
|
||||
return RoomData{
|
||||
@@ -168,5 +229,5 @@ func buildRoomData(room *lib.Room, user *lib.User) RoomData {
|
||||
IsOwner: room.OwnerID == user.ID,
|
||||
UserVotes: userVotes,
|
||||
StoryVotes: storyVotes,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user