new MemberProfile & session refactor.

This commit is contained in:
Miroslav Šedivý 2020-12-02 17:29:38 +01:00
parent 220e162f42
commit 0d2f3405bb
4 changed files with 122 additions and 62 deletions

View File

@ -16,10 +16,11 @@ type MemberDataPayload struct {
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"`
//Enabled bool `json:"enabled"` CanLogin bool `json:"can_login"`
//CanControl bool `json:"can_control"` CanConnect bool `json:"can_connect"`
//CanWatch bool `json:"can_watch"` CanWatch bool `json:"can_watch"`
//ClipboardAccess bool `json:"clipboard_access"` CanHost bool `json:"can_host"`
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) {

View File

@ -238,6 +238,18 @@ func (manager *SessionManagerCtx) OnDisconnected(listener func(session types.Ses
}) })
} }
func (manager *SessionManagerCtx) OnReceivingStarted(listener func(session types.Session)) {
manager.emmiter.On("receiving_started", func(payload ...interface{}) {
listener(payload[0].(*SessionCtx))
})
}
func (manager *SessionManagerCtx) OnReceivingStopped(listener func(session types.Session)) {
manager.emmiter.On("receiving_stopped", func(payload ...interface{}) {
listener(payload[0].(*SessionCtx))
})
}
// --- // ---
// config // config
// --- // ---

View File

@ -23,6 +23,14 @@ func (session *SessionCtx) ID() string {
return session.id return session.id
} }
// ---
// profile
// ---
func (session *SessionCtx) VerifySecret(secret string) bool {
return session.profile.Secret == secret
}
func (session *SessionCtx) Name() string { func (session *SessionCtx) Name() string {
return session.profile.Name return session.profile.Name
} }
@ -31,55 +39,44 @@ func (session *SessionCtx) IsAdmin() bool {
return session.profile.IsAdmin return session.profile.IsAdmin
} }
func (session *SessionCtx) CanLogin() bool {
return session.profile.CanLogin
}
func (session *SessionCtx) CanConnect() bool {
return session.profile.CanConnect
}
func (session *SessionCtx) CanWatch() bool {
return session.profile.CanWatch
}
func (session *SessionCtx) CanHost() bool {
return session.profile.CanHost
}
func (session *SessionCtx) CanAccessClipboard() bool {
return session.profile.CanAccessClipboard
}
func (session *SessionCtx) SetProfile(profile types.MemberProfile) {
session.profile = profile
}
// ---
// runtime
// ---
func (session *SessionCtx) IsHost() bool { func (session *SessionCtx) IsHost() bool {
return session.manager.host != nil && session.manager.host.ID() == session.ID() return session.manager.host != nil && session.manager.host.ID() == session.ID()
} }
func (session *SessionCtx) VerifySecret(secret string) bool {
return session.profile.Secret == secret
}
func (session *SessionCtx) IsConnected() bool { func (session *SessionCtx) IsConnected() bool {
// TODO: Refactor. return session.websocket_connected
return session.websocket_connected// && session.webrtc_connected
} }
func (session *SessionCtx) SetWebSocketPeer(websocket_peer types.WebSocketPeer) { func (session *SessionCtx) IsReceiving() bool {
session.websocket_peer = websocket_peer return session.webrtc_connected
}
func (session *SessionCtx) SetWebSocketConnected(connected bool) {
if connected {
session.websocket_connected = true
// TODO: Refactor.
//session.manager.emmiter.Emit("websocket_connected", session)
session.manager.emmiter.Emit("connected", session)
} else {
session.websocket_connected = false
// TODO: Refactor.
//session.manager.emmiter.Emit("websocket_disconnected", session)
session.manager.emmiter.Emit("disconnected", session)
session.websocket_peer = nil
}
}
func (session *SessionCtx) SetWebRTCPeer(webrtc_peer types.WebRTCPeer) {
session.webrtc_peer = webrtc_peer
}
func (session *SessionCtx) SetWebRTCConnected(connected bool) {
if connected {
session.webrtc_connected = true
session.manager.emmiter.Emit("webrtc_connected", session)
} else {
session.webrtc_connected = false
session.manager.emmiter.Emit("webrtc_disconnected", session)
session.webrtc_peer = nil
}
} }
func (session *SessionCtx) Disconnect(reason string) error { func (session *SessionCtx) Disconnect(reason string) error {
@ -106,6 +103,25 @@ func (session *SessionCtx) Disconnect(reason string) error {
return nil return nil
} }
// ---
// webscoket
// ---
func (session *SessionCtx) SetWebSocketPeer(websocket_peer types.WebSocketPeer) {
session.websocket_peer = websocket_peer
}
func (session *SessionCtx) SetWebSocketConnected(connected bool) {
if connected {
session.websocket_connected = true
session.manager.emmiter.Emit("connected", session)
} else {
session.websocket_connected = false
session.manager.emmiter.Emit("disconnected", session)
session.websocket_peer = nil
}
}
func (session *SessionCtx) Send(v interface{}) error { func (session *SessionCtx) Send(v interface{}) error {
if session.websocket_peer == nil { if session.websocket_peer == nil {
return nil return nil
@ -114,6 +130,25 @@ func (session *SessionCtx) Send(v interface{}) error {
return session.websocket_peer.Send(v) return session.websocket_peer.Send(v)
} }
// ---
// webrtc
// ---
func (session *SessionCtx) SetWebRTCPeer(webrtc_peer types.WebRTCPeer) {
session.webrtc_peer = webrtc_peer
}
func (session *SessionCtx) SetWebRTCConnected(connected bool) {
if connected {
session.webrtc_connected = true
session.manager.emmiter.Emit("receiving_started", session)
} else {
session.webrtc_connected = false
session.manager.emmiter.Emit("receiving_stopped", session)
session.webrtc_peer = nil
}
}
func (session *SessionCtx) SignalAnswer(sdp string) error { func (session *SessionCtx) SignalAnswer(sdp string) error {
if session.webrtc_peer == nil { if session.webrtc_peer == nil {
return nil return nil

View File

@ -3,29 +3,41 @@ package types
import "net/http" import "net/http"
type MemberProfile struct { type MemberProfile struct {
ID string
Secret string Secret string
Name string Name string
IsAdmin bool IsAdmin bool
//Enabled bool CanLogin bool
//CanControl bool CanConnect bool
//CanWatch bool CanWatch bool
//ClipboardAccess bool CanHost bool
CanAccessClipboard bool
} }
type Session interface { type Session interface {
ID() string ID() string
VerifySecret(secret string) bool
Name() string Name() string
IsAdmin() bool IsAdmin() bool
CanLogin() bool
CanConnect() bool
CanWatch() bool
CanHost() bool
CanAccessClipboard() bool
SetProfile(profile MemberProfile)
IsHost() bool IsHost() bool
IsConnected() bool IsConnected() bool
VerifySecret(secret string) bool IsReceiving() bool
Disconnect(reason string) error
SetWebSocketPeer(websocket_peer WebSocketPeer) SetWebSocketPeer(websocket_peer WebSocketPeer)
SetWebSocketConnected(connected bool) SetWebSocketConnected(connected bool)
Send(v interface{}) error
SetWebRTCPeer(webrtc_peer WebRTCPeer) SetWebRTCPeer(webrtc_peer WebRTCPeer)
SetWebRTCConnected(connected bool) SetWebRTCConnected(connected bool)
Disconnect(reason string) error
Send(v interface{}) error
SignalAnswer(sdp string) error SignalAnswer(sdp string) error
} }
@ -47,8 +59,8 @@ type SessionManager interface {
OnHostCleared(listener func(session Session)) OnHostCleared(listener func(session Session))
OnConnected(listener func(session Session)) OnConnected(listener func(session Session))
OnDisconnected(listener func(session Session)) OnDisconnected(listener func(session Session))
ImplicitHosting() bool ImplicitHosting() bool
// auth
Authenticate(r *http.Request) (Session, error) Authenticate(r *http.Request) (Session, error)
} }