+ GetProfile & GetState members.

This commit is contained in:
Miroslav Šedivý 2020-12-08 15:37:48 +01:00
parent 209971cb8a
commit 7fb40c0384
5 changed files with 57 additions and 89 deletions

View File

@ -14,6 +14,7 @@ type MemberCreatePayload struct {
func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
data := &MemberCreatePayload{
// default values
MemberProfile: &types.MemberProfile{
IsAdmin: false,
CanLogin: true,
@ -64,32 +65,14 @@ func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) {
member := GetMember(r)
profile := member.GetProfile()
// TODO: Get whole profile from session.
utils.HttpSuccess(w, types.MemberProfile{
Name: member.Name(),
IsAdmin: member.IsAdmin(),
CanLogin: member.CanLogin(),
CanConnect: member.CanConnect(),
CanWatch: member.CanWatch(),
CanHost: member.CanHost(),
CanAccessClipboard: member.CanAccessClipboard(),
})
utils.HttpSuccess(w, profile)
}
func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) {
member := GetMember(r)
// TODO: Get whole profile from session.
profile := types.MemberProfile{
Name: member.Name(),
IsAdmin: member.IsAdmin(),
CanLogin: member.CanLogin(),
CanConnect: member.CanConnect(),
CanWatch: member.CanWatch(),
CanHost: member.CanHost(),
CanAccessClipboard: member.CanAccessClipboard(),
}
profile := member.GetProfile()
if !utils.HttpJsonRequest(w, r, &profile) {
return

View File

@ -59,6 +59,12 @@ func (session *SessionCtx) CanAccessClipboard() bool {
return session.profile.CanAccessClipboard
}
func (session *SessionCtx) GetProfile() types.MemberProfile {
profile := session.profile
profile.Secret = ""
return profile
}
func (session *SessionCtx) profileChanged() {
if !session.CanHost() && session.IsHost() {
session.manager.ClearHost()
@ -84,7 +90,7 @@ func (session *SessionCtx) profileChanged() {
}
// ---
// runtime
// state
// ---
func (session *SessionCtx) IsHost() bool {
@ -99,28 +105,12 @@ func (session *SessionCtx) IsWatching() bool {
return session.webrtc_connected
}
func (session *SessionCtx) Disconnect(reason string) error {
if err := session.Send(
message.SystemDisconnect{
Event: event.SYSTEM_DISCONNECT,
Message: reason,
}); err != nil {
return err
func (session *SessionCtx) GetState() types.MemberState {
// TODO: Save state in member struct.
return types.MemberState{
IsConnected: session.IsConnected(),
IsWatching: session.IsWatching(),
}
if session.websocket_peer != nil {
if err := session.websocket_peer.Destroy(); err != nil {
return err
}
}
if session.webrtc_peer != nil {
if err := session.webrtc_peer.Destroy(); err != nil {
return err
}
}
return nil
}
// ---
@ -157,6 +147,30 @@ func (session *SessionCtx) Send(v interface{}) error {
return session.websocket_peer.Send(v)
}
func (session *SessionCtx) Disconnect(reason string) error {
if err := session.Send(
message.SystemDisconnect{
Event: event.SYSTEM_DISCONNECT,
Message: reason,
}); err != nil {
return err
}
if session.websocket_peer != nil {
if err := session.websocket_peer.Destroy(); err != nil {
return err
}
}
if session.webrtc_peer != nil {
if err := session.webrtc_peer.Destroy(); err != nil {
return err
}
}
return nil
}
// ---
// webrtc
// ---

View File

@ -31,6 +31,7 @@ type MembersDatabase interface {
type Session interface {
ID() string
// profile
VerifySecret(secret string) bool
Name() string
IsAdmin() bool
@ -39,16 +40,21 @@ type Session interface {
CanWatch() bool
CanHost() bool
CanAccessClipboard() bool
GetProfile() MemberProfile
// state
IsHost() bool
IsConnected() bool
IsWatching() bool
Disconnect(reason string) error
GetState() MemberState
// websocket
SetWebSocketPeer(websocket_peer WebSocketPeer)
SetWebSocketConnected(connected bool)
Send(v interface{}) error
Disconnect(reason string) error
// webrtc
SetWebRTCPeer(webrtc_peer WebRTCPeer)
SetWebRTCConnected(connected bool)
SignalAnswer(sdp string) error

View File

@ -11,21 +11,8 @@ func (h *MessageHandlerCtx) SessionCreated(session types.Session) error {
message.MemberData{
Event: event.MEMBER_CREATED,
ID: session.ID(),
// TODO: Get whole profile from session.
Profile: types.MemberProfile{
Name: session.Name(),
IsAdmin: session.IsAdmin(),
CanLogin: session.CanLogin(),
CanConnect: session.CanConnect(),
CanWatch: session.CanWatch(),
CanHost: session.CanHost(),
CanAccessClipboard: session.CanAccessClipboard(),
},
// TODO: Get whole state from session.
State: types.MemberState{
IsConnected: session.IsConnected(),
IsWatching: session.IsWatching(),
},
Profile: session.GetProfile(),
State: session.GetState(),
}, nil)
return nil
@ -82,35 +69,26 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
}
func (h *MessageHandlerCtx) SessionProfileChanged(session types.Session) error {
profile := session.GetProfile()
h.sessions.Broadcast(
message.MemberProfile{
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(),
},
MemberProfile: &profile,
}, nil)
return nil
}
func (h *MessageHandlerCtx) SessionStateChanged(session types.Session) error {
state := session.GetState()
h.sessions.Broadcast(
message.MemberState{
Event: event.MEMBER_STATE,
ID: session.ID(),
// TODO: Get whole state from session.
MemberState: &types.MemberState{
IsConnected: session.IsConnected(),
IsWatching: session.IsWatching(),
},
MemberState: &state,
}, nil)
return nil

View File

@ -27,21 +27,8 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error {
for _, session := range h.sessions.Members() {
members[session.ID()] = message.MemberData{
ID: session.ID(),
// TODO: Get whole profile from session.
Profile: types.MemberProfile{
Name: session.Name(),
IsAdmin: session.IsAdmin(),
CanLogin: session.CanLogin(),
CanConnect: session.CanConnect(),
CanWatch: session.CanWatch(),
CanHost: session.CanHost(),
CanAccessClipboard: session.CanAccessClipboard(),
},
// TODO: Get whole state from session.
State: types.MemberState{
IsConnected: session.IsConnected(),
IsWatching: session.IsWatching(),
},
Profile: session.GetProfile(),
State: session.GetState(),
}
}