Share links need not have type

This commit is contained in:
Daniel Ponte 2025-01-22 22:28:59 -05:00
parent e38e17abee
commit f5a238bccb

View file

@ -6,6 +6,7 @@ import (
"net/url" "net/url"
"time" "time"
"dynatron.me/x/stillbox/internal/common"
"dynatron.me/x/stillbox/internal/forms" "dynatron.me/x/stillbox/internal/forms"
"dynatron.me/x/stillbox/pkg/rbac/entities" "dynatron.me/x/stillbox/pkg/rbac/entities"
"dynatron.me/x/stillbox/pkg/shares" "dynatron.me/x/stillbox/pkg/shares"
@ -76,6 +77,7 @@ func (sa *shareAPI) Subrouter() http.Handler {
func (sa *shareAPI) RootRouter() http.Handler { func (sa *shareAPI) RootRouter() http.Handler {
r := chi.NewMux() 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}", sa.routeShare)
r.Get("/{shareId:[A-Za-z0-9_-]{20,}}/{type}/{subID}", sa.routeShare) r.Get("/{shareId:[A-Za-z0-9_-]{20,}}/{type}/{subID}", sa.routeShare)
return r return r
@ -107,8 +109,8 @@ func (sa *shareAPI) routeShare(w http.ResponseWriter, r *http.Request) {
shs := shares.FromCtx(ctx) shs := shares.FromCtx(ctx)
params := struct { params := struct {
Type string `param:"type"`
ID string `param:"shareId"` ID string `param:"shareId"`
Type *string `param:"type"`
SubID *string `param:"subID"` SubID *string `param:"subID"`
}{} }{}
@ -118,20 +120,31 @@ func (sa *shareAPI) routeShare(w http.ResponseWriter, r *http.Request) {
return return
} }
rType := ShareRequestType(params.Type)
id := params.ID id := params.ID
if !rType.IsValid() {
wErr(w, r, autoError(ErrBadShare))
return
}
sh, err := shs.GetShare(ctx, id) sh, err := shs.GetShare(ctx, id)
if err != nil { if err != nil {
wErr(w, r, autoError(err)) wErr(w, r, autoError(err))
return 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()) { if sh.Expiration != nil && sh.Expiration.Time().Before(time.Now()) {
wErr(w, r, autoError(shares.ErrNoShare)) wErr(w, r, autoError(shares.ErrNoShare))
return return