Files
CollabTable/backend/web.go
T
Gabriel20xxandClaude Sonnet 5 9a6ec940f7
Lint / Lint (push) Failing after 32s
Tests / Go backend (push) Successful in 54s
Tests / Dashboard checks (push) Successful in 51s
Docker / Docker Build & Push (push) Successful in 5m14s
Migrate auth to AuthGuard, retiring local Postgres-backed accounts
Same pattern as GameStream (commit 6a46007), adapted for CollabTable
being genuinely multi-user (not single-admin): admin user management
(list/create/update/delete, role reassignment, superadmin protection)
is fully proxied to AuthGuard, which already enforces the same rules
CollabTable checked locally. Cookie forwarding, no local session token,
AUTHGUARD_URL optional (unset = auth fully disabled), transport
failures always surface as errAuthGuardUnavailable rather than ever
letting a request through - proven with live tests against a closed
socket.

CollabTable's device-sync API token feature (separate from browser
sessions) is deliberately left local-only regardless of AUTHGUARD_URL,
keyed by a new currentUserID helper (AuthGuard's id when enabled, the
shared placeholder id 0 when disabled) - a real, functional token
either way, not faked data.

Real bug found and fixed: api_tokens.user_id had a hard FK into the
local auth_users table, which would reject new tokens once accounts
stop being written locally. Dropped the now-invalid FK.

Known, accepted gap (same as GameStream): self-service profile editing
for non-admin accounts is lost, since AuthGuard only exposes an admin
endpoint, no self-edit. TOTP QR issuer branding now comes from AuthGuard
rather than CollabTable.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-01 01:34:48 +02:00

142 lines
3.9 KiB
Go

package main
import (
"net/http"
"strconv"
"time"
)
func (s *Server) handleWebData(w http.ResponseWriter, r *http.Request) {
lists, err := s.db.queryLists(r.Context(), false, nil)
if err != nil {
writeJSONError(w, http.StatusInternalServerError, "Failed to fetch data")
return
}
fields, err := s.db.queryFields(r.Context(), false, nil, "")
if err != nil {
writeJSONError(w, http.StatusInternalServerError, "Failed to fetch data")
return
}
items, err := s.db.queryItems(r.Context(), false, nil, "")
if err != nil {
writeJSONError(w, http.StatusInternalServerError, "Failed to fetch data")
return
}
values, err := s.db.queryValues(r.Context(), nil, "")
if err != nil {
writeJSONError(w, http.StatusInternalServerError, "Failed to fetch data")
return
}
itemToList := make(map[string]string, len(items))
for _, item := range items {
itemToList[item.ID] = item.ListID
}
fieldMax := make(map[string]int64)
for _, field := range fields {
if int64(field.UpdatedAt) > fieldMax[field.ListID] {
fieldMax[field.ListID] = int64(field.UpdatedAt)
}
}
itemMax := make(map[string]int64)
for _, item := range items {
if int64(item.UpdatedAt) > itemMax[item.ListID] {
itemMax[item.ListID] = int64(item.UpdatedAt)
}
}
valueMax := make(map[string]int64)
for _, value := range values {
listID := itemToList[value.ItemID]
if listID != "" && int64(value.UpdatedAt) > valueMax[listID] {
valueMax[listID] = int64(value.UpdatedAt)
}
}
type webList struct {
List
EffectiveUpdatedAt int64 `json:"effectiveUpdatedAt"`
}
webLists := make([]webList, 0, len(lists))
for _, list := range lists {
effective := int64(list.UpdatedAt)
if fieldMax[list.ID] > effective {
effective = fieldMax[list.ID]
}
if itemMax[list.ID] > effective {
effective = itemMax[list.ID]
}
if valueMax[list.ID] > effective {
effective = valueMax[list.ID]
}
webLists = append(webLists, webList{List: list, EffectiveUpdatedAt: effective})
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
writeJSON(w, http.StatusOK, map[string]any{
"lists": webLists,
"fields": fields,
"items": items,
"values": values,
"stats": map[string]int{
"lists": len(lists),
"fields": len(fields),
"items": len(items),
"values": len(values),
},
"timestamp": time.Now().UnixMilli(),
})
}
func (s *Server) handleNotificationPoll(w http.ResponseWriter, r *http.Request) {
since, err := strconv.ParseInt(r.URL.Query().Get("since"), 10, 64)
if err != nil || since < 0 {
since = 0
}
deviceID := requestDeviceID(r, "")
query := `
SELECT id, deviceidorigin, eventtype, entitytype, entityid, listid, createdat
FROM notifications
WHERE createdat > $1
`
args := []any{since}
if deviceID != "" {
query += ` AND (deviceidorigin IS NULL OR deviceidorigin <> $2)`
args = append(args, deviceID)
}
query += ` ORDER BY createdat ASC LIMIT 500`
rows, err := s.db.pool.Query(r.Context(), query, args...)
if err != nil {
writeJSONError(w, http.StatusInternalServerError, "Failed to poll notifications")
return
}
defer rows.Close()
events := make([]NotificationEvent, 0)
for rows.Next() {
var event NotificationEvent
var origin, entityID, listID *string
if err := rows.Scan(&event.ID, &origin, &event.EventType, &event.EntityType, &entityID, &listID, &event.CreatedAt); err != nil {
writeJSONError(w, http.StatusInternalServerError, "Failed to poll notifications")
return
}
if origin != nil {
event.DeviceIDOrigin = *origin
}
if entityID != nil {
event.EntityID = *entityID
}
if listID != nil {
event.ListID = *listID
}
events = append(events, event)
}
if err := rows.Err(); err != nil {
writeJSONError(w, http.StatusInternalServerError, "Failed to poll notifications")
return
}
writeJSON(w, http.StatusOK, PollResponse{Notifications: events, ServerTimestamp: time.Now().UnixMilli()})
}