mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-02-24 21:42:25 +00:00
Merge pull request #4716 from OancaAndrei/syncplay-new-auth-policies
(cherry picked from commit 4f6a585424)
Signed-off-by: Joshua M. Boniface <joshua@boniface.me>
This commit is contained in:
@@ -41,6 +41,12 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||
/// </summary>
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
|
||||
/// <summary>
|
||||
/// The map between users and counter of active sessions.
|
||||
/// </summary>
|
||||
private readonly ConcurrentDictionary<Guid, int> _activeUsers =
|
||||
new ConcurrentDictionary<Guid, int>();
|
||||
|
||||
/// <summary>
|
||||
/// The map between sessions and groups.
|
||||
/// </summary>
|
||||
@@ -122,6 +128,7 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||
throw new InvalidOperationException("Could not add session to group!");
|
||||
}
|
||||
|
||||
UpdateSessionsCounter(session.UserId, 1);
|
||||
group.CreateGroup(session, request, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -172,6 +179,7 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||
if (existingGroup.GroupId.Equals(request.GroupId))
|
||||
{
|
||||
// Restore session.
|
||||
UpdateSessionsCounter(session.UserId, 1);
|
||||
group.SessionJoin(session, request, cancellationToken);
|
||||
return;
|
||||
}
|
||||
@@ -185,6 +193,7 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||
throw new InvalidOperationException("Could not add session to group!");
|
||||
}
|
||||
|
||||
UpdateSessionsCounter(session.UserId, 1);
|
||||
group.SessionJoin(session, request, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -223,6 +232,7 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||
throw new InvalidOperationException("Could not remove session from group!");
|
||||
}
|
||||
|
||||
UpdateSessionsCounter(session.UserId, -1);
|
||||
group.SessionLeave(session, request, cancellationToken);
|
||||
|
||||
if (group.IsGroupEmpty())
|
||||
@@ -318,6 +328,19 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsUserActive(Guid userId)
|
||||
{
|
||||
if (_activeUsers.TryGetValue(userId, out var sessionsCounter))
|
||||
{
|
||||
return sessionsCounter > 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases unmanaged and optionally managed resources.
|
||||
/// </summary>
|
||||
@@ -343,5 +366,26 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||
JoinGroup(session, request, CancellationToken.None);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateSessionsCounter(Guid userId, int toAdd)
|
||||
{
|
||||
// Update sessions counter.
|
||||
var newSessionsCounter = _activeUsers.AddOrUpdate(
|
||||
userId,
|
||||
1,
|
||||
(key, sessionsCounter) => sessionsCounter + toAdd);
|
||||
|
||||
// Should never happen.
|
||||
if (newSessionsCounter < 0)
|
||||
{
|
||||
throw new InvalidOperationException("Sessions counter is negative!");
|
||||
}
|
||||
|
||||
// Clean record if user has no more active sessions.
|
||||
if (newSessionsCounter == 0)
|
||||
{
|
||||
_activeUsers.TryRemove(new KeyValuePair<Guid, int>(userId, newSessionsCounter));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user