From 209971cb8a197a08cf5909e43a53c94750726232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Tue, 8 Dec 2020 15:14:20 +0100 Subject: [PATCH] member profile & state to types. --- internal/api/members/crud.go | 65 +++++++-------------------- internal/types/message/messages.go | 31 ++++++------- internal/types/session.go | 20 +++++---- internal/websocket/handler/session.go | 37 ++++++++------- internal/websocket/handler/system.go | 7 +-- 5 files changed, 68 insertions(+), 92 deletions(-) diff --git a/internal/api/members/crud.go b/internal/api/members/crud.go index f5122a0c..3dba1791 100644 --- a/internal/api/members/crud.go +++ b/internal/api/members/crud.go @@ -9,28 +9,19 @@ import ( type MemberCreatePayload struct { ID string `json:"id"` -} - -type MemberDataPayload struct { - ID string `json:"id"` - Secret string `json:"secret,omitempty"` - Name string `json:"name"` - IsAdmin bool `json:"is_admin"` - CanLogin bool `json:"can_login"` - CanConnect bool `json:"can_connect"` - CanWatch bool `json:"can_watch"` - CanHost bool `json:"can_host"` - CanAccessClipboard bool `json:"can_access_clipboard"` + *types.MemberProfile } func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) { - data := &MemberDataPayload{ - IsAdmin: false, - CanLogin: true, - CanConnect: true, - CanWatch: true, - CanHost: true, - CanAccessClipboard: true, + data := &MemberCreatePayload{ + MemberProfile: &types.MemberProfile{ + IsAdmin: false, + CanLogin: true, + CanConnect: true, + CanWatch: true, + CanHost: true, + CanAccessClipboard: true, + }, } if !utils.HttpJsonRequest(w, r, data) { @@ -60,18 +51,7 @@ func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) { } } - // TODO: Join structs? - session, err := h.sessions.Create(data.ID, types.MemberProfile{ - Secret: data.Secret, - Name: data.Name, - IsAdmin: data.IsAdmin, - CanLogin: data.CanLogin, - CanConnect: data.CanConnect, - CanWatch: data.CanWatch, - CanHost: data.CanHost, - CanAccessClipboard: data.CanAccessClipboard, - }) - + session, err := h.sessions.Create(data.ID, *data.MemberProfile) if err != nil { utils.HttpInternalServerError(w, err) return @@ -85,8 +65,8 @@ func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) { func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) { member := GetMember(r) - // TODO: Join structs? - utils.HttpSuccess(w, MemberDataPayload{ + // TODO: Get whole profile from session. + utils.HttpSuccess(w, types.MemberProfile{ Name: member.Name(), IsAdmin: member.IsAdmin(), CanLogin: member.CanLogin(), @@ -100,7 +80,8 @@ func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) { func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) { member := GetMember(r) - data := &MemberDataPayload{ + // TODO: Get whole profile from session. + profile := types.MemberProfile{ Name: member.Name(), IsAdmin: member.IsAdmin(), CanLogin: member.CanLogin(), @@ -110,23 +91,11 @@ func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) { CanAccessClipboard: member.CanAccessClipboard(), } - if !utils.HttpJsonRequest(w, r, data) { + if !utils.HttpJsonRequest(w, r, &profile) { return } - // TODO: Join structs? - err := h.sessions.Update(member.ID(), types.MemberProfile{ - Secret: data.Secret, - Name: data.Name, - IsAdmin: data.IsAdmin, - CanLogin: data.CanLogin, - CanConnect: data.CanConnect, - CanWatch: data.CanWatch, - CanHost: data.CanHost, - CanAccessClipboard: data.CanAccessClipboard, - }) - - if err != nil { + if err := h.sessions.Update(member.ID(), profile); err != nil { utils.HttpInternalServerError(w, err) return } diff --git a/internal/types/message/messages.go b/internal/types/message/messages.go index 3d5ca749..5766331f 100644 --- a/internal/types/message/messages.go +++ b/internal/types/message/messages.go @@ -1,5 +1,9 @@ package message +import ( + "demodesk/neko/internal/types" +) + type Message struct { Event string `json:"event"` Payload interface{} `json:"payload"` // TODO: New. @@ -55,29 +59,22 @@ type MemberID struct { } type MemberProfile struct { - Event string `json:"event,omitempty"` - ID string `json:"id,omitempty"` - Name string `json:"name"` - IsAdmin bool `json:"is_admin"` - CanLogin bool `json:"can_login"` - CanConnect bool `json:"can_connect"` - CanWatch bool `json:"can_watch"` - CanHost bool `json:"can_host"` - CanAccessClipboard bool `json:"can_access_clipboard"` + Event string `json:"event,omitempty"` + ID string `json:"id"` + *types.MemberProfile } type MemberState struct { - Event string `json:"event,omitempty"` - ID string `json:"id,omitempty"` - IsConnected bool `json:"is_connected"` - IsWatching bool `json:"is_watching"` + Event string `json:"event,omitempty"` + ID string `json:"id"` + *types.MemberState } type MemberData struct { - Event string `json:"event,omitempty"` - ID string `json:"id"` - Profile MemberProfile `json:"profile"` - State MemberState `json:"state"` + Event string `json:"event,omitempty"` + ID string `json:"id"` + Profile types.MemberProfile `json:"profile"` + State types.MemberState `json:"state"` } ///////////////////////////// diff --git a/internal/types/session.go b/internal/types/session.go index 0d753e24..9611cb5f 100644 --- a/internal/types/session.go +++ b/internal/types/session.go @@ -3,15 +3,19 @@ package types import "net/http" type MemberProfile struct { - Secret string - Name string + Secret string `json:"secret,omitempty"` + Name string `json:"name"` + IsAdmin bool `json:"is_admin"` + CanLogin bool `json:"can_login"` + CanConnect bool `json:"can_connect"` + CanWatch bool `json:"can_watch"` + CanHost bool `json:"can_host"` + CanAccessClipboard bool `json:"can_access_clipboard"` +} - IsAdmin bool - CanLogin bool - CanConnect bool - CanWatch bool - CanHost bool - CanAccessClipboard bool +type MemberState struct { + IsConnected bool `json:"is_connected"` + IsWatching bool `json:"is_watching"` } type MembersDatabase interface { diff --git a/internal/websocket/handler/session.go b/internal/websocket/handler/session.go index d17f9c04..36bde424 100644 --- a/internal/websocket/handler/session.go +++ b/internal/websocket/handler/session.go @@ -7,12 +7,12 @@ import ( ) func (h *MessageHandlerCtx) SessionCreated(session types.Session) error { - // TODO: Join structs? h.sessions.Broadcast( message.MemberData{ Event: event.MEMBER_CREATED, ID: session.ID(), - Profile: message.MemberProfile{ + // TODO: Get whole profile from session. + Profile: types.MemberProfile{ Name: session.Name(), IsAdmin: session.IsAdmin(), CanLogin: session.CanLogin(), @@ -21,7 +21,8 @@ func (h *MessageHandlerCtx) SessionCreated(session types.Session) error { CanHost: session.CanHost(), CanAccessClipboard: session.CanAccessClipboard(), }, - State: message.MemberState{ + // TODO: Get whole state from session. + State: types.MemberState{ IsConnected: session.IsConnected(), IsWatching: session.IsWatching(), }, @@ -81,31 +82,35 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error { } func (h *MessageHandlerCtx) SessionProfileChanged(session types.Session) error { - // TODO: Join structs? h.sessions.Broadcast( message.MemberProfile{ - Event: event.MEMBER_PROFILE, - ID: session.ID(), - Name: session.Name(), - IsAdmin: session.IsAdmin(), - CanLogin: session.CanLogin(), - CanConnect: session.CanConnect(), - CanWatch: session.CanWatch(), - CanHost: session.CanHost(), - CanAccessClipboard: session.CanAccessClipboard(), + Event: event.MEMBER_PROFILE, + ID: session.ID(), + // TODO: Get whole profile from session. + MemberProfile: &types.MemberProfile{ + Name: session.Name(), + IsAdmin: session.IsAdmin(), + CanLogin: session.CanLogin(), + CanConnect: session.CanConnect(), + CanWatch: session.CanWatch(), + CanHost: session.CanHost(), + CanAccessClipboard: session.CanAccessClipboard(), + }, }, nil) return nil } func (h *MessageHandlerCtx) SessionStateChanged(session types.Session) error { - // TODO: Join structs? h.sessions.Broadcast( message.MemberState{ Event: event.MEMBER_STATE, ID: session.ID(), - IsConnected: session.IsConnected(), - IsWatching: session.IsWatching(), + // TODO: Get whole state from session. + MemberState: &types.MemberState{ + IsConnected: session.IsConnected(), + IsWatching: session.IsWatching(), + }, }, nil) return nil diff --git a/internal/websocket/handler/system.go b/internal/websocket/handler/system.go index cacd28ec..47084c15 100644 --- a/internal/websocket/handler/system.go +++ b/internal/websocket/handler/system.go @@ -25,10 +25,10 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error { members := map[string]message.MemberData{} for _, session := range h.sessions.Members() { - // TODO: Join structs? members[session.ID()] = message.MemberData{ ID: session.ID(), - Profile: message.MemberProfile{ + // TODO: Get whole profile from session. + Profile: types.MemberProfile{ Name: session.Name(), IsAdmin: session.IsAdmin(), CanLogin: session.CanLogin(), @@ -37,7 +37,8 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error { CanHost: session.CanHost(), CanAccessClipboard: session.CanAccessClipboard(), }, - State: message.MemberState{ + // TODO: Get whole state from session. + State: types.MemberState{ IsConnected: session.IsConnected(), IsWatching: session.IsWatching(), },