PlaystateService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Net;
  4. using MediaBrowser.Controller.Session;
  5. using MediaBrowser.Model.Dto;
  6. using MediaBrowser.Model.Session;
  7. using ServiceStack;
  8. using System;
  9. using System.Globalization;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Api.UserLibrary
  13. {
  14. /// <summary>
  15. /// Class MarkPlayedItem
  16. /// </summary>
  17. [Route("/Users/{UserId}/PlayedItems/{Id}", "POST")]
  18. [Api(Description = "Marks an item as played")]
  19. public class MarkPlayedItem : IReturn<UserItemDataDto>
  20. {
  21. /// <summary>
  22. /// Gets or sets the user id.
  23. /// </summary>
  24. /// <value>The user id.</value>
  25. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  26. public Guid UserId { get; set; }
  27. [ApiMember(Name = "DatePlayed", Description = "The date the item was played (if any). Format = yyyyMMddHHmmss", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  28. public string DatePlayed { get; set; }
  29. /// <summary>
  30. /// Gets or sets the id.
  31. /// </summary>
  32. /// <value>The id.</value>
  33. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  34. public string Id { get; set; }
  35. }
  36. /// <summary>
  37. /// Class MarkUnplayedItem
  38. /// </summary>
  39. [Route("/Users/{UserId}/PlayedItems/{Id}", "DELETE")]
  40. [Api(Description = "Marks an item as unplayed")]
  41. public class MarkUnplayedItem : IReturn<UserItemDataDto>
  42. {
  43. /// <summary>
  44. /// Gets or sets the user id.
  45. /// </summary>
  46. /// <value>The user id.</value>
  47. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  48. public Guid UserId { get; set; }
  49. /// <summary>
  50. /// Gets or sets the id.
  51. /// </summary>
  52. /// <value>The id.</value>
  53. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  54. public string Id { get; set; }
  55. }
  56. [Route("/Sessions/Playing", "POST")]
  57. [Api(Description = "Reports playback has started within a session")]
  58. public class ReportPlaybackStart : PlaybackStartInfo, IReturnVoid
  59. {
  60. }
  61. [Route("/Sessions/Playing/Progress", "POST")]
  62. [Api(Description = "Reports playback progress within a session")]
  63. public class ReportPlaybackProgress : PlaybackProgressInfo, IReturnVoid
  64. {
  65. }
  66. [Route("/Sessions/Playing/Stopped", "POST")]
  67. [Api(Description = "Reports playback has stopped within a session")]
  68. public class ReportPlaybackStopped : PlaybackStopInfo, IReturnVoid
  69. {
  70. }
  71. /// <summary>
  72. /// Class OnPlaybackStart
  73. /// </summary>
  74. [Route("/Users/{UserId}/PlayingItems/{Id}", "POST")]
  75. [Api(Description = "Reports that a user has begun playing an item")]
  76. public class OnPlaybackStart : IReturnVoid
  77. {
  78. /// <summary>
  79. /// Gets or sets the user id.
  80. /// </summary>
  81. /// <value>The user id.</value>
  82. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  83. public Guid UserId { get; set; }
  84. /// <summary>
  85. /// Gets or sets the id.
  86. /// </summary>
  87. /// <value>The id.</value>
  88. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  89. public string Id { get; set; }
  90. [ApiMember(Name = "MediaSourceId", Description = "The id of the MediaSource", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  91. public string MediaSourceId { get; set; }
  92. /// <summary>
  93. /// Gets or sets a value indicating whether this <see cref="UpdateUserItemRating" /> is likes.
  94. /// </summary>
  95. /// <value><c>true</c> if likes; otherwise, <c>false</c>.</value>
  96. [ApiMember(Name = "CanSeek", Description = "Indicates if the client can seek", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "POST")]
  97. public bool CanSeek { get; set; }
  98. /// <summary>
  99. /// Gets or sets the id.
  100. /// </summary>
  101. /// <value>The id.</value>
  102. [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)]
  103. public string QueueableMediaTypes { get; set; }
  104. [ApiMember(Name = "AudioStreamIndex", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
  105. public int? AudioStreamIndex { get; set; }
  106. [ApiMember(Name = "SubtitleStreamIndex", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
  107. public int? SubtitleStreamIndex { get; set; }
  108. }
  109. /// <summary>
  110. /// Class OnPlaybackProgress
  111. /// </summary>
  112. [Route("/Users/{UserId}/PlayingItems/{Id}/Progress", "POST")]
  113. [Api(Description = "Reports a user's playback progress")]
  114. public class OnPlaybackProgress : IReturnVoid
  115. {
  116. /// <summary>
  117. /// Gets or sets the user id.
  118. /// </summary>
  119. /// <value>The user id.</value>
  120. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  121. public Guid UserId { get; set; }
  122. /// <summary>
  123. /// Gets or sets the id.
  124. /// </summary>
  125. /// <value>The id.</value>
  126. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  127. public string Id { get; set; }
  128. [ApiMember(Name = "MediaSourceId", Description = "The id of the MediaSource", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  129. public string MediaSourceId { get; set; }
  130. /// <summary>
  131. /// Gets or sets the position ticks.
  132. /// </summary>
  133. /// <value>The position ticks.</value>
  134. [ApiMember(Name = "PositionTicks", Description = "Optional. The current position, in ticks. 1 tick = 10000 ms", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
  135. public long? PositionTicks { get; set; }
  136. [ApiMember(Name = "IsPaused", Description = "Indicates if the player is paused.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "POST")]
  137. public bool IsPaused { get; set; }
  138. [ApiMember(Name = "IsMuted", Description = "Indicates if the player is muted.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "POST")]
  139. public bool IsMuted { get; set; }
  140. [ApiMember(Name = "AudioStreamIndex", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
  141. public int? AudioStreamIndex { get; set; }
  142. [ApiMember(Name = "SubtitleStreamIndex", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
  143. public int? SubtitleStreamIndex { get; set; }
  144. [ApiMember(Name = "VolumeLevel", Description = "Scale of 0-100", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
  145. public int? VolumeLevel { get; set; }
  146. }
  147. /// <summary>
  148. /// Class OnPlaybackStopped
  149. /// </summary>
  150. [Route("/Users/{UserId}/PlayingItems/{Id}", "DELETE")]
  151. [Api(Description = "Reports that a user has stopped playing an item")]
  152. public class OnPlaybackStopped : IReturnVoid
  153. {
  154. /// <summary>
  155. /// Gets or sets the user id.
  156. /// </summary>
  157. /// <value>The user id.</value>
  158. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  159. public Guid UserId { get; set; }
  160. /// <summary>
  161. /// Gets or sets the id.
  162. /// </summary>
  163. /// <value>The id.</value>
  164. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  165. public string Id { get; set; }
  166. [ApiMember(Name = "MediaSourceId", Description = "The id of the MediaSource", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
  167. public string MediaSourceId { get; set; }
  168. /// <summary>
  169. /// Gets or sets the position ticks.
  170. /// </summary>
  171. /// <value>The position ticks.</value>
  172. [ApiMember(Name = "PositionTicks", Description = "Optional. The position, in ticks, where playback stopped. 1 tick = 10000 ms", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "DELETE")]
  173. public long? PositionTicks { get; set; }
  174. }
  175. [Authenticated]
  176. public class PlaystateService : BaseApiService
  177. {
  178. private readonly IUserManager _userManager;
  179. private readonly IUserDataManager _userDataRepository;
  180. private readonly ILibraryManager _libraryManager;
  181. private readonly ISessionManager _sessionManager;
  182. public PlaystateService(IUserManager userManager, IUserDataManager userDataRepository, ILibraryManager libraryManager, ISessionManager sessionManager)
  183. {
  184. _userManager = userManager;
  185. _userDataRepository = userDataRepository;
  186. _libraryManager = libraryManager;
  187. _sessionManager = sessionManager;
  188. }
  189. /// <summary>
  190. /// Posts the specified request.
  191. /// </summary>
  192. /// <param name="request">The request.</param>
  193. public async Task<object> Post(MarkPlayedItem request)
  194. {
  195. var user = _userManager.GetUserById(request.UserId);
  196. DateTime? datePlayed = null;
  197. if (!string.IsNullOrEmpty(request.DatePlayed))
  198. {
  199. datePlayed = DateTime.ParseExact(request.DatePlayed, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
  200. }
  201. var session = GetSession();
  202. var dto = await UpdatePlayedStatus(user, request.Id, true, datePlayed).ConfigureAwait(false);
  203. foreach (var additionalUserInfo in session.AdditionalUsers)
  204. {
  205. var additionalUser = _userManager.GetUserById(new Guid(additionalUserInfo.UserId));
  206. await UpdatePlayedStatus(additionalUser, request.Id, true, datePlayed).ConfigureAwait(false);
  207. }
  208. return ToOptimizedResult(dto);
  209. }
  210. /// <summary>
  211. /// Posts the specified request.
  212. /// </summary>
  213. /// <param name="request">The request.</param>
  214. public Task Post(OnPlaybackStart request)
  215. {
  216. var queueableMediaTypes = (request.QueueableMediaTypes ?? string.Empty);
  217. return Post(new ReportPlaybackStart
  218. {
  219. CanSeek = request.CanSeek,
  220. ItemId = request.Id,
  221. QueueableMediaTypes = queueableMediaTypes.Split(',').ToList(),
  222. MediaSourceId = request.MediaSourceId,
  223. AudioStreamIndex = request.AudioStreamIndex,
  224. SubtitleStreamIndex = request.SubtitleStreamIndex
  225. });
  226. }
  227. public Task Post(ReportPlaybackStart request)
  228. {
  229. request.SessionId = GetSession().Id;
  230. return _sessionManager.OnPlaybackStart(request);
  231. }
  232. /// <summary>
  233. /// Posts the specified request.
  234. /// </summary>
  235. /// <param name="request">The request.</param>
  236. public Task Post(OnPlaybackProgress request)
  237. {
  238. return Post(new ReportPlaybackProgress
  239. {
  240. ItemId = request.Id,
  241. PositionTicks = request.PositionTicks,
  242. IsMuted = request.IsMuted,
  243. IsPaused = request.IsPaused,
  244. MediaSourceId = request.MediaSourceId,
  245. AudioStreamIndex = request.AudioStreamIndex,
  246. SubtitleStreamIndex = request.SubtitleStreamIndex,
  247. VolumeLevel = request.VolumeLevel
  248. });
  249. }
  250. public Task Post(ReportPlaybackProgress request)
  251. {
  252. request.SessionId = GetSession().Id;
  253. return _sessionManager.OnPlaybackProgress(request);
  254. }
  255. /// <summary>
  256. /// Posts the specified request.
  257. /// </summary>
  258. /// <param name="request">The request.</param>
  259. public Task Delete(OnPlaybackStopped request)
  260. {
  261. return Post(new ReportPlaybackStopped
  262. {
  263. ItemId = request.Id,
  264. PositionTicks = request.PositionTicks,
  265. MediaSourceId = request.MediaSourceId
  266. });
  267. }
  268. public Task Post(ReportPlaybackStopped request)
  269. {
  270. request.SessionId = GetSession().Id;
  271. return _sessionManager.OnPlaybackStopped(request);
  272. }
  273. /// <summary>
  274. /// Deletes the specified request.
  275. /// </summary>
  276. /// <param name="request">The request.</param>
  277. public object Delete(MarkUnplayedItem request)
  278. {
  279. var task = MarkUnplayed(request);
  280. return ToOptimizedResult(task.Result);
  281. }
  282. private async Task<UserItemDataDto> MarkUnplayed(MarkUnplayedItem request)
  283. {
  284. var user = _userManager.GetUserById(request.UserId);
  285. var session = GetSession();
  286. var dto = await UpdatePlayedStatus(user, request.Id, false, null).ConfigureAwait(false);
  287. foreach (var additionalUserInfo in session.AdditionalUsers)
  288. {
  289. var additionalUser = _userManager.GetUserById(new Guid(additionalUserInfo.UserId));
  290. await UpdatePlayedStatus(additionalUser, request.Id, false, null).ConfigureAwait(false);
  291. }
  292. return dto;
  293. }
  294. /// <summary>
  295. /// Updates the played status.
  296. /// </summary>
  297. /// <param name="user">The user.</param>
  298. /// <param name="itemId">The item id.</param>
  299. /// <param name="wasPlayed">if set to <c>true</c> [was played].</param>
  300. /// <param name="datePlayed">The date played.</param>
  301. /// <returns>Task.</returns>
  302. private async Task<UserItemDataDto> UpdatePlayedStatus(User user, string itemId, bool wasPlayed, DateTime? datePlayed)
  303. {
  304. var item = _libraryManager.GetItemById(itemId);
  305. if (wasPlayed)
  306. {
  307. await item.MarkPlayed(user, datePlayed, _userDataRepository).ConfigureAwait(false);
  308. }
  309. else
  310. {
  311. await item.MarkUnplayed(user, _userDataRepository).ConfigureAwait(false);
  312. }
  313. return _userDataRepository.GetUserDataDto(item, user);
  314. }
  315. }
  316. }