jellyfin/Emby.Server.Implementations/SyncPlay/SyncPlayManager.cs

374 lines
12 KiB
C#
Raw Normal View History

2020-04-01 15:52:42 +00:00
using System;
using System.Collections.Generic;
2020-04-04 22:50:57 +00:00
using System.Globalization;
2020-04-01 15:52:42 +00:00
using System.Linq;
using System.Threading;
2020-05-27 00:52:05 +00:00
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
2020-04-01 15:52:42 +00:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Session;
2020-05-06 21:42:53 +00:00
using MediaBrowser.Controller.SyncPlay;
using MediaBrowser.Model.SyncPlay;
2020-05-27 00:52:05 +00:00
using Microsoft.Extensions.Logging;
2020-04-01 15:52:42 +00:00
2020-05-06 21:42:53 +00:00
namespace Emby.Server.Implementations.SyncPlay
2020-04-01 15:52:42 +00:00
{
/// <summary>
2020-05-06 21:42:53 +00:00
/// Class SyncPlayManager.
2020-04-01 15:52:42 +00:00
/// </summary>
2020-05-06 21:42:53 +00:00
public class SyncPlayManager : ISyncPlayManager, IDisposable
2020-04-01 15:52:42 +00:00
{
/// <summary>
/// The logger.
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// The user manager.
/// </summary>
private readonly IUserManager _userManager;
2020-04-01 15:52:42 +00:00
/// <summary>
/// The session manager.
/// </summary>
private readonly ISessionManager _sessionManager;
2020-04-04 22:50:57 +00:00
/// <summary>
/// The library manager.
/// </summary>
private readonly ILibraryManager _libraryManager;
2020-04-01 15:52:42 +00:00
/// <summary>
2020-04-04 15:56:21 +00:00
/// The map between sessions and groups.
2020-04-01 15:52:42 +00:00
/// </summary>
2020-05-06 21:42:53 +00:00
private readonly Dictionary<string, ISyncPlayController> _sessionToGroupMap =
new Dictionary<string, ISyncPlayController>(StringComparer.OrdinalIgnoreCase);
2020-04-01 15:52:42 +00:00
/// <summary>
/// The groups.
/// </summary>
2020-05-09 12:34:07 +00:00
private readonly Dictionary<Guid, ISyncPlayController> _groups =
new Dictionary<Guid, ISyncPlayController>();
2020-04-28 12:12:06 +00:00
/// <summary>
/// Lock used for accesing any group.
/// </summary>
private readonly object _groupsLock = new object();
2020-04-01 15:52:42 +00:00
private bool _disposed = false;
2020-05-06 21:42:53 +00:00
public SyncPlayManager(
ILogger<SyncPlayManager> logger,
IUserManager userManager,
2020-04-04 22:50:57 +00:00
ISessionManager sessionManager,
ILibraryManager libraryManager)
2020-04-01 15:52:42 +00:00
{
_logger = logger;
_userManager = userManager;
2020-04-01 15:52:42 +00:00
_sessionManager = sessionManager;
2020-04-04 22:50:57 +00:00
_libraryManager = libraryManager;
2020-04-01 15:52:42 +00:00
2020-04-21 21:37:37 +00:00
_sessionManager.SessionEnded += OnSessionManagerSessionEnded;
_sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped;
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Gets all groups.
/// </summary>
/// <value>All groups.</value>
2020-05-06 21:42:53 +00:00
public IEnumerable<ISyncPlayController> Groups => _groups.Values;
2020-04-01 15:52:42 +00:00
/// <inheritdoc />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and optionally managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
2020-04-21 21:37:37 +00:00
_sessionManager.SessionEnded -= OnSessionManagerSessionEnded;
_sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped;
2020-04-01 15:52:42 +00:00
_disposed = true;
}
2020-04-21 21:37:37 +00:00
private void OnSessionManagerSessionEnded(object sender, SessionEventArgs e)
2020-04-01 15:52:42 +00:00
{
2020-04-04 15:56:21 +00:00
var session = e.SessionInfo;
2020-05-09 12:34:07 +00:00
if (!IsSessionInGroup(session))
{
return;
}
2020-05-04 17:46:02 +00:00
LeaveGroup(session, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
2020-04-21 21:37:37 +00:00
private void OnSessionManagerPlaybackStopped(object sender, PlaybackStopEventArgs e)
2020-04-01 15:52:42 +00:00
{
2020-04-04 15:56:21 +00:00
var session = e.Session;
2020-05-09 12:34:07 +00:00
if (!IsSessionInGroup(session))
{
return;
}
2020-05-04 17:46:02 +00:00
LeaveGroup(session, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
2020-04-04 15:56:21 +00:00
private bool IsSessionInGroup(SessionInfo session)
2020-04-01 15:52:42 +00:00
{
2020-04-04 15:56:21 +00:00
return _sessionToGroupMap.ContainsKey(session.Id);
2020-04-01 15:52:42 +00:00
}
2020-04-04 22:50:57 +00:00
private bool HasAccessToItem(User user, Guid itemId)
{
2020-04-05 07:28:55 +00:00
var item = _libraryManager.GetItemById(itemId);
2020-05-04 17:46:02 +00:00
// Check ParentalRating access
var hasParentalRatingAccess = !user.MaxParentalAgeRating.HasValue
|| item.InheritedParentalRatingValue <= user.MaxParentalAgeRating;
2020-04-05 07:28:55 +00:00
2020-05-27 00:52:05 +00:00
if (!user.HasPermission(PermissionKind.EnableAllFolders) && hasParentalRatingAccess)
2020-04-04 22:50:57 +00:00
{
var collections = _libraryManager.GetCollectionFolders(item).Select(
2020-05-27 00:52:05 +00:00
folder => folder.Id.ToString("N", CultureInfo.InvariantCulture));
return collections.Intersect(user.GetPreference(PreferenceKind.EnabledFolders)).Any();
2020-04-04 22:50:57 +00:00
}
2020-05-27 00:52:05 +00:00
return hasParentalRatingAccess;
2020-04-04 22:50:57 +00:00
}
2020-04-04 15:56:21 +00:00
private Guid? GetSessionGroup(SessionInfo session)
2020-04-01 15:52:42 +00:00
{
2020-05-27 00:52:05 +00:00
_sessionToGroupMap.TryGetValue(session.Id, out var group);
return group?.GetGroupId();
2020-04-01 15:52:42 +00:00
}
/// <inheritdoc />
2020-05-04 17:46:02 +00:00
public void NewGroup(SessionInfo session, CancellationToken cancellationToken)
2020-04-01 15:52:42 +00:00
{
var user = _userManager.GetUserById(session.UserId);
2020-05-27 00:52:05 +00:00
if (user.SyncPlayAccess != SyncPlayAccess.CreateAndJoinGroups)
2020-04-01 15:52:42 +00:00
{
2020-05-04 17:46:02 +00:00
_logger.LogWarning("NewGroup: {0} does not have permission to create groups.", session.Id);
2020-04-22 20:05:53 +00:00
var error = new GroupUpdate<string>()
{
Type = GroupUpdateType.CreateGroupDenied
};
2020-05-27 00:52:05 +00:00
_sessionManager.SendSyncPlayGroupUpdate(session.Id, error, CancellationToken.None);
2020-04-22 20:05:53 +00:00
return;
}
2020-04-28 12:12:06 +00:00
lock (_groupsLock)
{
2020-04-28 12:12:06 +00:00
if (IsSessionInGroup(session))
{
2020-05-04 17:46:02 +00:00
LeaveGroup(session, cancellationToken);
2020-04-28 12:12:06 +00:00
}
2020-04-01 15:52:42 +00:00
2020-05-06 21:42:53 +00:00
var group = new SyncPlayController(_sessionManager, this);
2020-05-09 12:34:07 +00:00
_groups[group.GetGroupId()] = group;
2020-04-01 15:52:42 +00:00
2020-05-04 17:46:02 +00:00
group.InitGroup(session, cancellationToken);
2020-04-28 12:12:06 +00:00
}
2020-04-01 15:52:42 +00:00
}
/// <inheritdoc />
2020-05-09 12:34:07 +00:00
public void JoinGroup(SessionInfo session, Guid groupId, JoinGroupRequest request, CancellationToken cancellationToken)
2020-04-01 15:52:42 +00:00
{
var user = _userManager.GetUserById(session.UserId);
2020-05-27 00:52:05 +00:00
if (user.SyncPlayAccess == SyncPlayAccess.None)
{
2020-05-06 21:42:53 +00:00
_logger.LogWarning("JoinGroup: {0} does not have access to SyncPlay.", session.Id);
2020-04-22 20:05:53 +00:00
var error = new GroupUpdate<string>()
{
Type = GroupUpdateType.JoinGroupDenied
};
2020-05-27 00:52:05 +00:00
_sessionManager.SendSyncPlayGroupUpdate(session.Id, error, CancellationToken.None);
2020-04-22 20:05:53 +00:00
return;
}
2020-04-28 12:12:06 +00:00
lock (_groupsLock)
2020-04-01 15:52:42 +00:00
{
2020-05-06 21:42:53 +00:00
ISyncPlayController group;
2020-04-28 12:12:06 +00:00
_groups.TryGetValue(groupId, out group);
2020-04-01 15:52:42 +00:00
2020-04-28 12:12:06 +00:00
if (group == null)
2020-04-22 20:05:53 +00:00
{
2020-05-04 17:46:02 +00:00
_logger.LogWarning("JoinGroup: {0} tried to join group {0} that does not exist.", session.Id, groupId);
2020-04-04 22:50:57 +00:00
2020-04-28 12:12:06 +00:00
var error = new GroupUpdate<string>()
{
2020-05-04 17:46:02 +00:00
Type = GroupUpdateType.GroupDoesNotExist
2020-04-28 12:12:06 +00:00
};
2020-05-27 00:52:05 +00:00
_sessionManager.SendSyncPlayGroupUpdate(session.Id, error, CancellationToken.None);
2020-04-28 12:12:06 +00:00
return;
}
2020-04-22 20:05:53 +00:00
2020-04-28 12:12:06 +00:00
if (!HasAccessToItem(user, group.GetPlayingItemId()))
2020-04-22 20:05:53 +00:00
{
2020-05-04 17:46:02 +00:00
_logger.LogWarning("JoinGroup: {0} does not have access to {1}.", session.Id, group.GetPlayingItemId());
2020-04-28 12:12:06 +00:00
var error = new GroupUpdate<string>()
{
GroupId = group.GetGroupId().ToString(),
Type = GroupUpdateType.LibraryAccessDenied
};
2020-05-27 00:52:05 +00:00
_sessionManager.SendSyncPlayGroupUpdate(session.Id, error, CancellationToken.None);
2020-04-28 12:12:06 +00:00
return;
}
if (IsSessionInGroup(session))
{
2020-05-09 12:34:07 +00:00
if (GetSessionGroup(session).Equals(groupId))
{
return;
}
2020-05-04 17:46:02 +00:00
LeaveGroup(session, cancellationToken);
2020-04-28 12:12:06 +00:00
}
2020-04-04 22:50:57 +00:00
2020-05-04 17:46:02 +00:00
group.SessionJoin(session, request, cancellationToken);
2020-04-21 21:37:37 +00:00
}
2020-04-01 15:52:42 +00:00
}
/// <inheritdoc />
2020-05-04 17:46:02 +00:00
public void LeaveGroup(SessionInfo session, CancellationToken cancellationToken)
2020-04-01 15:52:42 +00:00
{
2020-04-21 21:37:37 +00:00
// TODO: determine what happens to users that are in a group and get their permissions revoked
2020-04-28 12:12:06 +00:00
lock (_groupsLock)
2020-04-01 15:52:42 +00:00
{
_sessionToGroupMap.TryGetValue(session.Id, out var group);
2020-04-01 15:52:42 +00:00
2020-04-28 12:12:06 +00:00
if (group == null)
2020-04-22 20:05:53 +00:00
{
2020-05-04 17:46:02 +00:00
_logger.LogWarning("LeaveGroup: {0} does not belong to any group.", session.Id);
2020-04-01 15:52:42 +00:00
2020-04-28 12:12:06 +00:00
var error = new GroupUpdate<string>()
{
Type = GroupUpdateType.NotInGroup
};
2020-05-27 00:52:05 +00:00
_sessionManager.SendSyncPlayGroupUpdate(session.Id, error, CancellationToken.None);
2020-04-28 12:12:06 +00:00
return;
}
2020-05-04 17:46:02 +00:00
group.SessionLeave(session, cancellationToken);
2020-04-28 12:12:06 +00:00
if (group.IsGroupEmpty())
{
2020-05-04 17:46:02 +00:00
_logger.LogInformation("LeaveGroup: removing empty group {0}.", group.GetGroupId());
2020-05-09 12:34:07 +00:00
_groups.Remove(group.GetGroupId(), out _);
2020-04-28 12:12:06 +00:00
}
2020-04-01 15:52:42 +00:00
}
}
/// <inheritdoc />
2020-05-04 17:46:02 +00:00
public List<GroupInfoView> ListGroups(SessionInfo session, Guid filterItemId)
2020-04-01 15:52:42 +00:00
{
var user = _userManager.GetUserById(session.UserId);
2020-05-27 00:52:05 +00:00
if (user.SyncPlayAccess == SyncPlayAccess.None)
{
return new List<GroupInfoView>();
}
2020-05-04 17:46:02 +00:00
// Filter by item if requested
if (!filterItemId.Equals(Guid.Empty))
2020-04-01 15:52:42 +00:00
{
return _groups.Values.Where(
2020-05-04 17:46:02 +00:00
group => group.GetPlayingItemId().Equals(filterItemId) && HasAccessToItem(user, group.GetPlayingItemId())
2020-04-01 15:52:42 +00:00
).Select(
group => group.GetInfo()
).ToList();
}
// Otherwise show all available groups
else
{
2020-04-04 22:50:57 +00:00
return _groups.Values.Where(
group => HasAccessToItem(user, group.GetPlayingItemId())
).Select(
2020-04-01 15:52:42 +00:00
group => group.GetInfo()
).ToList();
}
}
/// <inheritdoc />
2020-05-04 17:46:02 +00:00
public void HandleRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
2020-04-01 15:52:42 +00:00
{
var user = _userManager.GetUserById(session.UserId);
2020-05-27 00:52:05 +00:00
if (user.SyncPlayAccess == SyncPlayAccess.None)
{
2020-05-06 21:42:53 +00:00
_logger.LogWarning("HandleRequest: {0} does not have access to SyncPlay.", session.Id);
2020-04-22 20:05:53 +00:00
var error = new GroupUpdate<string>()
{
Type = GroupUpdateType.JoinGroupDenied
};
2020-05-27 00:52:05 +00:00
_sessionManager.SendSyncPlayGroupUpdate(session.Id, error, CancellationToken.None);
2020-04-22 20:05:53 +00:00
return;
}
2020-04-28 12:12:06 +00:00
lock (_groupsLock)
2020-04-01 15:52:42 +00:00
{
_sessionToGroupMap.TryGetValue(session.Id, out var group);
2020-04-01 15:52:42 +00:00
2020-04-28 12:12:06 +00:00
if (group == null)
2020-04-22 20:05:53 +00:00
{
2020-05-04 17:46:02 +00:00
_logger.LogWarning("HandleRequest: {0} does not belong to any group.", session.Id);
2020-04-28 12:12:06 +00:00
var error = new GroupUpdate<string>()
{
Type = GroupUpdateType.NotInGroup
};
_sessionManager.SendSyncPlayGroupUpdate(session.Id, error, CancellationToken.None);
2020-04-28 12:12:06 +00:00
return;
}
2020-05-04 17:46:02 +00:00
group.HandleRequest(session, request, cancellationToken);
2020-04-01 15:52:42 +00:00
}
}
2020-04-01 15:52:42 +00:00
/// <inheritdoc />
2020-05-06 21:42:53 +00:00
public void AddSessionToGroup(SessionInfo session, ISyncPlayController group)
2020-04-01 15:52:42 +00:00
{
2020-04-04 15:56:21 +00:00
if (IsSessionInGroup(session))
2020-04-01 15:52:42 +00:00
{
2020-04-04 15:56:21 +00:00
throw new InvalidOperationException("Session in other group already!");
2020-04-01 15:52:42 +00:00
}
2020-05-26 09:37:52 +00:00
2020-04-04 15:56:21 +00:00
_sessionToGroupMap[session.Id] = group;
2020-04-01 15:52:42 +00:00
}
/// <inheritdoc />
2020-05-06 21:42:53 +00:00
public void RemoveSessionFromGroup(SessionInfo session, ISyncPlayController group)
2020-04-01 15:52:42 +00:00
{
2020-04-04 15:56:21 +00:00
if (!IsSessionInGroup(session))
2020-04-01 15:52:42 +00:00
{
2020-04-04 15:56:21 +00:00
throw new InvalidOperationException("Session not in any group!");
2020-04-01 15:52:42 +00:00
}
_sessionToGroupMap.Remove(session.Id, out var tempGroup);
2020-04-01 15:52:42 +00:00
if (!tempGroup.GetGroupId().Equals(group.GetGroupId()))
{
2020-04-04 15:56:21 +00:00
throw new InvalidOperationException("Session was in wrong group!");
2020-04-01 15:52:42 +00:00
}
}
}
}