2020-04-01 08:52:42 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
2020-04-04 15:50:57 -07:00
|
|
|
using System.Globalization;
|
2020-04-01 08:52:42 -07:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-04-04 15:50:57 -07:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2020-04-01 08:52:42 -07:00
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
using MediaBrowser.Controller.Syncplay;
|
2020-04-04 08:59:16 -07:00
|
|
|
using MediaBrowser.Model.Configuration;
|
2020-04-01 08:52:42 -07:00
|
|
|
using MediaBrowser.Model.Syncplay;
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.Syncplay
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class SyncplayManager.
|
|
|
|
/// </summary>
|
|
|
|
public class SyncplayManager : ISyncplayManager, IDisposable
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The logger.
|
|
|
|
/// </summary>
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
2020-04-04 08:59:16 -07:00
|
|
|
/// <summary>
|
|
|
|
/// The user manager.
|
|
|
|
/// </summary>
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
2020-04-01 08:52:42 -07:00
|
|
|
/// <summary>
|
|
|
|
/// The session manager.
|
|
|
|
/// </summary>
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
2020-04-04 15:50:57 -07:00
|
|
|
/// <summary>
|
|
|
|
/// The library manager.
|
|
|
|
/// </summary>
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
2020-04-01 08:52:42 -07:00
|
|
|
/// <summary>
|
2020-04-04 08:56:21 -07:00
|
|
|
/// The map between sessions and groups.
|
2020-04-01 08:52:42 -07:00
|
|
|
/// </summary>
|
2020-04-04 08:56:21 -07:00
|
|
|
private readonly ConcurrentDictionary<string, ISyncplayController> _sessionToGroupMap =
|
2020-04-01 08:52:42 -07:00
|
|
|
new ConcurrentDictionary<string, ISyncplayController>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The groups.
|
|
|
|
/// </summary>
|
|
|
|
private readonly ConcurrentDictionary<string, ISyncplayController> _groups =
|
|
|
|
new ConcurrentDictionary<string, ISyncplayController>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
private bool _disposed = false;
|
|
|
|
|
|
|
|
public SyncplayManager(
|
|
|
|
ILogger<SyncplayManager> logger,
|
2020-04-04 08:59:16 -07:00
|
|
|
IUserManager userManager,
|
2020-04-04 15:50:57 -07:00
|
|
|
ISessionManager sessionManager,
|
|
|
|
ILibraryManager libraryManager)
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2020-04-04 08:59:16 -07:00
|
|
|
_userManager = userManager;
|
2020-04-01 08:52:42 -07:00
|
|
|
_sessionManager = sessionManager;
|
2020-04-04 15:50:57 -07:00
|
|
|
_libraryManager = libraryManager;
|
2020-04-01 08:52:42 -07:00
|
|
|
|
|
|
|
_sessionManager.SessionEnded += _sessionManager_SessionEnded;
|
|
|
|
_sessionManager.PlaybackStopped += _sessionManager_PlaybackStopped;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets all groups.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>All groups.</value>
|
|
|
|
public IEnumerable<ISyncplayController> Groups => _groups.Values;
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
}
|
|
|
|
|
|
|
|
_sessionManager.SessionEnded -= _sessionManager_SessionEnded;
|
|
|
|
_sessionManager.PlaybackStopped -= _sessionManager_PlaybackStopped;
|
|
|
|
|
|
|
|
_disposed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CheckDisposed()
|
|
|
|
{
|
|
|
|
if (_disposed)
|
|
|
|
{
|
|
|
|
throw new ObjectDisposedException(GetType().Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _sessionManager_SessionEnded(object sender, SessionEventArgs e)
|
|
|
|
{
|
2020-04-04 08:56:21 -07:00
|
|
|
var session = e.SessionInfo;
|
|
|
|
if (!IsSessionInGroup(session)) return;
|
|
|
|
LeaveGroup(session);
|
2020-04-01 08:52:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void _sessionManager_PlaybackStopped(object sender, PlaybackStopEventArgs e)
|
|
|
|
{
|
2020-04-04 08:56:21 -07:00
|
|
|
var session = e.Session;
|
|
|
|
if (!IsSessionInGroup(session)) return;
|
|
|
|
LeaveGroup(session);
|
2020-04-01 08:52:42 -07:00
|
|
|
}
|
|
|
|
|
2020-04-04 08:56:21 -07:00
|
|
|
private bool IsSessionInGroup(SessionInfo session)
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
2020-04-04 08:56:21 -07:00
|
|
|
return _sessionToGroupMap.ContainsKey(session.Id);
|
2020-04-01 08:52:42 -07:00
|
|
|
}
|
|
|
|
|
2020-04-04 15:50:57 -07:00
|
|
|
private bool HasAccessToItem(User user, Guid itemId)
|
|
|
|
{
|
2020-04-05 00:28:55 -07:00
|
|
|
var item = _libraryManager.GetItemById(itemId);
|
|
|
|
var hasParentalRatingAccess = user.Policy.MaxParentalRating.HasValue ? item.InheritedParentalRatingValue <= user.Policy.MaxParentalRating : true;
|
|
|
|
|
2020-04-04 15:50:57 -07:00
|
|
|
if (!user.Policy.EnableAllFolders)
|
|
|
|
{
|
|
|
|
var collections = _libraryManager.GetCollectionFolders(item).Select(
|
|
|
|
folder => folder.Id.ToString("N", CultureInfo.InvariantCulture)
|
|
|
|
);
|
|
|
|
var intersect = collections.Intersect(user.Policy.EnabledFolders);
|
2020-04-05 00:28:55 -07:00
|
|
|
return intersect.Count() > 0 && hasParentalRatingAccess;
|
2020-04-04 15:50:57 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-05 00:28:55 -07:00
|
|
|
return hasParentalRatingAccess;
|
2020-04-04 15:50:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 08:56:21 -07:00
|
|
|
private Guid? GetSessionGroup(SessionInfo session)
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
|
|
|
ISyncplayController group;
|
2020-04-04 08:56:21 -07:00
|
|
|
_sessionToGroupMap.TryGetValue(session.Id, out group);
|
2020-04-01 08:52:42 -07:00
|
|
|
if (group != null)
|
|
|
|
{
|
|
|
|
return group.GetGroupId();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-04-04 08:56:21 -07:00
|
|
|
public void NewGroup(SessionInfo session)
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
2020-04-04 08:59:16 -07:00
|
|
|
var user = _userManager.GetUserById(session.UserId);
|
|
|
|
|
|
|
|
if (user.Policy.SyncplayAccess != SyncplayAccess.CreateAndJoinGroups)
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
2020-04-04 08:59:16 -07:00
|
|
|
// TODO: shall an error message be sent back to the client?
|
2020-04-15 09:03:58 -07:00
|
|
|
throw new ArgumentException("User does not have permission to create groups");
|
2020-04-04 08:59:16 -07:00
|
|
|
}
|
|
|
|
|
2020-04-04 08:56:21 -07:00
|
|
|
if (IsSessionInGroup(session))
|
2020-04-04 08:59:16 -07:00
|
|
|
{
|
2020-04-04 08:56:21 -07:00
|
|
|
LeaveGroup(session);
|
2020-04-01 08:52:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var group = new SyncplayController(_logger, _sessionManager, this);
|
|
|
|
_groups[group.GetGroupId().ToString()] = group;
|
|
|
|
|
2020-04-04 08:56:21 -07:00
|
|
|
group.InitGroup(session);
|
2020-04-01 08:52:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-04-15 09:03:58 -07:00
|
|
|
public void JoinGroup(SessionInfo session, string groupId, JoinGroupRequest request)
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
2020-04-04 08:59:16 -07:00
|
|
|
var user = _userManager.GetUserById(session.UserId);
|
|
|
|
|
|
|
|
if (user.Policy.SyncplayAccess == SyncplayAccess.None)
|
|
|
|
{
|
|
|
|
// TODO: shall an error message be sent back to the client?
|
2020-04-15 09:03:58 -07:00
|
|
|
throw new ArgumentException("User does not have access to syncplay");
|
2020-04-04 08:59:16 -07:00
|
|
|
}
|
|
|
|
|
2020-04-04 08:56:21 -07:00
|
|
|
if (IsSessionInGroup(session))
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
2020-04-04 08:56:21 -07:00
|
|
|
if (GetSessionGroup(session).Equals(groupId)) return;
|
|
|
|
LeaveGroup(session);
|
2020-04-01 08:52:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ISyncplayController group;
|
|
|
|
_groups.TryGetValue(groupId, out group);
|
|
|
|
|
|
|
|
if (group == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Syncplaymanager JoinGroup: " + groupId + " does not exist.");
|
|
|
|
|
2020-04-15 09:03:58 -07:00
|
|
|
var update = new GroupUpdate<string>();
|
|
|
|
update.Type = GroupUpdateType.NotInGroup;
|
2020-04-04 08:56:21 -07:00
|
|
|
_sessionManager.SendSyncplayGroupUpdate(session.Id.ToString(), update, CancellationToken.None);
|
2020-04-01 08:52:42 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-04-04 15:50:57 -07:00
|
|
|
|
|
|
|
if (!HasAccessToItem(user, group.GetPlayingItemId()))
|
|
|
|
{
|
2020-04-15 09:03:58 -07:00
|
|
|
throw new ArgumentException("User does not have access to playing item");
|
2020-04-04 15:50:57 -07:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:03:58 -07:00
|
|
|
group.SessionJoin(session, request);
|
2020-04-01 08:52:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-04-04 08:56:21 -07:00
|
|
|
public void LeaveGroup(SessionInfo session)
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
2020-04-04 08:59:16 -07:00
|
|
|
// TODO: what happens to users that are in a group and get their permissions revoked?
|
|
|
|
|
2020-04-01 08:52:42 -07:00
|
|
|
ISyncplayController group;
|
2020-04-04 08:56:21 -07:00
|
|
|
_sessionToGroupMap.TryGetValue(session.Id, out group);
|
2020-04-01 08:52:42 -07:00
|
|
|
|
|
|
|
if (group == null)
|
|
|
|
{
|
2020-04-04 08:56:21 -07:00
|
|
|
_logger.LogWarning("Syncplaymanager HandleRequest: " + session.Id + " not in group.");
|
2020-04-01 08:52:42 -07:00
|
|
|
|
2020-04-15 09:03:58 -07:00
|
|
|
var update = new GroupUpdate<string>();
|
|
|
|
update.Type = GroupUpdateType.NotInGroup;
|
2020-04-04 08:56:21 -07:00
|
|
|
_sessionManager.SendSyncplayGroupUpdate(session.Id.ToString(), update, CancellationToken.None);
|
2020-04-01 08:52:42 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-04-04 08:56:21 -07:00
|
|
|
group.SessionLeave(session);
|
2020-04-01 08:52:42 -07:00
|
|
|
|
|
|
|
if (group.IsGroupEmpty())
|
|
|
|
{
|
|
|
|
_groups.Remove(group.GetGroupId().ToString(), out _);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-04-04 08:56:21 -07:00
|
|
|
public List<GroupInfoView> ListGroups(SessionInfo session)
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
2020-04-04 08:59:16 -07:00
|
|
|
var user = _userManager.GetUserById(session.UserId);
|
|
|
|
|
|
|
|
if (user.Policy.SyncplayAccess == SyncplayAccess.None)
|
|
|
|
{
|
|
|
|
return new List<GroupInfoView>();
|
|
|
|
}
|
|
|
|
|
2020-04-01 08:52:42 -07:00
|
|
|
// Filter by playing item if the user is viewing something already
|
2020-04-04 08:56:21 -07:00
|
|
|
if (session.NowPlayingItem != null)
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
|
|
|
return _groups.Values.Where(
|
2020-04-04 15:50:57 -07:00
|
|
|
group => HasAccessToItem(user, group.GetPlayingItemId())
|
|
|
|
).Where(
|
2020-04-04 08:56:21 -07:00
|
|
|
group => group.GetPlayingItemId().Equals(session.FullNowPlayingItem.Id)
|
2020-04-01 08:52:42 -07:00
|
|
|
).Select(
|
|
|
|
group => group.GetInfo()
|
|
|
|
).ToList();
|
|
|
|
}
|
|
|
|
// Otherwise show all available groups
|
|
|
|
else
|
|
|
|
{
|
2020-04-04 15:50:57 -07:00
|
|
|
return _groups.Values.Where(
|
|
|
|
group => HasAccessToItem(user, group.GetPlayingItemId())
|
|
|
|
).Select(
|
2020-04-01 08:52:42 -07:00
|
|
|
group => group.GetInfo()
|
|
|
|
).ToList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-04-15 09:03:58 -07:00
|
|
|
public void HandleRequest(SessionInfo session, PlaybackRequest request)
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
2020-04-04 08:59:16 -07:00
|
|
|
var user = _userManager.GetUserById(session.UserId);
|
|
|
|
|
|
|
|
if (user.Policy.SyncplayAccess == SyncplayAccess.None)
|
|
|
|
{
|
|
|
|
// TODO: same as LeaveGroup
|
2020-04-15 09:03:58 -07:00
|
|
|
throw new ArgumentException("User does not have access to syncplay");
|
2020-04-04 08:59:16 -07:00
|
|
|
}
|
|
|
|
|
2020-04-01 08:52:42 -07:00
|
|
|
ISyncplayController group;
|
2020-04-04 08:56:21 -07:00
|
|
|
_sessionToGroupMap.TryGetValue(session.Id, out group);
|
2020-04-01 08:52:42 -07:00
|
|
|
|
|
|
|
if (group == null)
|
|
|
|
{
|
2020-04-04 08:56:21 -07:00
|
|
|
_logger.LogWarning("Syncplaymanager HandleRequest: " + session.Id + " not in group.");
|
2020-04-01 08:52:42 -07:00
|
|
|
|
2020-04-15 09:03:58 -07:00
|
|
|
var update = new GroupUpdate<string>();
|
|
|
|
update.Type = GroupUpdateType.NotInGroup;
|
2020-04-04 08:56:21 -07:00
|
|
|
_sessionManager.SendSyncplayGroupUpdate(session.Id.ToString(), update, CancellationToken.None);
|
2020-04-01 08:52:42 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-04-04 08:56:21 -07:00
|
|
|
group.HandleRequest(session, request);
|
2020-04-01 08:52:42 -07:00
|
|
|
}
|
2020-04-15 09:03:58 -07:00
|
|
|
|
2020-04-01 08:52:42 -07:00
|
|
|
/// <inheritdoc />
|
2020-04-04 08:56:21 -07:00
|
|
|
public void MapSessionToGroup(SessionInfo session, ISyncplayController group)
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
2020-04-04 08:56:21 -07:00
|
|
|
if (IsSessionInGroup(session))
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
2020-04-04 08:56:21 -07:00
|
|
|
throw new InvalidOperationException("Session in other group already!");
|
2020-04-01 08:52:42 -07:00
|
|
|
}
|
2020-04-04 08:56:21 -07:00
|
|
|
_sessionToGroupMap[session.Id] = group;
|
2020-04-01 08:52:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-04-04 08:56:21 -07:00
|
|
|
public void UnmapSessionFromGroup(SessionInfo session, ISyncplayController group)
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
2020-04-04 08:56:21 -07:00
|
|
|
if (!IsSessionInGroup(session))
|
2020-04-01 08:52:42 -07:00
|
|
|
{
|
2020-04-04 08:56:21 -07:00
|
|
|
throw new InvalidOperationException("Session not in any group!");
|
2020-04-01 08:52:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ISyncplayController tempGroup;
|
2020-04-04 08:56:21 -07:00
|
|
|
_sessionToGroupMap.Remove(session.Id, out tempGroup);
|
2020-04-01 08:52:42 -07:00
|
|
|
|
|
|
|
if (!tempGroup.GetGroupId().Equals(group.GetGroupId()))
|
|
|
|
{
|
2020-04-04 08:56:21 -07:00
|
|
|
throw new InvalidOperationException("Session was in wrong group!");
|
2020-04-01 08:52:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|