Get userid from room session data if present.

This commit is contained in:
Joachim Bauch 2021-04-22 14:18:35 +02:00
parent 047e6655a1
commit 3a2adffab6
No known key found for this signature in database
GPG Key ID: 77C1D22D53E15F02
4 changed files with 84 additions and 3 deletions

View File

@ -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 {

View File

@ -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
}

10
room.go
View File

@ -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)

View File

@ -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)
}
}