From f5a238bccb16b43fbf5e96f741851945eb143b3e Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Wed, 22 Jan 2025 22:28:59 -0500 Subject: [PATCH 1/2] Share links need not have type --- pkg/rest/share.go | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/pkg/rest/share.go b/pkg/rest/share.go index 4cc260e..8a726e6 100644 --- a/pkg/rest/share.go +++ b/pkg/rest/share.go @@ -6,6 +6,7 @@ import ( "net/url" "time" + "dynatron.me/x/stillbox/internal/common" "dynatron.me/x/stillbox/internal/forms" "dynatron.me/x/stillbox/pkg/rbac/entities" "dynatron.me/x/stillbox/pkg/shares" @@ -76,6 +77,7 @@ func (sa *shareAPI) Subrouter() http.Handler { func (sa *shareAPI) RootRouter() http.Handler { r := chi.NewMux() + r.Get("/{shareId:[A-Za-z0-9_-]{20,}}", sa.routeShare) r.Get("/{shareId:[A-Za-z0-9_-]{20,}}/{type}", sa.routeShare) r.Get("/{shareId:[A-Za-z0-9_-]{20,}}/{type}/{subID}", sa.routeShare) return r @@ -107,8 +109,8 @@ func (sa *shareAPI) routeShare(w http.ResponseWriter, r *http.Request) { shs := shares.FromCtx(ctx) params := struct { - Type string `param:"type"` ID string `param:"shareId"` + Type *string `param:"type"` SubID *string `param:"subID"` }{} @@ -118,20 +120,31 @@ func (sa *shareAPI) routeShare(w http.ResponseWriter, r *http.Request) { return } - rType := ShareRequestType(params.Type) id := params.ID - - if !rType.IsValid() { - wErr(w, r, autoError(ErrBadShare)) - return - } - sh, err := shs.GetShare(ctx, id) if err != nil { wErr(w, r, autoError(err)) return } + var rType ShareRequestType + if params.Type != nil { + rType = ShareRequestType(*params.Type) + } else { + switch sh.Type { + case shares.EntityCall: + rType = ShareRequestCall + params.SubID = common.PtrTo(sh.EntityID.String()) + case shares.EntityIncident: + rType = ShareRequestIncident + } + } + + if !rType.IsValid() { + wErr(w, r, autoError(ErrBadShare)) + return + } + if sh.Expiration != nil && sh.Expiration.Time().Before(time.Now()) { wErr(w, r, autoError(shares.ErrNoShare)) return From 5ae5461eab601d35a7f7a841da7286f3d25a5d02 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Wed, 22 Jan 2025 22:37:49 -0500 Subject: [PATCH 2/2] Set header for share type when not specified --- pkg/rest/share.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/rest/share.go b/pkg/rest/share.go index 8a726e6..1ffba53 100644 --- a/pkg/rest/share.go +++ b/pkg/rest/share.go @@ -128,6 +128,7 @@ func (sa *shareAPI) routeShare(w http.ResponseWriter, r *http.Request) { } var rType ShareRequestType + if params.Type != nil { rType = ShareRequestType(*params.Type) } else { @@ -138,6 +139,7 @@ func (sa *shareAPI) routeShare(w http.ResponseWriter, r *http.Request) { case shares.EntityIncident: rType = ShareRequestIncident } + w.Header().Set("X-Share-Type", string(rType)) } if !rType.IsValid() {