From a6fbafc0792ee6187020edfc1e4ef0e191185a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 5 Dec 2020 22:18:45 +0100 Subject: [PATCH] SetProfile changed as Update. --- internal/api/members/crud.go | 7 +++++-- internal/session/manager.go | 15 +++++++++++++++ internal/session/session.go | 5 ----- internal/types/session.go | 2 +- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/internal/api/members/crud.go b/internal/api/members/crud.go index 66033c50..4be4cb89 100644 --- a/internal/api/members/crud.go +++ b/internal/api/members/crud.go @@ -84,7 +84,7 @@ func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) { // TODO: Join structs? // TODO: Update independent props. - member.SetProfile(types.MemberProfile{ + if err := h.sessions.Update(member.ID(), types.MemberProfile{ Secret: data.Secret, Name: data.Name, IsAdmin: data.IsAdmin, @@ -93,7 +93,10 @@ func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) { CanWatch: data.CanWatch, CanHost: data.CanHost, CanAccessClipboard: data.CanAccessClipboard, - }) + }); err != nil { + utils.HttpInternalServerError(w, err) + return + } utils.HttpSuccess(w) } diff --git a/internal/session/manager.go b/internal/session/manager.go index 21a1bcad..eafbb472 100644 --- a/internal/session/manager.go +++ b/internal/session/manager.go @@ -67,6 +67,21 @@ func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) return session } +func (manager *SessionManagerCtx) Update(id string, profile types.MemberProfile) error { + manager.membersMu.Lock() + session, ok := manager.members[id] + if !ok { + manager.membersMu.Unlock() + return fmt.Errorf("Member not found.") + } + + session.profile = profile + manager.membersMu.Unlock() + + manager.emmiter.Emit("profile_changed", session) + return nil +} + func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) { manager.membersMu.Lock() session, ok := manager.members[id] diff --git a/internal/session/session.go b/internal/session/session.go index 9d6107f6..1542fb3b 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -59,11 +59,6 @@ func (session *SessionCtx) CanAccessClipboard() bool { return session.profile.CanAccessClipboard } -func (session *SessionCtx) SetProfile(profile types.MemberProfile) { - session.profile = profile - session.manager.emmiter.Emit("profile_changed", session) -} - // --- // runtime // --- diff --git a/internal/types/session.go b/internal/types/session.go index 635e755d..28085d82 100644 --- a/internal/types/session.go +++ b/internal/types/session.go @@ -25,7 +25,6 @@ type Session interface { CanWatch() bool CanHost() bool CanAccessClipboard() bool - SetProfile(profile MemberProfile) IsHost() bool IsConnected() bool @@ -43,6 +42,7 @@ type Session interface { type SessionManager interface { Create(id string, profile MemberProfile) Session + Update(id string, profile MemberProfile) error Get(id string) (Session, bool) Delete(id string) error