+ 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) { func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
data := &MemberCreatePayload{ data := &MemberCreatePayload{
// default values
MemberProfile: &types.MemberProfile{ MemberProfile: &types.MemberProfile{
IsAdmin: false, IsAdmin: false,
CanLogin: true, 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) { func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) {
member := GetMember(r) member := GetMember(r)
profile := member.GetProfile()
// TODO: Get whole profile from session. utils.HttpSuccess(w, profile)
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(),
})
} }
func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) { func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) {
member := GetMember(r) member := GetMember(r)
profile := member.GetProfile()
// 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(),
}
if !utils.HttpJsonRequest(w, r, &profile) { if !utils.HttpJsonRequest(w, r, &profile) {
return return

View File

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

View File

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

View File

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

View File

@ -27,21 +27,8 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error {
for _, session := range h.sessions.Members() { for _, session := range h.sessions.Members() {
members[session.ID()] = message.MemberData{ members[session.ID()] = message.MemberData{
ID: session.ID(), ID: session.ID(),
// TODO: Get whole profile from session. Profile: session.GetProfile(),
Profile: types.MemberProfile{ State: session.GetState(),
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(),
},
} }
} }