make marking date played up to the caller to decide

This commit is contained in:
Luke Pulverenti 2013-09-21 15:24:50 -04:00
parent 842b95ea02
commit 0a7df60207
4 changed files with 78 additions and 30 deletions

View File

@ -184,6 +184,9 @@ namespace MediaBrowser.Api.UserLibrary
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get; set; }
[ApiMember(Name = "DatePlayed", Description = "The date the item was played (if any)", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public DateTime? DatePlayed { get; set; }
/// <summary>
/// Gets or sets the id.
/// </summary>
@ -630,7 +633,7 @@ namespace MediaBrowser.Api.UserLibrary
{
var user = _userManager.GetUserById(request.UserId);
var task = UpdatePlayedStatus(user, request.Id, true);
var task = UpdatePlayedStatus(user, request.Id, true, request.DatePlayed);
return ToOptimizedResult(task.Result);
}
@ -703,7 +706,7 @@ namespace MediaBrowser.Api.UserLibrary
{
var user = _userManager.GetUserById(request.UserId);
var task = UpdatePlayedStatus(user, request.Id, false);
var task = UpdatePlayedStatus(user, request.Id, false, null);
return ToOptimizedResult(task.Result);
}
@ -714,12 +717,20 @@ namespace MediaBrowser.Api.UserLibrary
/// <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)
private async Task<UserItemDataDto> UpdatePlayedStatus(User user, string itemId, bool wasPlayed, DateTime? datePlayed)
{
var item = _dtoService.GetItemByDtoId(itemId, user.Id);
await item.SetPlayedStatus(user, wasPlayed, _userDataRepository).ConfigureAwait(false);
if (wasPlayed)
{
await item.MarkPlayed(user, datePlayed, _userDataRepository).ConfigureAwait(false);
}
else
{
await item.MarkUnplayed(user, _userDataRepository).ConfigureAwait(false);
}
return _dtoService.GetUserItemDataDto(_userDataRepository.GetUserData(user.Id, item.GetUserDataKey()));
}

View File

@ -1315,14 +1315,14 @@ namespace MediaBrowser.Controller.Entities
}
/// <summary>
/// Marks the item as either played or unplayed
/// Marks the played.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="wasPlayed">if set to <c>true</c> [was played].</param>
/// <param name="datePlayed">The date played.</param>
/// <param name="userManager">The user manager.</param>
/// <returns>Task.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
public virtual async Task SetPlayedStatus(User user, bool wasPlayed, IUserDataRepository userManager)
public virtual async Task MarkPlayed(User user, DateTime? datePlayed, IUserDataRepository userManager)
{
if (user == null)
{
@ -1333,20 +1333,39 @@ namespace MediaBrowser.Controller.Entities
var data = userManager.GetUserData(user.Id, key);
if (wasPlayed)
data.PlayCount = Math.Max(data.PlayCount, 1);
data.LastPlayedDate = datePlayed ?? data.LastPlayedDate;
data.Played = true;
await userManager.SaveUserData(user.Id, key, data, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Marks the unplayed.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="userManager">The user manager.</param>
/// <returns>Task.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
public virtual async Task MarkUnplayed(User user, IUserDataRepository userManager)
{
if (user == null)
{
data.PlayCount = Math.Max(data.PlayCount, 1);
}
else
{
//I think it is okay to do this here.
// if this is only called when a user is manually forcing something to un-played
// then it probably is what we want to do...
data.PlayCount = 0;
data.PlaybackPositionTicks = 0;
throw new ArgumentNullException();
}
data.Played = wasPlayed;
var key = GetUserDataKey();
var data = userManager.GetUserData(user.Id, key);
//I think it is okay to do this here.
// if this is only called when a user is manually forcing something to un-played
// then it probably is what we want to do...
data.PlayCount = 0;
data.PlaybackPositionTicks = 0;
data.LastPlayedDate = null;
data.Played = false;
await userManager.SaveUserData(user.Id, key, data, CancellationToken.None).ConfigureAwait(false);
}

View File

@ -1161,16 +1161,30 @@ namespace MediaBrowser.Controller.Entities
}
/// <summary>
/// Marks the item as either played or unplayed
/// Marks the played.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="wasPlayed">if set to <c>true</c> [was played].</param>
/// <param name="datePlayed">The date played.</param>
/// <param name="userManager">The user manager.</param>
/// <returns>Task.</returns>
public override async Task SetPlayedStatus(User user, bool wasPlayed, IUserDataRepository userManager)
public override async Task MarkPlayed(User user, DateTime? datePlayed, IUserDataRepository userManager)
{
// Sweep through recursively and update status
var tasks = GetRecursiveChildren(user, true).Where(i => !i.IsFolder).Select(c => c.SetPlayedStatus(user, wasPlayed, userManager));
var tasks = GetRecursiveChildren(user, true).Where(i => !i.IsFolder).Select(c => c.MarkPlayed(user, datePlayed, userManager));
await Task.WhenAll(tasks).ConfigureAwait(false);
}
/// <summary>
/// Marks the unplayed.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="userManager">The user manager.</param>
/// <returns>Task.</returns>
public override async Task MarkUnplayed(User user, IUserDataRepository userManager)
{
// Sweep through recursively and update status
var tasks = GetRecursiveChildren(user, true).Where(i => !i.IsFolder).Select(c => c.MarkUnplayed(user, userManager));
await Task.WhenAll(tasks).ConfigureAwait(false);
}

View File

@ -453,17 +453,21 @@ namespace MediaBrowser.Model.ApiClient
Task<CountryInfo[]> GetCountriesAsync();
/// <summary>
/// Marks an item as played or unplayed.
/// This should not be used to update playstate following playback.
/// There are separate playstate check-in methods for that. This should be used for a
/// separate option to reset playstate.
/// Marks the played async.
/// </summary>
/// <param name="itemId">The item id.</param>
/// <param name="userId">The user id.</param>
/// <param name="wasPlayed">if set to <c>true</c> [was played].</param>
/// <returns>Task.</returns>
/// <exception cref="ArgumentNullException">itemId</exception>
Task<UserItemDataDto> UpdatePlayedStatusAsync(string itemId, string userId, bool wasPlayed);
/// <param name="datePlayed">The date played.</param>
/// <returns>Task{UserItemDataDto}.</returns>
Task<UserItemDataDto> MarkPlayedAsync(string itemId, string userId, DateTime? datePlayed);
/// <summary>
/// Marks the unplayed async.
/// </summary>
/// <param name="itemId">The item id.</param>
/// <param name="userId">The user id.</param>
/// <returns>Task{UserItemDataDto}.</returns>
Task<UserItemDataDto> MarkUnplayedAsync(string itemId, string userId);
/// <summary>
/// Updates the favorite status async.