Store time when join room was successfull in session, not when it ended.

This commit is contained in:
Joachim Bauch 2025-12-18 10:52:05 +01:00
commit 348e7b3360
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
5 changed files with 20 additions and 14 deletions

View file

@ -329,14 +329,14 @@ func (s *ClientSession) ParsedUserData() (api.StringMap, error) {
return s.parseUserData()
}
func (s *ClientSession) SetRoom(room *Room) {
func (s *ClientSession) SetRoom(room *Room, joinTime time.Time) {
s.room.Store(room)
s.onRoomSet(room != nil)
s.onRoomSet(room != nil, joinTime)
}
func (s *ClientSession) onRoomSet(hasRoom bool) {
func (s *ClientSession) onRoomSet(hasRoom bool, joinTime time.Time) {
if hasRoom {
s.roomJoinTime.Store(time.Now().UnixNano())
s.roomJoinTime.Store(joinTime.UnixNano())
} else {
s.roomJoinTime.Store(0)
}
@ -361,7 +361,7 @@ func (s *ClientSession) SetFederationClient(federation *FederationClient) {
defer s.mu.Unlock()
s.doLeaveRoom(true)
s.onRoomSet(federation != nil)
s.onRoomSet(federation != nil, time.Now())
if prev := s.federation.Swap(federation); prev != nil && prev != federation {
prev.Close()
@ -561,7 +561,7 @@ func (s *ClientSession) doLeaveRoom(notify bool) *Room {
}
s.doUnsubscribeRoomEvents(notify)
s.SetRoom(nil)
s.SetRoom(nil, time.Time{})
s.releaseMcuObjects()
room.RemoveSession(s)
return room

11
hub.go
View file

@ -1848,8 +1848,10 @@ func (h *Hub) processRoom(sess Session, message *api.ClientMessage) {
}
var room talk.BackendClientResponse
var joinRoomTime time.Time
if session.ClientType() == api.HelloClientTypeInternal {
// Internal clients can join any room.
joinRoomTime = time.Now()
room = talk.BackendClientResponse{
Type: "room",
Room: &talk.BackendClientRoomResponse{
@ -1876,6 +1878,7 @@ func (h *Hub) processRoom(sess Session, message *api.ClientMessage) {
// TODO(jojo): Validate response
joinRoomTime = time.Now()
if message.Room.SessionId != "" {
// There can only be one connection per Nextcloud Talk session,
// disconnect any other connections without sending a "leave" event.
@ -1886,7 +1889,7 @@ func (h *Hub) processRoom(sess Session, message *api.ClientMessage) {
}
}
h.processJoinRoom(session, message, &room)
h.processJoinRoom(session, message, &room, joinRoomTime)
}
func (h *Hub) publishFederatedSessions() (int, *sync.WaitGroup) {
@ -2005,7 +2008,7 @@ func (h *Hub) createRoomLocked(id string, properties json.RawMessage, backend *t
return room, nil
}
func (h *Hub) processJoinRoom(session *ClientSession, message *api.ClientMessage, room *talk.BackendClientResponse) {
func (h *Hub) processJoinRoom(session *ClientSession, message *api.ClientMessage, room *talk.BackendClientResponse, joinTime time.Time) {
if room.Type == "error" {
session.SendMessage(message.NewErrorServerMessage(room.Error))
return
@ -2051,7 +2054,7 @@ func (h *Hub) processJoinRoom(session *ClientSession, message *api.ClientMessage
delete(h.dialoutSessions, session)
}
h.mu.Unlock()
session.SetRoom(r)
session.SetRoom(r, joinTime)
if room.Room.Permissions != nil {
session.SetPermissions(*room.Room.Permissions)
}
@ -2540,7 +2543,7 @@ func (h *Hub) processInternalMsg(sess Session, message *api.ClientMessage) {
statsHubSessionsTotal.WithLabelValues(session.Backend().Id(), string(sess.ClientType())).Inc()
h.logger.Printf("Session %s added virtual session %s with initial flags %d", session.PublicId(), sess.PublicId(), sess.Flags())
session.AddVirtualSession(sess)
sess.SetRoom(room)
sess.SetRoom(room, time.Now())
room.AddSession(sess, nil)
case "updatesession":
msg := msg.UpdateSession

View file

@ -26,6 +26,7 @@ import (
"encoding/json"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -82,7 +83,7 @@ func (s *DummySession) ParsedBackendUrl() *url.URL {
return nil
}
func (s *DummySession) SetRoom(room *Room) {
func (s *DummySession) SetRoom(room *Room, joinTime time.Time) {
}
func (s *DummySession) GetRoom() *Room {

View file

@ -26,6 +26,7 @@ import (
"encoding/json"
"net/url"
"sync"
"time"
"github.com/strukturag/nextcloud-spreed-signaling/api"
"github.com/strukturag/nextcloud-spreed-signaling/talk"
@ -46,7 +47,7 @@ type Session interface {
BackendUrl() string
ParsedBackendUrl() *url.URL
SetRoom(room *Room)
SetRoom(room *Room, joinTime time.Time)
GetRoom() *Room
LeaveRoom(notify bool) *Room
IsInRoom(id string) bool

View file

@ -27,6 +27,7 @@ import (
"errors"
"net/url"
"sync/atomic"
"time"
"github.com/strukturag/nextcloud-spreed-signaling/api"
"github.com/strukturag/nextcloud-spreed-signaling/async/events"
@ -169,7 +170,7 @@ func (s *VirtualSession) ParsedUserData() (api.StringMap, error) {
return s.parseUserData()
}
func (s *VirtualSession) SetRoom(room *Room) {
func (s *VirtualSession) SetRoom(room *Room, joinTime time.Time) {
s.room.Store(room)
if room != nil {
if err := s.hub.roomSessions.SetRoomSession(s, api.RoomSessionId(s.PublicId())); err != nil {
@ -195,7 +196,7 @@ func (s *VirtualSession) LeaveRoom(notify bool) *Room {
return nil
}
s.SetRoom(nil)
s.SetRoom(nil, time.Time{})
room.RemoveSession(s)
return room
}