2014-07-04 22:21:13 -07:00
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Library ;
using MediaBrowser.Controller.Net ;
using MediaBrowser.Controller.Session ;
using MediaBrowser.Model.Dto ;
using MediaBrowser.Model.Session ;
using ServiceStack ;
using System ;
using System.Globalization ;
using System.Linq ;
using System.Threading.Tasks ;
namespace MediaBrowser.Api.UserLibrary
{
/// <summary>
/// Class MarkPlayedItem
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/PlayedItems/{Id}", "POST", Summary = "Marks an item as played")]
2014-07-04 22:21:13 -07:00
public class MarkPlayedItem : IReturn < UserItemDataDto >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2015-05-29 16:51:33 -07:00
public string UserId { get ; set ; }
2014-07-04 22:21:13 -07:00
[ApiMember(Name = "DatePlayed", Description = "The date the item was played (if any). Format = yyyyMMddHHmmss", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string DatePlayed { get ; set ; }
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Id { get ; set ; }
}
/// <summary>
/// Class MarkUnplayedItem
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/PlayedItems/{Id}", "DELETE", Summary = "Marks an item as unplayed")]
2014-07-04 22:21:13 -07:00
public class MarkUnplayedItem : IReturn < UserItemDataDto >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
2015-05-29 16:51:33 -07:00
public string UserId { get ; set ; }
2014-07-04 22:21:13 -07:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Id { get ; set ; }
}
2014-11-14 19:31:03 -07:00
[Route("/Sessions/Playing", "POST", Summary = "Reports playback has started within a session")]
2014-07-04 22:21:13 -07:00
public class ReportPlaybackStart : PlaybackStartInfo , IReturnVoid
{
}
2014-11-14 19:31:03 -07:00
[Route("/Sessions/Playing/Progress", "POST", Summary = "Reports playback progress within a session")]
2014-07-04 22:21:13 -07:00
public class ReportPlaybackProgress : PlaybackProgressInfo , IReturnVoid
{
}
2015-04-16 20:31:19 -07:00
[Route("/Sessions/Playing/Ping", "POST", Summary = "Pings a playback session")]
public class PingPlaybackSession : IReturnVoid
{
[ApiMember(Name = "PlaySessionId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string PlaySessionId { get ; set ; }
}
2014-11-14 19:31:03 -07:00
[Route("/Sessions/Playing/Stopped", "POST", Summary = "Reports playback has stopped within a session")]
2014-07-04 22:21:13 -07:00
public class ReportPlaybackStopped : PlaybackStopInfo , IReturnVoid
{
}
/// <summary>
/// Class OnPlaybackStart
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/PlayingItems/{Id}", "POST", Summary = "Reports that a user has begun playing an item")]
2014-07-04 22:21:13 -07:00
public class OnPlaybackStart : IReturnVoid
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2015-05-29 16:51:33 -07:00
public string UserId { get ; set ; }
2014-07-04 22:21:13 -07:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Id { get ; set ; }
[ApiMember(Name = "MediaSourceId", Description = "The id of the MediaSource", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string MediaSourceId { get ; set ; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="UpdateUserItemRating" /> is likes.
/// </summary>
/// <value><c>true</c> if likes; otherwise, <c>false</c>.</value>
[ApiMember(Name = "CanSeek", Description = "Indicates if the client can seek", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "POST")]
public bool CanSeek { get ; set ; }
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "QueueableMediaTypes", Description = "A list of media types that can be queued from this item, comma delimited. Audio,Video,Book,Game", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
public string QueueableMediaTypes { get ; set ; }
[ApiMember(Name = "AudioStreamIndex", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
public int? AudioStreamIndex { get ; set ; }
[ApiMember(Name = "SubtitleStreamIndex", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
public int? SubtitleStreamIndex { get ; set ; }
2015-04-13 12:14:37 -07:00
[ApiMember(Name = "PlayMethod", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public PlayMethod PlayMethod { get ; set ; }
[ApiMember(Name = "LiveStreamId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string LiveStreamId { get ; set ; }
[ApiMember(Name = "PlaySessionId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string PlaySessionId { get ; set ; }
2014-07-04 22:21:13 -07:00
}
/// <summary>
/// Class OnPlaybackProgress
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/PlayingItems/{Id}/Progress", "POST", Summary = "Reports a user's playback progress")]
2014-07-04 22:21:13 -07:00
public class OnPlaybackProgress : IReturnVoid
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2015-05-29 16:51:33 -07:00
public string UserId { get ; set ; }
2014-07-04 22:21:13 -07:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Id { get ; set ; }
[ApiMember(Name = "MediaSourceId", Description = "The id of the MediaSource", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string MediaSourceId { get ; set ; }
/// <summary>
/// Gets or sets the position ticks.
/// </summary>
/// <value>The position ticks.</value>
[ApiMember(Name = "PositionTicks", Description = "Optional. The current position, in ticks. 1 tick = 10000 ms", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
public long? PositionTicks { get ; set ; }
[ApiMember(Name = "IsPaused", Description = "Indicates if the player is paused.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "POST")]
public bool IsPaused { get ; set ; }
[ApiMember(Name = "IsMuted", Description = "Indicates if the player is muted.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "POST")]
public bool IsMuted { get ; set ; }
[ApiMember(Name = "AudioStreamIndex", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
public int? AudioStreamIndex { get ; set ; }
[ApiMember(Name = "SubtitleStreamIndex", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
public int? SubtitleStreamIndex { get ; set ; }
[ApiMember(Name = "VolumeLevel", Description = "Scale of 0-100", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
public int? VolumeLevel { get ; set ; }
2015-04-13 12:14:37 -07:00
[ApiMember(Name = "PlayMethod", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public PlayMethod PlayMethod { get ; set ; }
[ApiMember(Name = "LiveStreamId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string LiveStreamId { get ; set ; }
[ApiMember(Name = "PlaySessionId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string PlaySessionId { get ; set ; }
2015-07-30 07:34:46 -07:00
[ApiMember(Name = "RepeatMode", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public RepeatMode RepeatMode { get ; set ; }
2014-07-04 22:21:13 -07:00
}
/// <summary>
/// Class OnPlaybackStopped
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/PlayingItems/{Id}", "DELETE", Summary = "Reports that a user has stopped playing an item")]
2014-07-04 22:21:13 -07:00
public class OnPlaybackStopped : IReturnVoid
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
2015-05-29 16:51:33 -07:00
public string UserId { get ; set ; }
2014-07-04 22:21:13 -07:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Id { get ; set ; }
[ApiMember(Name = "MediaSourceId", Description = "The id of the MediaSource", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string MediaSourceId { get ; set ; }
/// <summary>
/// Gets or sets the position ticks.
/// </summary>
/// <value>The position ticks.</value>
[ApiMember(Name = "PositionTicks", Description = "Optional. The position, in ticks, where playback stopped. 1 tick = 10000 ms", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "DELETE")]
public long? PositionTicks { get ; set ; }
2015-04-13 12:14:37 -07:00
[ApiMember(Name = "LiveStreamId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string LiveStreamId { get ; set ; }
[ApiMember(Name = "PlaySessionId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string PlaySessionId { get ; set ; }
2014-07-04 22:21:13 -07:00
}
[Authenticated]
public class PlaystateService : BaseApiService
{
private readonly IUserManager _userManager ;
private readonly IUserDataManager _userDataRepository ;
private readonly ILibraryManager _libraryManager ;
private readonly ISessionManager _sessionManager ;
public PlaystateService ( IUserManager userManager , IUserDataManager userDataRepository , ILibraryManager libraryManager , ISessionManager sessionManager )
{
_userManager = userManager ;
_userDataRepository = userDataRepository ;
_libraryManager = libraryManager ;
_sessionManager = sessionManager ;
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2014-08-18 07:20:02 -07:00
public object Post ( MarkPlayedItem request )
{
var result = MarkPlayed ( request ) . Result ;
return ToOptimizedResult ( result ) ;
}
private async Task < UserItemDataDto > MarkPlayed ( MarkPlayedItem request )
2014-07-04 22:21:13 -07:00
{
var user = _userManager . GetUserById ( request . UserId ) ;
DateTime ? datePlayed = null ;
if ( ! string . IsNullOrEmpty ( request . DatePlayed ) )
{
datePlayed = DateTime . ParseExact ( request . DatePlayed , "yyyyMMddHHmmss" , CultureInfo . InvariantCulture , DateTimeStyles . AssumeUniversal ) ;
}
2015-03-09 12:40:03 -07:00
var session = await GetSession ( ) . ConfigureAwait ( false ) ;
2014-07-04 22:21:13 -07:00
var dto = await UpdatePlayedStatus ( user , request . Id , true , datePlayed ) . ConfigureAwait ( false ) ;
foreach ( var additionalUserInfo in session . AdditionalUsers )
{
2014-09-14 08:10:51 -07:00
var additionalUser = _userManager . GetUserById ( additionalUserInfo . UserId ) ;
2014-07-04 22:21:13 -07:00
await UpdatePlayedStatus ( additionalUser , request . Id , true , datePlayed ) . ConfigureAwait ( false ) ;
}
2014-08-18 07:20:02 -07:00
return dto ;
2014-07-04 22:21:13 -07:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2014-08-18 07:20:02 -07:00
public void Post ( OnPlaybackStart request )
2014-07-04 22:21:13 -07:00
{
var queueableMediaTypes = ( request . QueueableMediaTypes ? ? string . Empty ) ;
2014-08-18 07:20:02 -07:00
Post ( new ReportPlaybackStart
2014-07-04 22:21:13 -07:00
{
CanSeek = request . CanSeek ,
ItemId = request . Id ,
QueueableMediaTypes = queueableMediaTypes . Split ( ',' ) . ToList ( ) ,
MediaSourceId = request . MediaSourceId ,
AudioStreamIndex = request . AudioStreamIndex ,
2015-04-13 12:14:37 -07:00
SubtitleStreamIndex = request . SubtitleStreamIndex ,
PlayMethod = request . PlayMethod ,
PlaySessionId = request . PlaySessionId ,
LiveStreamId = request . LiveStreamId
2014-07-04 22:21:13 -07:00
} ) ;
}
2014-08-18 07:20:02 -07:00
public void Post ( ReportPlaybackStart request )
2014-07-04 22:21:13 -07:00
{
2015-03-09 12:40:03 -07:00
request . SessionId = GetSession ( ) . Result . Id ;
2014-07-04 22:21:13 -07:00
2014-08-18 07:20:02 -07:00
var task = _sessionManager . OnPlaybackStart ( request ) ;
Task . WaitAll ( task ) ;
2014-07-04 22:21:13 -07:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2014-08-18 07:20:02 -07:00
public void Post ( OnPlaybackProgress request )
2014-07-04 22:21:13 -07:00
{
2014-08-18 07:20:02 -07:00
Post ( new ReportPlaybackProgress
2014-07-04 22:21:13 -07:00
{
ItemId = request . Id ,
PositionTicks = request . PositionTicks ,
IsMuted = request . IsMuted ,
IsPaused = request . IsPaused ,
MediaSourceId = request . MediaSourceId ,
AudioStreamIndex = request . AudioStreamIndex ,
SubtitleStreamIndex = request . SubtitleStreamIndex ,
2015-04-13 12:14:37 -07:00
VolumeLevel = request . VolumeLevel ,
PlayMethod = request . PlayMethod ,
PlaySessionId = request . PlaySessionId ,
2015-07-30 07:34:46 -07:00
LiveStreamId = request . LiveStreamId ,
RepeatMode = request . RepeatMode
2014-07-04 22:21:13 -07:00
} ) ;
}
2014-08-18 07:20:02 -07:00
public void Post ( ReportPlaybackProgress request )
2014-07-04 22:21:13 -07:00
{
2015-04-01 18:34:14 -07:00
if ( ! string . IsNullOrWhiteSpace ( request . PlaySessionId ) )
{
2015-04-13 12:14:37 -07:00
ApiEntryPoint . Instance . PingTranscodingJob ( request . PlaySessionId ) ;
2015-04-01 18:34:14 -07:00
}
2015-03-09 12:40:03 -07:00
request . SessionId = GetSession ( ) . Result . Id ;
2014-07-04 22:21:13 -07:00
2014-08-18 07:20:02 -07:00
var task = _sessionManager . OnPlaybackProgress ( request ) ;
Task . WaitAll ( task ) ;
2014-07-04 22:21:13 -07:00
}
2015-04-16 20:31:19 -07:00
public void Post ( PingPlaybackSession request )
{
ApiEntryPoint . Instance . PingTranscodingJob ( request . PlaySessionId ) ;
}
2014-07-04 22:21:13 -07:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2014-08-18 07:20:02 -07:00
public void Delete ( OnPlaybackStopped request )
2014-07-04 22:21:13 -07:00
{
2014-08-18 07:20:02 -07:00
Post ( new ReportPlaybackStopped
2014-07-04 22:21:13 -07:00
{
ItemId = request . Id ,
PositionTicks = request . PositionTicks ,
2015-04-13 12:14:37 -07:00
MediaSourceId = request . MediaSourceId ,
PlaySessionId = request . PlaySessionId ,
LiveStreamId = request . LiveStreamId
2014-07-04 22:21:13 -07:00
} ) ;
}
2014-08-18 07:20:02 -07:00
public void Post ( ReportPlaybackStopped request )
2014-07-04 22:21:13 -07:00
{
2016-01-27 23:39:02 -07:00
Logger . Debug ( "ReportPlaybackStopped PlaySessionId: {0}" , request . PlaySessionId ? ? string . Empty ) ;
2015-04-01 18:34:14 -07:00
if ( ! string . IsNullOrWhiteSpace ( request . PlaySessionId ) )
{
ApiEntryPoint . Instance . KillTranscodingJobs ( AuthorizationContext . GetAuthorizationInfo ( Request ) . DeviceId , request . PlaySessionId , s = > true ) ;
}
2015-03-09 12:40:03 -07:00
request . SessionId = GetSession ( ) . Result . Id ;
2014-07-04 22:21:13 -07:00
2014-08-18 07:20:02 -07:00
var task = _sessionManager . OnPlaybackStopped ( request ) ;
Task . WaitAll ( task ) ;
2014-07-04 22:21:13 -07:00
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
public object Delete ( MarkUnplayedItem request )
{
var task = MarkUnplayed ( request ) ;
return ToOptimizedResult ( task . Result ) ;
}
private async Task < UserItemDataDto > MarkUnplayed ( MarkUnplayedItem request )
{
var user = _userManager . GetUserById ( request . UserId ) ;
2015-03-09 12:40:03 -07:00
var session = await GetSession ( ) . ConfigureAwait ( false ) ;
2014-07-04 22:21:13 -07:00
var dto = await UpdatePlayedStatus ( user , request . Id , false , null ) . ConfigureAwait ( false ) ;
foreach ( var additionalUserInfo in session . AdditionalUsers )
{
2014-09-14 08:10:51 -07:00
var additionalUser = _userManager . GetUserById ( additionalUserInfo . UserId ) ;
2014-07-04 22:21:13 -07:00
await UpdatePlayedStatus ( additionalUser , request . Id , false , null ) . ConfigureAwait ( false ) ;
}
return dto ;
}
/// <summary>
/// Updates the played status.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="itemId">The item id.</param>
/// <param name="wasPlayed">if set to <c>true</c> [was played].</param>
/// <param name="datePlayed">The date played.</param>
/// <returns>Task.</returns>
private async Task < UserItemDataDto > UpdatePlayedStatus ( User user , string itemId , bool wasPlayed , DateTime ? datePlayed )
{
var item = _libraryManager . GetItemById ( itemId ) ;
if ( wasPlayed )
{
2014-10-25 11:32:58 -07:00
await item . MarkPlayed ( user , datePlayed , true ) . ConfigureAwait ( false ) ;
2014-07-04 22:21:13 -07:00
}
else
{
2014-10-25 11:32:58 -07:00
await item . MarkUnplayed ( user ) . ConfigureAwait ( false ) ;
2014-07-04 22:21:13 -07:00
}
return _userDataRepository . GetUserDataDto ( item , user ) ;
}
}
2014-08-18 07:20:02 -07:00
}