- Updated API documentation to include `ownedByMe` field in character responses, indicating if a character was last updated by the current user's tokens. - Modified MapView component to track and display the live status based on user-owned characters. - Enhanced map data handling to exclude hidden maps for non-admin users and improved character icon representation on the map. - Refactored character data structures to support new properties and ensure accurate rendering in the frontend.
101 lines
2.8 KiB
Go
101 lines
2.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/andyleap/hnh-map/internal/app"
|
|
)
|
|
|
|
// APIConfig handles GET /map/api/config.
|
|
func (h *Handlers) APIConfig(rw http.ResponseWriter, req *http.Request) {
|
|
ctx := req.Context()
|
|
s := h.Auth.GetSession(ctx, req)
|
|
if s == nil {
|
|
JSONError(rw, http.StatusUnauthorized, "Unauthorized", "UNAUTHORIZED")
|
|
return
|
|
}
|
|
config, err := h.Map.GetConfig(ctx, s.Auths)
|
|
if err != nil {
|
|
HandleServiceError(rw, err)
|
|
return
|
|
}
|
|
JSON(rw, http.StatusOK, config)
|
|
}
|
|
|
|
// CharacterResponse is the API shape for a character, including ownedByMe for the current session.
|
|
type CharacterResponse struct {
|
|
Name string `json:"name"`
|
|
ID int `json:"id"`
|
|
Map int `json:"map"`
|
|
Position app.Position `json:"position"`
|
|
Type string `json:"type"`
|
|
Updated time.Time `json:"updated,omitempty"`
|
|
OwnedByMe bool `json:"ownedByMe"`
|
|
}
|
|
|
|
// APIGetChars handles GET /map/api/v1/characters.
|
|
func (h *Handlers) APIGetChars(rw http.ResponseWriter, req *http.Request) {
|
|
ctx := req.Context()
|
|
s := h.Auth.GetSession(ctx, req)
|
|
if !h.canAccessMap(s) {
|
|
JSONError(rw, http.StatusUnauthorized, "Unauthorized", "UNAUTHORIZED")
|
|
return
|
|
}
|
|
if !s.Auths.Has(app.AUTH_MARKERS) && !s.Auths.Has(app.AUTH_ADMIN) {
|
|
JSON(rw, http.StatusOK, []interface{}{})
|
|
return
|
|
}
|
|
chars := h.Map.GetCharacters()
|
|
out := make([]CharacterResponse, 0, len(chars))
|
|
for _, c := range chars {
|
|
out = append(out, CharacterResponse{
|
|
Name: c.Name,
|
|
ID: c.ID,
|
|
Map: c.Map,
|
|
Position: c.Position,
|
|
Type: c.Type,
|
|
Updated: c.Updated.UTC(),
|
|
OwnedByMe: c.Username == s.Username,
|
|
})
|
|
}
|
|
JSON(rw, http.StatusOK, out)
|
|
}
|
|
|
|
// APIGetMarkers handles GET /map/api/v1/markers.
|
|
func (h *Handlers) APIGetMarkers(rw http.ResponseWriter, req *http.Request) {
|
|
ctx := req.Context()
|
|
s := h.Auth.GetSession(ctx, req)
|
|
if !h.canAccessMap(s) {
|
|
JSONError(rw, http.StatusUnauthorized, "Unauthorized", "UNAUTHORIZED")
|
|
return
|
|
}
|
|
if !s.Auths.Has(app.AUTH_MARKERS) && !s.Auths.Has(app.AUTH_ADMIN) {
|
|
JSON(rw, http.StatusOK, []interface{}{})
|
|
return
|
|
}
|
|
markers, err := h.Map.GetMarkers(ctx)
|
|
if err != nil {
|
|
HandleServiceError(rw, err)
|
|
return
|
|
}
|
|
JSON(rw, http.StatusOK, markers)
|
|
}
|
|
|
|
// APIGetMaps handles GET /map/api/maps.
|
|
func (h *Handlers) APIGetMaps(rw http.ResponseWriter, req *http.Request) {
|
|
ctx := req.Context()
|
|
s := h.Auth.GetSession(ctx, req)
|
|
if !h.canAccessMap(s) {
|
|
JSONError(rw, http.StatusUnauthorized, "Unauthorized", "UNAUTHORIZED")
|
|
return
|
|
}
|
|
showHidden := s.Auths.Has(app.AUTH_ADMIN)
|
|
maps, err := h.Map.GetMaps(ctx, showHidden)
|
|
if err != nil {
|
|
HandleServiceError(rw, err)
|
|
return
|
|
}
|
|
JSON(rw, http.StatusOK, maps)
|
|
}
|