From 3a2adffab67153bf7ae662ec0df4aa974cb9bcd2 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Thu, 22 Apr 2021 14:18:35 +0200 Subject: [PATCH] Get userid from room session data if present. --- clientsession.go | 10 ++++++++- hub_test.go | 10 +++++++++ room.go | 10 +++++++-- room_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 3 deletions(-) diff --git a/clientsession.go b/clientsession.go index 1b36ca6..7792ea9 100644 --- a/clientsession.go +++ b/clientsession.go @@ -240,7 +240,15 @@ func (s *ClientSession) ParsedBackendUrl() *url.URL { } func (s *ClientSession) UserId() string { - return s.userId + userId := s.userId + if userId == "" { + if room := s.GetRoom(); room != nil { + if data := room.GetRoomSessionData(s); data != nil { + userId = data.UserId + } + } + } + return userId } func (s *ClientSession) UserData() *json.RawMessage { diff --git a/hub_test.go b/hub_test.go index 90d4a36..9f2ebf4 100644 --- a/hub_test.go +++ b/hub_test.go @@ -252,6 +252,16 @@ func processRoomRequest(t *testing.T, w http.ResponseWriter, r *http.Request, re RoomId: request.Room.RoomId, }, } + if request.Room.RoomId == "test-room-with-sessiondata" { + data := map[string]string{ + "userid": "userid-from-sessiondata", + } + tmp, err := json.Marshal(data) + if err != nil { + t.Fatalf("Could not marshal %+v: %s", data, err) + } + response.Room.Session = (*json.RawMessage)(&tmp) + } return response } diff --git a/room.go b/room.go index 58c3d0d..81f188b 100644 --- a/room.go +++ b/room.go @@ -354,6 +354,12 @@ func (r *Room) UpdateProperties(properties *json.RawMessage) { r.publish(message) } +func (r *Room) GetRoomSessionData(session Session) *RoomSessionData { + r.mu.RLock() + defer r.mu.RUnlock() + return r.roomSessionData[session.PublicId()] +} + func (r *Room) PublishSessionJoined(session Session, sessionData *RoomSessionData) { sessionId := session.PublicId() if sessionId == "" { @@ -617,8 +623,8 @@ func (r *Room) publishSessionFlagsChanged(session *VirtualSession) { } func (r *Room) publishActiveSessions() { - r.mu.Lock() - defer r.mu.Unlock() + r.mu.RLock() + defer r.mu.RUnlock() entries := make(map[string][]BackendPingEntry) urls := make(map[string]*url.URL) diff --git a/room_test.go b/room_test.go index e7463ad..a5cc544 100644 --- a/room_test.go +++ b/room_test.go @@ -355,3 +355,60 @@ loop: t.Error(err) } } + +func TestRoom_RoomSessionData(t *testing.T) { + hub, _, router, server, shutdown := CreateHubForTest(t) + defer shutdown() + + config, err := getTestConfig(server) + if err != nil { + t.Fatal(err) + } + b, err := NewBackendServer(config, hub, "no-version") + if err != nil { + t.Fatal(err) + } + if err := b.Start(router); err != nil { + t.Fatal(err) + } + + client := NewTestClient(t, server, hub) + defer client.CloseWithBye() + + if err := client.SendHello(authAnonymousUserId); err != nil { + t.Fatal(err) + } + + ctx, cancel := context.WithTimeout(context.Background(), testTimeout) + defer cancel() + + hello, err := client.RunUntilHello(ctx) + if err != nil { + t.Fatal(err) + } + + // Join room by id. + roomId := "test-room-with-sessiondata" + if room, err := client.JoinRoom(ctx, roomId); err != nil { + t.Fatal(err) + } else if room.Room.RoomId != roomId { + t.Fatalf("Expected room %s, got %s", roomId, room.Room.RoomId) + } + + // We will receive a "joined" event with the userid from the room session data. + expected := "userid-from-sessiondata" + if message, err := client.RunUntilMessage(ctx); err != nil { + t.Error(err) + } else if err := client.checkMessageJoinedSession(message, hello.Hello.SessionId, expected); err != nil { + t.Error(err) + } + + session := hub.GetSessionByPublicId(hello.Hello.SessionId) + if session == nil { + t.Fatalf("Could not find session %s", hello.Hello.SessionId) + } + + if userid := session.UserId(); userid != expected { + t.Errorf("Expected userid %s, got %s", expected, userid) + } +}