mirror of https://github.com/jellyfin/jellyfin.git
Rename GroupController into Group
This commit is contained in:
parent
7169c0a22d
commit
b7eb4da04e
|
@ -1,12 +1,9 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Jellyfin.Data.Entities;
|
using Jellyfin.Data.Entities;
|
||||||
using Jellyfin.Data.Enums;
|
|
||||||
using MediaBrowser.Controller.Entities;
|
|
||||||
using MediaBrowser.Controller.Library;
|
using MediaBrowser.Controller.Library;
|
||||||
using MediaBrowser.Controller.Session;
|
using MediaBrowser.Controller.Session;
|
||||||
using MediaBrowser.Controller.SyncPlay;
|
using MediaBrowser.Controller.SyncPlay;
|
||||||
|
@ -19,17 +16,17 @@ using Microsoft.Extensions.Logging;
|
||||||
namespace Emby.Server.Implementations.SyncPlay
|
namespace Emby.Server.Implementations.SyncPlay
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class GroupController.
|
/// Class Group.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// Class is not thread-safe, external locking is required when accessing methods.
|
/// Class is not thread-safe, external locking is required when accessing methods.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public class GroupController : IGroupController, IGroupStateContext
|
public class Group : IGroupStateContext
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The logger.
|
/// The logger.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly ILogger<GroupController> _logger;
|
private readonly ILogger<Group> _logger;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The logger factory.
|
/// The logger factory.
|
||||||
|
@ -63,13 +60,13 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
private IGroupState _state;
|
private IGroupState _state;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="GroupController" /> class.
|
/// Initializes a new instance of the <see cref="Group" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="loggerFactory">The logger factory.</param>
|
/// <param name="loggerFactory">The logger factory.</param>
|
||||||
/// <param name="userManager">The user manager.</param>
|
/// <param name="userManager">The user manager.</param>
|
||||||
/// <param name="sessionManager">The session manager.</param>
|
/// <param name="sessionManager">The session manager.</param>
|
||||||
/// <param name="libraryManager">The library manager.</param>
|
/// <param name="libraryManager">The library manager.</param>
|
||||||
public GroupController(
|
public Group(
|
||||||
ILoggerFactory loggerFactory,
|
ILoggerFactory loggerFactory,
|
||||||
IUserManager userManager,
|
IUserManager userManager,
|
||||||
ISessionManager sessionManager,
|
ISessionManager sessionManager,
|
||||||
|
@ -79,7 +76,7 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
_sessionManager = sessionManager;
|
_sessionManager = sessionManager;
|
||||||
_libraryManager = libraryManager;
|
_libraryManager = libraryManager;
|
||||||
_logger = loggerFactory.CreateLogger<GroupController>();
|
_logger = loggerFactory.CreateLogger<Group>();
|
||||||
|
|
||||||
_state = new IdleGroupState(loggerFactory);
|
_state = new IdleGroupState(loggerFactory);
|
||||||
}
|
}
|
||||||
|
@ -235,10 +232,18 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
return !usersWithNoAccess.Any();
|
return !usersWithNoAccess.Any();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <summary>
|
||||||
|
/// Checks if the group is empty.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns><c>true</c> if the group is empty, <c>false</c> otherwise.</returns>
|
||||||
public bool IsGroupEmpty() => _participants.Count == 0;
|
public bool IsGroupEmpty() => _participants.Count == 0;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <summary>
|
||||||
|
/// Initializes the group with the session's info.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="session">The session.</param>
|
||||||
|
/// <param name="request">The request.</param>
|
||||||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
public void CreateGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken)
|
public void CreateGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
GroupName = request.GroupName;
|
GroupName = request.GroupName;
|
||||||
|
@ -273,7 +278,12 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
_logger.LogInformation("Session {SessionId} created group {GroupId}.", session.Id, GroupId.ToString());
|
_logger.LogInformation("Session {SessionId} created group {GroupId}.", session.Id, GroupId.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <summary>
|
||||||
|
/// Adds the session to the group.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="session">The session.</param>
|
||||||
|
/// <param name="request">The request.</param>
|
||||||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
public void SessionJoin(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
|
public void SessionJoin(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
AddSession(session);
|
AddSession(session);
|
||||||
|
@ -289,21 +299,12 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
_logger.LogInformation("Session {SessionId} joined group {GroupId}.", session.Id, GroupId.ToString());
|
_logger.LogInformation("Session {SessionId} joined group {GroupId}.", session.Id, GroupId.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <summary>
|
||||||
public void SessionRestore(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
|
/// Removes the session from the group.
|
||||||
{
|
/// </summary>
|
||||||
var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupJoined, GetInfo());
|
/// <param name="session">The session.</param>
|
||||||
SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
|
/// <param name="request">The request.</param>
|
||||||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserJoined, session.UserName);
|
|
||||||
SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
|
|
||||||
|
|
||||||
_state.SessionJoined(this, _state.Type, session, cancellationToken);
|
|
||||||
|
|
||||||
_logger.LogInformation("Session {SessionId} re-joined group {GroupId}.", session.Id, GroupId.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public void SessionLeave(SessionInfo session, LeaveGroupRequest request, CancellationToken cancellationToken)
|
public void SessionLeave(SessionInfo session, LeaveGroupRequest request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_state.SessionLeaving(this, _state.Type, session, cancellationToken);
|
_state.SessionLeaving(this, _state.Type, session, cancellationToken);
|
||||||
|
@ -319,7 +320,12 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
_logger.LogInformation("Session {SessionId} left group {GroupId}.", session.Id, GroupId.ToString());
|
_logger.LogInformation("Session {SessionId} left group {GroupId}.", session.Id, GroupId.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <summary>
|
||||||
|
/// Handles the requested action by the session.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="session">The session.</param>
|
||||||
|
/// <param name="request">The requested action.</param>
|
||||||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
public void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToken)
|
public void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
// The server's job is to maintain a consistent state for clients to reference
|
// The server's job is to maintain a consistent state for clients to reference
|
||||||
|
@ -329,14 +335,21 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
request.Apply(this, _state, session, cancellationToken);
|
request.Apply(this, _state, session, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <summary>
|
||||||
|
/// Gets the info about the group for the clients.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The group info for the clients.</returns>
|
||||||
public GroupInfoDto GetInfo()
|
public GroupInfoDto GetInfo()
|
||||||
{
|
{
|
||||||
var participants = _participants.Values.Select(session => session.Session.UserName).Distinct().ToList();
|
var participants = _participants.Values.Select(session => session.Session.UserName).Distinct().ToList();
|
||||||
return new GroupInfoDto(GroupId, GroupName, _state.Type, participants, DateTime.UtcNow);
|
return new GroupInfoDto(GroupId, GroupName, _state.Type, participants, DateTime.UtcNow);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <summary>
|
||||||
|
/// Checks if a user has access to all content in the play queue.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="user">The user.</param>
|
||||||
|
/// <returns><c>true</c> if the user can access the play queue; <c>false</c> otherwise.</returns>
|
||||||
public bool HasAccessToPlayQueue(User user)
|
public bool HasAccessToPlayQueue(User user)
|
||||||
{
|
{
|
||||||
var items = PlayQueue.GetPlaylist().Select(item => item.ItemId).ToList();
|
var items = PlayQueue.GetPlaylist().Select(item => item.ItemId).ToList();
|
|
@ -44,20 +44,20 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The map between sessions and groups.
|
/// The map between sessions and groups.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly Dictionary<string, IGroupController> _sessionToGroupMap =
|
private readonly Dictionary<string, Group> _sessionToGroupMap =
|
||||||
new Dictionary<string, IGroupController>(StringComparer.OrdinalIgnoreCase);
|
new Dictionary<string, Group>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The groups.
|
/// The groups.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly Dictionary<Guid, IGroupController> _groups =
|
private readonly Dictionary<Guid, Group> _groups =
|
||||||
new Dictionary<Guid, IGroupController>();
|
new Dictionary<Guid, Group>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Lock used for accessing any group.
|
/// Lock used for accessing any group.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// Always lock before <see cref="_mapsLock"/> and before locking on any <see cref="IGroupController"/>.
|
/// Always lock before <see cref="_mapsLock"/> and before locking on any <see cref="Group"/>.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
private readonly object _groupsLock = new object();
|
private readonly object _groupsLock = new object();
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
/// Lock used for accessing the session-to-group map.
|
/// Lock used for accessing the session-to-group map.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// Always lock after <see cref="_groupsLock"/> and before locking on any <see cref="IGroupController"/>.
|
/// Always lock after <see cref="_groupsLock"/> and before locking on any <see cref="Group"/>.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
private readonly object _mapsLock = new object();
|
private readonly object _mapsLock = new object();
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
LeaveGroup(session, leaveGroupRequest, cancellationToken);
|
LeaveGroup(session, leaveGroupRequest, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
var group = new GroupController(_loggerFactory, _userManager, _sessionManager, _libraryManager);
|
var group = new Group(_loggerFactory, _userManager, _sessionManager, _libraryManager);
|
||||||
_groups[group.GroupId] = group;
|
_groups[group.GroupId] = group;
|
||||||
|
|
||||||
AddSessionToGroup(session, group);
|
AddSessionToGroup(session, group);
|
||||||
|
@ -132,7 +132,7 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
// Locking required to access list of groups.
|
// Locking required to access list of groups.
|
||||||
lock (_groupsLock)
|
lock (_groupsLock)
|
||||||
{
|
{
|
||||||
_groups.TryGetValue(request.GroupId, out IGroupController group);
|
_groups.TryGetValue(request.GroupId, out Group group);
|
||||||
|
|
||||||
if (group == null)
|
if (group == null)
|
||||||
{
|
{
|
||||||
|
@ -162,7 +162,8 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
{
|
{
|
||||||
if (FindJoinedGroupId(session).Equals(request.GroupId))
|
if (FindJoinedGroupId(session).Equals(request.GroupId))
|
||||||
{
|
{
|
||||||
group.SessionRestore(session, request, cancellationToken);
|
// Restore session.
|
||||||
|
group.SessionJoin(session, request, cancellationToken);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,7 +241,7 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToken)
|
public void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
IGroupController group;
|
Group group;
|
||||||
lock (_mapsLock)
|
lock (_mapsLock)
|
||||||
{
|
{
|
||||||
group = FindJoinedGroup(session);
|
group = FindJoinedGroup(session);
|
||||||
|
@ -255,7 +256,7 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group lock required as GroupController is not thread-safe.
|
// Group lock required as Group is not thread-safe.
|
||||||
lock (group)
|
lock (group)
|
||||||
{
|
{
|
||||||
group.HandleRequest(session, request, cancellationToken);
|
group.HandleRequest(session, request, cancellationToken);
|
||||||
|
@ -317,7 +318,7 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="session">The session.</param>
|
/// <param name="session">The session.</param>
|
||||||
/// <returns>The group.</returns>
|
/// <returns>The group.</returns>
|
||||||
private IGroupController FindJoinedGroup(SessionInfo session)
|
private Group FindJoinedGroup(SessionInfo session)
|
||||||
{
|
{
|
||||||
_sessionToGroupMap.TryGetValue(session.Id, out var group);
|
_sessionToGroupMap.TryGetValue(session.Id, out var group);
|
||||||
return group;
|
return group;
|
||||||
|
@ -345,7 +346,7 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
/// <param name="session">The session.</param>
|
/// <param name="session">The session.</param>
|
||||||
/// <param name="group">The group.</param>
|
/// <param name="group">The group.</param>
|
||||||
/// <exception cref="InvalidOperationException">Thrown when the user is in another group already.</exception>
|
/// <exception cref="InvalidOperationException">Thrown when the user is in another group already.</exception>
|
||||||
private void AddSessionToGroup(SessionInfo session, IGroupController group)
|
private void AddSessionToGroup(SessionInfo session, Group group)
|
||||||
{
|
{
|
||||||
if (session == null)
|
if (session == null)
|
||||||
{
|
{
|
||||||
|
@ -369,7 +370,7 @@ namespace Emby.Server.Implementations.SyncPlay
|
||||||
/// <param name="session">The session.</param>
|
/// <param name="session">The session.</param>
|
||||||
/// <param name="group">The group.</param>
|
/// <param name="group">The group.</param>
|
||||||
/// <exception cref="InvalidOperationException">Thrown when the user is not found in the specified group.</exception>
|
/// <exception cref="InvalidOperationException">Thrown when the user is not found in the specified group.</exception>
|
||||||
private void RemoveSessionFromGroup(SessionInfo session, IGroupController group)
|
private void RemoveSessionFromGroup(SessionInfo session, Group group)
|
||||||
{
|
{
|
||||||
if (session == null)
|
if (session == null)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,87 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Threading;
|
|
||||||
using Jellyfin.Data.Entities;
|
|
||||||
using MediaBrowser.Controller.Session;
|
|
||||||
using MediaBrowser.Controller.SyncPlay.Queue;
|
|
||||||
using MediaBrowser.Controller.SyncPlay.Requests;
|
|
||||||
using MediaBrowser.Model.SyncPlay;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Controller.SyncPlay
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Interface IGroupController.
|
|
||||||
/// </summary>
|
|
||||||
public interface IGroupController
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the group identifier.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The group identifier.</value>
|
|
||||||
Guid GroupId { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the play queue.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The play queue.</value>
|
|
||||||
PlayQueueManager PlayQueue { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if the group is empty.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns><c>true</c> if the group is empty, <c>false</c> otherwise.</returns>
|
|
||||||
bool IsGroupEmpty();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes the group with the session's info.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="session">The session.</param>
|
|
||||||
/// <param name="request">The request.</param>
|
|
||||||
/// <param name="cancellationToken">The cancellation token.</param>
|
|
||||||
void CreateGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds the session to the group.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="session">The session.</param>
|
|
||||||
/// <param name="request">The request.</param>
|
|
||||||
/// <param name="cancellationToken">The cancellation token.</param>
|
|
||||||
void SessionJoin(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Restores the state of a session that already joined the group.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="session">The session.</param>
|
|
||||||
/// <param name="request">The request.</param>
|
|
||||||
/// <param name="cancellationToken">The cancellation token.</param>
|
|
||||||
void SessionRestore(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Removes the session from the group.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="session">The session.</param>
|
|
||||||
/// <param name="request">The request.</param>
|
|
||||||
/// <param name="cancellationToken">The cancellation token.</param>
|
|
||||||
void SessionLeave(SessionInfo session, LeaveGroupRequest request, CancellationToken cancellationToken);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Handles the requested action by the session.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="session">The session.</param>
|
|
||||||
/// <param name="request">The requested action.</param>
|
|
||||||
/// <param name="cancellationToken">The cancellation token.</param>
|
|
||||||
void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToken);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the info about the group for the clients.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The group info for the clients.</returns>
|
|
||||||
GroupInfoDto GetInfo();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if a user has access to all content in the play queue.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="user">The user.</param>
|
|
||||||
/// <returns><c>true</c> if the user can access the play queue; <c>false</c> otherwise.</returns>
|
|
||||||
bool HasAccessToPlayQueue(User user);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue