Skip to content

Commit

Permalink
Update UserSessionTracker.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Nov 3, 2024
1 parent 5137cbd commit 9668700
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/Server.UI/Services/Fusion/UserSessionTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ public virtual async Task AddUserSession(string pageComponent, CancellationToken
return;

var sessionInfo = await GetSessionInfo().ConfigureAwait(false);
ImmutableInterlocked.AddOrUpdate(
ref _pageUserSessions,
pageComponent,
ImmutableHashSet.Create(sessionInfo),
(key, existingSessions) => existingSessions.Add(sessionInfo));

using var invalidating = Invalidation.Begin();
_ = await GetUserSessions(pageComponent, cancellationToken).ConfigureAwait(false);
if (sessionInfo != null)
{
ImmutableInterlocked.AddOrUpdate(
ref _pageUserSessions,
pageComponent,
ImmutableHashSet.Create(sessionInfo),
(key, existingSessions) => existingSessions.Add(sessionInfo));

using var invalidating = Invalidation.Begin();
_ = await GetUserSessions(pageComponent, cancellationToken).ConfigureAwait(false);
}
}

/// <summary>
Expand Down Expand Up @@ -69,8 +72,8 @@ public virtual async Task RemoveUserSession(string pageComponent, CancellationTo
return;

var sessionInfo = await GetSessionInfo().ConfigureAwait(false);

if (_pageUserSessions.TryGetValue(pageComponent, out var users) && users.Contains(sessionInfo))
if (sessionInfo!=null && _pageUserSessions.TryGetValue(pageComponent, out var users) && users.Contains(sessionInfo))
{
var updatedUsers = users.Remove(sessionInfo);

Expand Down Expand Up @@ -132,21 +135,22 @@ public virtual Task RemoveAllSessions(string userId, CancellationToken cancellat
/// </summary>
/// <returns>The session information.</returns>
/// <exception cref="InvalidOperationException">Thrown when the HTTP context is not available.</exception>
private Task<SessionInfo> GetSessionInfo()
private Task<SessionInfo?> GetSessionInfo()
{
if (_httpContextAccessor.HttpContext != null && (_httpContextAccessor.HttpContext.User.Identity?.IsAuthenticated??false))
if (_httpContextAccessor.HttpContext != null && (_httpContextAccessor.HttpContext.User.Identity?.IsAuthenticated ?? false))
{
var httpUser = _httpContextAccessor.HttpContext?.User;
var ipAddress = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress?.ToString();
var userId = _httpContextAccessor.HttpContext?.User?.GetUserId();
var userName = _httpContextAccessor.HttpContext?.User?.GetUserName();
var displayName = _httpContextAccessor.HttpContext?.User?.GetDisplayName();
var tenantId = _httpContextAccessor.HttpContext?.User?.GetTenantId();
return Task.FromResult(new SessionInfo(userId, userName, displayName, ipAddress, tenantId, "", UserPresence.Available));
}
else
{
return Task.FromResult(new SessionInfo("","","","","","", UserPresence.Statusunknown));

if (userId != null && userName != null && displayName != null && ipAddress != null && tenantId != null)
{
return Task.FromResult<SessionInfo?>(new SessionInfo(userId, userName, displayName, ipAddress, tenantId, "", UserPresence.Available));
}
}
return Task.FromResult<SessionInfo?>(null);
}
}

0 comments on commit 9668700

Please sign in to comment.