using MediaBrowser.Common.Net; using MediaBrowser.Common.Serialization; using MediaBrowser.Controller; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; using ServiceStack.ServiceHost; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using ServiceStack.Text.Controller; namespace MediaBrowser.Api.UserLibrary { /// /// Class GetItem /// [Route("/Users/{UserId}/Items/{Id}", "GET")] [Route("/Users/{UserId}/Items/Root", "GET")] public class GetItem : IReturn { /// /// Gets or sets the user id. /// /// The user id. public Guid UserId { get; set; } /// /// Gets or sets the id. /// /// The id. public string Id { get; set; } } /// /// Class GetIntros /// [Route("/Users/{UserId}/Items/{Id}/Intros", "GET")] [ServiceStack.ServiceHost.Api(("Gets intros to play before the main media item plays"))] public class GetIntros : IReturn> { /// /// Gets or sets the user id. /// /// The user id. public Guid UserId { get; set; } /// /// Gets or sets the item id. /// /// The item id. public string Id { get; set; } } /// /// Class UpdateDisplayPreferences /// [Route("/Users/{UserId}/Items/{Id}/DisplayPreferences", "GET")] [ServiceStack.ServiceHost.Api(("Updates a user's display preferences for an item"))] public class UpdateDisplayPreferences : IReturnVoid, IRequiresRequestStream { /// /// Gets or sets the user id. /// /// The user id. public Guid UserId { get; set; } /// /// Gets or sets the id. /// /// The id. public string Id { get; set; } /// /// The raw Http Request Input Stream /// /// The request stream. public Stream RequestStream { get; set; } } /// /// Class GetVirtualFolders /// [Route("/Users/{UserId}/VirtualFolders", "GET")] public class GetVirtualFolders : IReturn> { /// /// Gets or sets the user id. /// /// The user id. public Guid UserId { get; set; } } /// /// Class MarkFavoriteItem /// [Route("/Users/{UserId}/FavoriteItems/{Id}", "POST")] public class MarkFavoriteItem : IReturnVoid { /// /// Gets or sets the user id. /// /// The user id. public Guid UserId { get; set; } /// /// Gets or sets the id. /// /// The id. public string Id { get; set; } } /// /// Class UnmarkFavoriteItem /// [Route("/Users/{UserId}/FavoriteItems/{Id}", "DELETE")] public class UnmarkFavoriteItem : IReturnVoid { /// /// Gets or sets the user id. /// /// The user id. public Guid UserId { get; set; } /// /// Gets or sets the id. /// /// The id. public string Id { get; set; } } /// /// Class ClearUserItemRating /// [Route("/Users/{UserId}/Items/{Id}/Rating", "DELETE")] public class DeleteUserItemRating : IReturnVoid { /// /// Gets or sets the user id. /// /// The user id. public Guid UserId { get; set; } /// /// Gets or sets the id. /// /// The id. public string Id { get; set; } } /// /// Class UpdateUserItemRating /// [Route("/Users/{UserId}/Items/{Id}/Rating", "POST")] public class UpdateUserItemRating : IReturnVoid { /// /// Gets or sets the user id. /// /// The user id. public Guid UserId { get; set; } /// /// Gets or sets the id. /// /// The id. public string Id { get; set; } /// /// Gets or sets a value indicating whether this is likes. /// /// true if likes; otherwise, false. public bool Likes { get; set; } } /// /// Class MarkPlayedItem /// [Route("/Users/{UserId}/PlayedItems/{Id}", "POST")] public class MarkPlayedItem : IReturnVoid { /// /// Gets or sets the user id. /// /// The user id. public Guid UserId { get; set; } /// /// Gets or sets the id. /// /// The id. public string Id { get; set; } } /// /// Class MarkUnplayedItem /// [Route("/Users/{UserId}/PlayedItems/{Id}", "DELETE")] public class MarkUnplayedItem : IReturnVoid { /// /// Gets or sets the user id. /// /// The user id. public Guid UserId { get; set; } /// /// Gets or sets the id. /// /// The id. public string Id { get; set; } } [Route("/Users/{UserId}/Items/{Id}/LocalTrailers", "GET")] public class GetLocalTrailers : IReturn> { /// /// Gets or sets the user id. /// /// The user id. public Guid UserId { get; set; } /// /// Gets or sets the id. /// /// The id. public string Id { get; set; } } [Route("/Users/{UserId}/Items/{Id}/SpecialFeatures", "GET")] public class GetSpecialFeatures : IReturn> { /// /// Gets or sets the user id. /// /// The user id. public Guid UserId { get; set; } /// /// Gets or sets the id. /// /// The id. public string Id { get; set; } } /// /// Class UserLibraryService /// public class UserLibraryService : BaseRestService { public object Get(GetSpecialFeatures request) { var kernel = (Kernel)Kernel; var user = kernel.GetUserById(request.UserId); var item = DtoBuilder.GetItemByClientId(request.Id, user.Id); // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList(); var movie = (Movie)item; var dtoBuilder = new DtoBuilder(Logger); var items = movie.SpecialFeatures.Select(i => dtoBuilder.GetDtoBaseItem(item, user, fields)).AsParallel().Select(t => t.Result).ToList(); return ToOptimizedResult(items); } public object Get(GetLocalTrailers request) { var kernel = (Kernel)Kernel; var user = kernel.GetUserById(request.UserId); var item = DtoBuilder.GetItemByClientId(request.Id, user.Id); // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList(); var dtoBuilder = new DtoBuilder(Logger); var items = item.LocalTrailers.Select(i => dtoBuilder.GetDtoBaseItem(item, user, fields)).AsParallel().Select(t => t.Result).ToList(); return ToOptimizedResult(items); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetItem request) { var kernel = (Kernel)Kernel; var user = kernel.GetUserById(request.UserId); var item = string.IsNullOrEmpty(request.Id) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.Id, user.Id); // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList(); var dtoBuilder = new DtoBuilder(Logger); var result = dtoBuilder.GetDtoBaseItem(item, user, fields).Result; return ToOptimizedResult(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetVirtualFolders request) { var kernel = (Kernel)Kernel; var user = kernel.GetUserById(request.UserId); var result = kernel.LibraryManager.GetVirtualFolders(user).ToList(); return ToOptimizedResult(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetIntros request) { var kernel = (Kernel)Kernel; var user = kernel.GetUserById(request.UserId); var item = DtoBuilder.GetItemByClientId(request.Id, user.Id); var result = kernel.IntroProviders.SelectMany(i => i.GetIntros(item, user)); return ToOptimizedResult(result); } /// /// Posts the specified request. /// /// The request. public void Post(UpdateDisplayPreferences request) { // We need to parse this manually because we told service stack not to with IRequiresRequestStream // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs var pathInfo = PathInfo.Parse(Request.PathInfo); var userId = new Guid(pathInfo.GetArgumentValue(1)); var itemId = pathInfo.GetArgumentValue(3); var kernel = (Kernel)Kernel; var user = kernel.GetUserById(userId); var item = (Folder)DtoBuilder.GetItemByClientId(itemId, user.Id); var displayPreferences = JsonSerializer.DeserializeFromStream(request.RequestStream); var task = kernel.LibraryManager.SaveDisplayPreferencesForFolder(user, item, displayPreferences); Task.WaitAll(task); } /// /// Posts the specified request. /// /// The request. public void Post(MarkFavoriteItem request) { var kernel = (Kernel)Kernel; var user = kernel.GetUserById(request.UserId); var item = (Folder)DtoBuilder.GetItemByClientId(request.Id, user.Id); // Get the user data for this item var data = item.GetUserData(user, true); // Set favorite status data.IsFavorite = true; var task = kernel.UserDataManager.SaveUserDataForItem(user, item, data); Task.WaitAll(task); } /// /// Deletes the specified request. /// /// The request. public void Delete(UnmarkFavoriteItem request) { var kernel = (Kernel)Kernel; var user = kernel.GetUserById(request.UserId); var item = (Folder)DtoBuilder.GetItemByClientId(request.Id, user.Id); // Get the user data for this item var data = item.GetUserData(user, true); // Set favorite status data.IsFavorite = false; var task = kernel.UserDataManager.SaveUserDataForItem(user, item, data); Task.WaitAll(task); } /// /// Deletes the specified request. /// /// The request. public void Delete(DeleteUserItemRating request) { var kernel = (Kernel)Kernel; var user = kernel.GetUserById(request.UserId); var item = (Folder)DtoBuilder.GetItemByClientId(request.Id, user.Id); // Get the user data for this item var data = item.GetUserData(user, true); data.Rating = null; var task = kernel.UserDataManager.SaveUserDataForItem(user, item, data); Task.WaitAll(task); } /// /// Posts the specified request. /// /// The request. public void Post(UpdateUserItemRating request) { var kernel = (Kernel)Kernel; var user = kernel.GetUserById(request.UserId); var item = (Folder)DtoBuilder.GetItemByClientId(request.Id, user.Id); // Get the user data for this item var data = item.GetUserData(user, true); data.Likes = request.Likes; var task = kernel.UserDataManager.SaveUserDataForItem(user, item, data); Task.WaitAll(task); } /// /// Posts the specified request. /// /// The request. public void Post(MarkPlayedItem request) { var kernel = (Kernel)Kernel; var user = kernel.GetUserById(request.UserId); var task = UpdatePlayedStatus(user, request.Id, true); Task.WaitAll(task); } /// /// Deletes the specified request. /// /// The request. public void Delete(MarkUnplayedItem request) { var kernel = (Kernel)Kernel; var user = kernel.GetUserById(request.UserId); var task = UpdatePlayedStatus(user, request.Id, false); Task.WaitAll(task); } /// /// Updates the played status. /// /// The user. /// The item id. /// if set to true [was played]. /// Task. private Task UpdatePlayedStatus(User user, string itemId, bool wasPlayed) { var item = DtoBuilder.GetItemByClientId(itemId, user.Id); return item.SetPlayedStatus(user, wasPlayed); } } }