stillbox/pkg/rest/share.go

178 lines
3.7 KiB
Go
Raw Normal View History

package rest
import (
2025-01-19 21:51:39 -05:00
"errors"
"net/http"
"net/url"
2025-01-19 21:51:39 -05:00
"time"
2025-01-22 22:28:59 -05:00
"dynatron.me/x/stillbox/internal/common"
"dynatron.me/x/stillbox/internal/forms"
"dynatron.me/x/stillbox/pkg/rbac/entities"
2025-01-19 21:51:39 -05:00
"dynatron.me/x/stillbox/pkg/shares"
"github.com/go-chi/chi/v5"
2025-01-20 20:28:25 -05:00
"github.com/google/uuid"
)
2025-01-19 21:51:39 -05:00
var (
ErrBadShare = errors.New("bad share request type")
)
type ShareRequestType string
const (
ShareRequestCall ShareRequestType = "call"
2025-01-20 20:28:25 -05:00
ShareRequestCallDL ShareRequestType = "callDL"
2025-01-19 21:51:39 -05:00
ShareRequestIncident ShareRequestType = "incident"
ShareRequestIncidentM3U ShareRequestType = "m3u"
2025-01-22 14:15:53 -05:00
ShareRequestTalkgroups ShareRequestType = "talkgroups"
2025-01-19 21:51:39 -05:00
)
func (rt ShareRequestType) IsValid() bool {
switch rt {
2025-01-22 14:15:53 -05:00
case ShareRequestCall, ShareRequestCallDL, ShareRequestIncident,
ShareRequestIncidentM3U, ShareRequestTalkgroups:
2025-01-19 21:51:39 -05:00
return true
}
return false
}
2025-01-21 09:21:46 -05:00
func (rt ShareRequestType) IsValidSubtype() bool {
switch rt {
2025-01-22 14:15:53 -05:00
case ShareRequestCall, ShareRequestCallDL, ShareRequestTalkgroups:
2025-01-21 09:21:46 -05:00
return true
}
return false
}
2025-01-22 14:15:53 -05:00
type ID interface {
}
type HandlerFunc func(id ID, share *shares.Share, w http.ResponseWriter, r *http.Request)
2025-01-20 20:28:25 -05:00
type ShareHandlers map[ShareRequestType]HandlerFunc
type shareAPI struct {
baseURL *url.URL
2025-01-20 20:28:25 -05:00
shnd ShareHandlers
2025-01-20 16:31:43 -05:00
}
2025-01-19 21:51:39 -05:00
2025-01-20 20:28:25 -05:00
func newShareAPI(baseURL *url.URL, shnd ShareHandlers) *shareAPI {
2025-01-19 21:51:39 -05:00
return &shareAPI{
baseURL: baseURL,
2025-01-20 20:28:25 -05:00
shnd: shnd,
2025-01-19 21:51:39 -05:00
}
}
2025-01-19 21:51:39 -05:00
func (sa *shareAPI) Subrouter() http.Handler {
r := chi.NewMux()
2025-01-19 21:51:39 -05:00
r.Post(`/create`, sa.createShare)
r.Delete(`/{id:[A-Za-z0-9_-]{20,}}`, sa.deleteShare)
return r
}
2025-01-20 20:28:25 -05:00
func (sa *shareAPI) RootRouter() http.Handler {
r := chi.NewMux()
2025-01-22 22:28:59 -05:00
r.Get("/{shareId:[A-Za-z0-9_-]{20,}}", sa.routeShare)
2025-01-22 14:15:53 -05:00
r.Get("/{shareId:[A-Za-z0-9_-]{20,}}/{type}", sa.routeShare)
r.Get("/{shareId:[A-Za-z0-9_-]{20,}}/{type}/{subID}", sa.routeShare)
2025-01-20 20:28:25 -05:00
return r
}
2025-01-19 21:51:39 -05:00
func (sa *shareAPI) createShare(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2025-01-19 21:51:39 -05:00
shs := shares.FromCtx(ctx)
p := shares.CreateShareParams{}
err := forms.Unmarshal(r, &p, forms.WithTag("json"), forms.WithAcceptBlank(), forms.WithOmitEmpty())
if err != nil {
wErr(w, r, badRequest(err))
return
}
2025-01-19 21:51:39 -05:00
sh, err := shs.NewShare(ctx, p)
if err != nil {
wErr(w, r, autoError(err))
return
}
2025-01-19 21:51:39 -05:00
respond(w, r, sh)
}
2025-01-20 20:28:25 -05:00
func (sa *shareAPI) routeShare(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2025-01-19 21:51:39 -05:00
shs := shares.FromCtx(ctx)
2025-01-20 16:31:43 -05:00
params := struct {
2025-01-22 14:15:53 -05:00
ID string `param:"shareId"`
2025-01-22 22:28:59 -05:00
Type *string `param:"type"`
2025-01-22 14:15:53 -05:00
SubID *string `param:"subID"`
2025-01-20 16:31:43 -05:00
}{}
err := decodeParams(&params, r)
if err != nil {
2025-01-20 20:28:25 -05:00
wErr(w, r, autoError(err))
return
}
2025-01-20 16:31:43 -05:00
id := params.ID
2025-01-19 21:51:39 -05:00
sh, err := shs.GetShare(ctx, id)
if err != nil {
2025-01-20 20:28:25 -05:00
wErr(w, r, autoError(err))
return
}
2025-01-22 22:28:59 -05:00
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
}
2025-01-19 21:51:39 -05:00
if sh.Expiration != nil && sh.Expiration.Time().Before(time.Now()) {
2025-01-20 20:28:25 -05:00
wErr(w, r, autoError(shares.ErrNoShare))
return
}
ctx = entities.CtxWithSubject(ctx, sh)
2025-01-19 21:51:39 -05:00
r = r.WithContext(ctx)
2025-01-22 14:15:53 -05:00
switch rType {
case ShareRequestTalkgroups:
sa.shnd[rType](nil, sh, w, r)
case ShareRequestCall, ShareRequestCallDL:
2025-01-21 09:21:46 -05:00
if params.SubID == nil {
wErr(w, r, autoError(ErrBadShare))
return
}
subIDU, err := uuid.Parse(*params.SubID)
if err != nil {
wErr(w, r, badRequest(err))
return
}
2025-01-22 14:15:53 -05:00
sa.shnd[rType](subIDU, sh, w, r)
case ShareRequestIncident, ShareRequestIncidentM3U:
sa.shnd[rType](sh.EntityID, sh, w, r)
2025-01-21 09:21:46 -05:00
}
}
2025-01-20 20:28:25 -05:00
func (sa *shareAPI) deleteShare(w http.ResponseWriter, r *http.Request) {
}