Added api methods to mark an item as played or unplayed

pull/702/head
LukePulverenti Luke Pulverenti luke pulverenti 12 years ago
parent f68137ec4a
commit 614668a447

@ -172,7 +172,7 @@ namespace MediaBrowser.Api
dto.Type = item.GetType().Name;
dto.UserRating = item.UserRating;
dto.UserData = GetDTOUserItemData(item.GetUserData(user));
dto.UserData = GetDTOUserItemData(item.GetUserData(user, false));
Folder folder = item as Folder;

@ -11,7 +11,7 @@ namespace MediaBrowser.Api.HttpHandlers
/// Provides a handler to set user favorite status for an item
/// </summary>
[Export(typeof(BaseHandler))]
public class FavoriteStatus : BaseSerializationHandler<DTOUserItemData>
public class FavoriteStatusHandler : BaseSerializationHandler<DTOUserItemData>
{
public override bool HandlesRequest(HttpListenerRequest request)
{
@ -27,13 +27,7 @@ namespace MediaBrowser.Api.HttpHandlers
User user = ApiService.GetUserById(QueryString["userid"], true);
// Get the user data for this item
UserItemData data = item.GetUserData(user);
if (data == null)
{
data = new UserItemData();
item.AddUserData(user, data);
}
UserItemData data = item.GetUserData(user, true);
// Set favorite status
data.IsFavorite = QueryString["isfavorite"] == "1";

@ -0,0 +1,38 @@
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;
using System.ComponentModel.Composition;
using System.Net;
using System.Threading.Tasks;
namespace MediaBrowser.Api.HttpHandlers
{
/// <summary>
/// Provides a handler to set played status for an item
/// </summary>
[Export(typeof(BaseHandler))]
public class PlayedStatusHandler : BaseSerializationHandler<DTOUserItemData>
{
public override bool HandlesRequest(HttpListenerRequest request)
{
return ApiService.IsApiUrlMatch("PlayedStatus", request);
}
protected override Task<DTOUserItemData> GetObjectToSerialize()
{
// Get the item
BaseItem item = ApiService.GetItemById(QueryString["id"]);
// Get the user
User user = ApiService.GetUserById(QueryString["userid"], true);
bool wasPlayed = QueryString["played"] == "1";
item.SetPlayedStatus(user, wasPlayed);
UserItemData data = item.GetUserData(user, true);
return Task.FromResult<DTOUserItemData>(ApiService.GetDTOUserItemData(data));
}
}
}

@ -27,13 +27,7 @@ namespace MediaBrowser.Api.HttpHandlers
User user = ApiService.GetUserById(QueryString["userid"], true);
// Get the user data for this item
UserItemData data = item.GetUserData(user);
if (data == null)
{
data = new UserItemData();
item.AddUserData(user, data);
}
UserItemData data = item.GetUserData(user, true);
// If clearing the rating, set it to null
if (QueryString["clear"] == "1")

@ -62,6 +62,7 @@
<Compile Include="HttpHandlers\BaseMediaHandler.cs" />
<Compile Include="HttpHandlers\FavoriteStatusHandler.cs" />
<Compile Include="HttpHandlers\MovieSpecialFeaturesHandler.cs" />
<Compile Include="HttpHandlers\PlayedStatusHandler.cs" />
<Compile Include="HttpHandlers\UserHandler.cs" />
<Compile Include="HttpHandlers\GenreHandler.cs" />
<Compile Include="HttpHandlers\GenresHandler.cs" />

@ -410,6 +410,19 @@ namespace MediaBrowser.ApiInteraction.Portable
GetDataAsync(url, callback);
}
/// <summary>
/// Updates played status for an item
/// </summary>
public void UpdatePlayedStatusAsync(Guid itemId, Guid userId, bool wasPlayed, Action<DTOUserItemData> callback)
{
string url = ApiUrl + "/PlayedStatus?id=" + itemId;
url += "&userid=" + userId;
url += "&played=" + (wasPlayed ? "1" : "0");
GetDataAsync(url, callback);
}
/// <summary>
/// Clears a user's rating for an item
/// </summary>

@ -464,6 +464,22 @@ namespace MediaBrowser.ApiInteraction
}
}
/// <summary>
/// Updates played status for an item
/// </summary>
public async Task<DTOUserItemData> UpdatePlayedStatusAsync(Guid itemId, Guid userId, bool wasPlayed)
{
string url = ApiUrl + "/PlayedStatus?id=" + itemId;
url += "&userid=" + userId;
url += "&played=" + (wasPlayed ? "1" : "0");
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
return DeserializeFromStream<DTOUserItemData>(stream);
}
}
/// <summary>
/// Updates a user's favorite status for an item and returns the updated UserItemData object.
/// </summary>

@ -72,17 +72,24 @@ namespace MediaBrowser.Model.Entities
public Dictionary<Guid, UserItemData> UserData { get; set; }
public UserItemData GetUserData(User user)
public UserItemData GetUserData(User user, bool createIfNull)
{
if (UserData == null || !UserData.ContainsKey(user.Id))
{
if (createIfNull)
{
AddUserData(user, new UserItemData());
}
else
{
return null;
}
}
return UserData[user.Id];
}
public void AddUserData(User user, UserItemData data)
private void AddUserData(User user, UserItemData data)
{
if (UserData == null)
{
@ -143,5 +150,23 @@ namespace MediaBrowser.Model.Entities
People[person.Name] = person;
}
/// <summary>
/// Marks the item as either played or unplayed
/// </summary>
public virtual void SetPlayedStatus(User user, bool wasPlayed)
{
UserItemData data = GetUserData(user, true);
if (wasPlayed)
{
data.PlayCount = Math.Max(data.PlayCount, 1);
}
else
{
data.PlayCount = 0;
data.PlaybackPositionTicks = 0;
}
}
}
}

@ -103,7 +103,7 @@ namespace MediaBrowser.Model.Entities
{
return GetParentalAllowedRecursiveChildren(user).Where(c =>
{
UserItemData data = c.GetUserData(user);
UserItemData data = c.GetUserData(user, false);
if (data != null)
{
@ -171,21 +171,30 @@ namespace MediaBrowser.Model.Entities
return GetInProgressItems(GetParentalAllowedRecursiveChildren(user), user);
}
/// <summary>
/// Takes a list of items and returns the ones that are recently added
/// </summary>
private static IEnumerable<BaseItem> GetRecentlyAddedItems(IEnumerable<BaseItem> itemSet, User user)
{
return itemSet.Where(i => !(i.IsFolder) && i.IsRecentlyAdded(user));
}
/// <summary>
/// Takes a list of items and returns the ones that are recently added and unplayed
/// </summary>
private static IEnumerable<BaseItem> GetRecentlyAddedUnplayedItems(IEnumerable<BaseItem> itemSet, User user)
{
return GetRecentlyAddedItems(itemSet, user).Where(i =>
{
var userdata = i.GetUserData(user);
var userdata = i.GetUserData(user, false);
return userdata == null || userdata.PlayCount == 0;
});
}
/// <summary>
/// Takes a list of items and returns the ones that are in progress
/// </summary>
private static IEnumerable<BaseItem> GetInProgressItems(IEnumerable<BaseItem> itemSet, User user)
{
return itemSet.Where(i =>
@ -195,12 +204,15 @@ namespace MediaBrowser.Model.Entities
return false;
}
var userdata = i.GetUserData(user);
var userdata = i.GetUserData(user, false);
return userdata != null && userdata.PlaybackPositionTicks > 0;
});
}
/// <summary>
/// Gets the total played percentage for a set of items
/// </summary>
private static decimal GetPlayedPercentage(IEnumerable<BaseItem> itemSet, User user)
{
itemSet = itemSet.Where(i => !(i.IsFolder));
@ -214,7 +226,7 @@ namespace MediaBrowser.Model.Entities
foreach (BaseItem item in itemSet)
{
UserItemData data = item.GetUserData(user);
UserItemData data = item.GetUserData(user, false);
if (data == null)
{
@ -236,6 +248,20 @@ namespace MediaBrowser.Model.Entities
return totalPercent / itemSet.Count();
}
/// <summary>
/// Marks the item as either played or unplayed
/// </summary>
public override void SetPlayedStatus(User user, bool wasPlayed)
{
base.SetPlayedStatus(user, wasPlayed);
// Now sweep through recursively and update status
foreach (BaseItem item in GetParentalAllowedChildren(user))
{
item.SetPlayedStatus(user, wasPlayed);
}
}
/// <summary>
/// Finds an item by ID, recursively
/// </summary>

Loading…
Cancel
Save