Merge pull request 'shareLinks improvements' (#105) from shareLinks into trunk
Reviewed-on: #105
This commit is contained in:
commit
a4fc0cacb1
1 changed files with 23 additions and 8 deletions
|
@ -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,33 @@ 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
|
||||||
|
}
|
||||||
|
w.Header().Set("X-Share-Type", string(rType))
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in a new issue