use json marshal default values.

This commit is contained in:
Miroslav Šedivý 2020-12-08 14:13:12 +01:00
parent 303c22bba5
commit 4b4ddcfe15

View File

@ -12,57 +12,64 @@ type MemberCreatePayload struct {
} }
type MemberDataPayload struct { type MemberDataPayload struct {
ID *string `json:"id"` ID string `json:"id"`
Secret *string `json:"secret,omitempty"` Secret string `json:"secret,omitempty"`
Name *string `json:"name"` Name string `json:"name"`
IsAdmin *bool `json:"is_admin"` IsAdmin bool `json:"is_admin"`
CanLogin *bool `json:"can_login"` CanLogin bool `json:"can_login"`
CanConnect *bool `json:"can_connect"` CanConnect bool `json:"can_connect"`
CanWatch *bool `json:"can_watch"` CanWatch bool `json:"can_watch"`
CanHost *bool `json:"can_host"` CanHost bool `json:"can_host"`
CanAccessClipboard *bool `json:"can_access_clipboard"` CanAccessClipboard bool `json:"can_access_clipboard"`
} }
func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) { func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
data := &MemberDataPayload{} data := &MemberDataPayload{
IsAdmin: false,
CanLogin: true,
CanConnect: true,
CanWatch: true,
CanHost: true,
CanAccessClipboard: true,
}
if !utils.HttpJsonRequest(w, r, data) { if !utils.HttpJsonRequest(w, r, data) {
return return
} }
if data.Secret == nil || *data.Secret == "" { if data.Secret == "" {
utils.HttpBadRequest(w, "Secret cannot be empty.") utils.HttpBadRequest(w, "Secret cannot be empty.")
return return
} }
if data.Name == nil || *data.Name == "" { if data.Name == "" {
utils.HttpBadRequest(w, "Name cannot be empty.") utils.HttpBadRequest(w, "Name cannot be empty.")
return return
} }
var ID string if data.ID == "" {
if data.ID == nil || *data.ID == "" {
var err error var err error
if ID, err = utils.NewUID(32); err != nil { if data.ID, err = utils.NewUID(32); err != nil {
utils.HttpInternalServerError(w, err) utils.HttpInternalServerError(w, err)
return return
} }
} else { } else {
ID = *data.ID if _, ok := h.sessions.Get(data.ID); ok {
if _, ok := h.sessions.Get(ID); ok {
utils.HttpBadRequest(w, "Member ID already exists.") utils.HttpBadRequest(w, "Member ID already exists.")
return return
} }
} }
session, err := h.sessions.Create(ID, types.MemberProfile{ // TODO: Join structs?
Secret: *data.Secret, session, err := h.sessions.Create(data.ID, types.MemberProfile{
Name: *data.Name, Secret: data.Secret,
IsAdmin: defaultBool(data.IsAdmin, false), Name: data.Name,
CanLogin: defaultBool(data.CanLogin, true), IsAdmin: data.IsAdmin,
CanConnect: defaultBool(data.CanConnect, true), CanLogin: data.CanLogin,
CanWatch: defaultBool(data.CanWatch, true), CanConnect: data.CanConnect,
CanHost: defaultBool(data.CanHost, true), CanWatch: data.CanWatch,
CanAccessClipboard: defaultBool(data.CanAccessClipboard, true), CanHost: data.CanHost,
CanAccessClipboard: data.CanAccessClipboard,
}) })
if err != nil { if err != nil {
@ -79,46 +86,44 @@ func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) {
member := GetMember(r) member := GetMember(r)
// TODO: Join structs? // TODO: Join structs?
// TODO: Ugly.
utils.HttpSuccess(w, MemberDataPayload{ utils.HttpSuccess(w, MemberDataPayload{
Name: func(v string) *string { return &v }(member.Name()), Name: member.Name(),
IsAdmin: func(v bool) *bool { return &v }(member.IsAdmin()), IsAdmin: member.IsAdmin(),
CanLogin: func(v bool) *bool { return &v }(member.CanLogin()), CanLogin: member.CanLogin(),
CanConnect: func(v bool) *bool { return &v }(member.CanConnect()), CanConnect: member.CanConnect(),
CanWatch: func(v bool) *bool { return &v }(member.CanWatch()), CanWatch: member.CanWatch(),
CanHost: func(v bool) *bool { return &v }(member.CanHost()), CanHost: member.CanHost(),
CanAccessClipboard: func(v bool) *bool { return &v }(member.CanAccessClipboard()), CanAccessClipboard: member.CanAccessClipboard(),
}) })
} }
func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) { func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) {
data := &MemberDataPayload{} member := GetMember(r)
data := &MemberDataPayload{
Name: member.Name(),
IsAdmin: member.IsAdmin(),
CanLogin: member.CanLogin(),
CanConnect: member.CanConnect(),
CanWatch: member.CanWatch(),
CanHost: member.CanHost(),
CanAccessClipboard: member.CanAccessClipboard(),
}
if !utils.HttpJsonRequest(w, r, data) { if !utils.HttpJsonRequest(w, r, data) {
return return
} }
member := GetMember(r)
secret := ""
if data.Secret != nil && *data.Secret != "" {
secret = *data.Secret
}
name := member.Name()
if data.Name != nil && *data.Name != "" {
name = *data.Name
}
// TODO: Join structs? // TODO: Join structs?
err := h.sessions.Update(member.ID(), types.MemberProfile{ err := h.sessions.Update(member.ID(), types.MemberProfile{
Secret: secret, Secret: data.Secret,
Name: name, Name: data.Name,
IsAdmin: defaultBool(data.IsAdmin, member.IsAdmin()), IsAdmin: data.IsAdmin,
CanLogin: defaultBool(data.CanLogin, member.CanLogin()), CanLogin: data.CanLogin,
CanConnect: defaultBool(data.CanConnect, member.CanConnect()), CanConnect: data.CanConnect,
CanWatch: defaultBool(data.CanWatch, member.CanWatch()), CanWatch: data.CanWatch,
CanHost: defaultBool(data.CanHost, member.CanHost()), CanHost: data.CanHost,
CanAccessClipboard: defaultBool(data.CanAccessClipboard, member.CanAccessClipboard()), CanAccessClipboard: data.CanAccessClipboard,
}) })
if err != nil { if err != nil {
@ -139,10 +144,3 @@ func (h *MembersHandler) membersDelete(w http.ResponseWriter, r *http.Request) {
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }
func defaultBool(val *bool, def bool) bool {
if val != nil {
return *val
}
return def
}