add item to user data save event

This commit is contained in:
Luke Pulverenti 2013-10-23 12:03:12 -04:00
parent 77cff18dce
commit 6415776cd1
9 changed files with 53 additions and 48 deletions

View File

@ -228,7 +228,7 @@ namespace MediaBrowser.Api.UserLibrary
// Set favorite status // Set favorite status
data.IsFavorite = isFavorite; data.IsFavorite = isFavorite;
await UserDataRepository.SaveUserData(userId, key, data, UserDataSaveReason.UpdateUserRating, CancellationToken.None).ConfigureAwait(false); await UserDataRepository.SaveUserData(userId, item, data, UserDataSaveReason.UpdateUserRating, CancellationToken.None).ConfigureAwait(false);
data = UserDataRepository.GetUserData(userId, key); data = UserDataRepository.GetUserData(userId, key);
@ -254,7 +254,7 @@ namespace MediaBrowser.Api.UserLibrary
data.Likes = likes; data.Likes = likes;
await UserDataRepository.SaveUserData(userId, key, data, UserDataSaveReason.UpdateUserRating, CancellationToken.None).ConfigureAwait(false); await UserDataRepository.SaveUserData(userId, item, data, UserDataSaveReason.UpdateUserRating, CancellationToken.None).ConfigureAwait(false);
data = UserDataRepository.GetUserData(userId, key); data = UserDataRepository.GetUserData(userId, key);

View File

@ -608,7 +608,7 @@ namespace MediaBrowser.Api.UserLibrary
// Set favorite status // Set favorite status
data.IsFavorite = isFavorite; data.IsFavorite = isFavorite;
await _userDataRepository.SaveUserData(user.Id, key, data, UserDataSaveReason.UpdateUserRating, CancellationToken.None).ConfigureAwait(false); await _userDataRepository.SaveUserData(user.Id, item, data, UserDataSaveReason.UpdateUserRating, CancellationToken.None).ConfigureAwait(false);
data = _userDataRepository.GetUserData(user.Id, key); data = _userDataRepository.GetUserData(user.Id, key);
@ -657,7 +657,7 @@ namespace MediaBrowser.Api.UserLibrary
data.Likes = likes; data.Likes = likes;
await _userDataRepository.SaveUserData(user.Id, key, data, UserDataSaveReason.UpdateUserRating, CancellationToken.None).ConfigureAwait(false); await _userDataRepository.SaveUserData(user.Id, item, data, UserDataSaveReason.UpdateUserRating, CancellationToken.None).ConfigureAwait(false);
data = _userDataRepository.GetUserData(user.Id, key); data = _userDataRepository.GetUserData(user.Id, key);

View File

@ -1339,7 +1339,7 @@ namespace MediaBrowser.Controller.Entities
data.LastPlayedDate = datePlayed ?? data.LastPlayedDate; data.LastPlayedDate = datePlayed ?? data.LastPlayedDate;
data.Played = true; data.Played = true;
await userManager.SaveUserData(user.Id, key, data, UserDataSaveReason.TogglePlayed, CancellationToken.None).ConfigureAwait(false); await userManager.SaveUserData(user.Id, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None).ConfigureAwait(false);
} }
/// <summary> /// <summary>
@ -1368,7 +1368,7 @@ namespace MediaBrowser.Controller.Entities
data.LastPlayedDate = null; data.LastPlayedDate = null;
data.Played = false; data.Played = false;
await userManager.SaveUserData(user.Id, key, data, UserDataSaveReason.TogglePlayed, CancellationToken.None).ConfigureAwait(false); await userManager.SaveUserData(user.Id, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None).ConfigureAwait(false);
} }
/// <summary> /// <summary>

View File

@ -20,12 +20,12 @@ namespace MediaBrowser.Controller.Library
/// Saves the user data. /// Saves the user data.
/// </summary> /// </summary>
/// <param name="userId">The user id.</param> /// <param name="userId">The user id.</param>
/// <param name="key">The key.</param> /// <param name="item">The item.</param>
/// <param name="userData">The user data.</param> /// <param name="userData">The user data.</param>
/// <param name="reason">The reason.</param> /// <param name="reason">The reason.</param>
/// <param name="cancellationToken">The cancellation token.</param> /// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns> /// <returns>Task.</returns>
Task SaveUserData(Guid userId, string key, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken); Task SaveUserData(Guid userId, BaseItem item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken);
/// <summary> /// <summary>
/// Gets the user data. /// Gets the user data.

View File

@ -32,5 +32,11 @@ namespace MediaBrowser.Controller.Library
/// </summary> /// </summary>
/// <value>The user data.</value> /// <value>The user data.</value>
public UserItemData UserData { get; set; } public UserItemData UserData { get; set; }
/// <summary>
/// Gets or sets the item.
/// </summary>
/// <value>The item.</value>
public BaseItem Item { get; set; }
} }
} }

View File

@ -248,11 +248,6 @@ namespace MediaBrowser.Controller.Providers
return true; return true;
} }
if (RequiresInternet && DateTime.UtcNow > (providerInfo.LastRefreshed.AddDays(ConfigurationManager.Configuration.MetadataRefreshDays)))
{
return true;
}
if (providerInfo.LastRefreshStatus != ProviderRefreshStatus.Success) if (providerInfo.LastRefreshStatus != ProviderRefreshStatus.Success)
{ {
return true; return true;

View File

@ -123,17 +123,13 @@ namespace MediaBrowser.Providers.TV
if (parts.Length == 3) if (parts.Length == 3)
{ {
var seasonNumberString = parts[1];
int seasonNumber; int seasonNumber;
if (int.TryParse(seasonNumberString, NumberStyles.Integer, UsCulture, out seasonNumber)) if (int.TryParse(parts[1], NumberStyles.Integer, UsCulture, out seasonNumber))
{ {
var episodeNumberString = parts[2];
int episodeNumber; int episodeNumber;
if (int.TryParse(episodeNumberString, NumberStyles.Integer, UsCulture, out episodeNumber)) if (int.TryParse(parts[2], NumberStyles.Integer, UsCulture, out episodeNumber))
{ {
return new Tuple<int, int>(seasonNumber, episodeNumber); return new Tuple<int, int>(seasonNumber, episodeNumber);
} }
@ -145,22 +141,17 @@ namespace MediaBrowser.Providers.TV
.Where(i => i.Item1 != -1 && i.Item2 != -1) .Where(i => i.Item1 != -1 && i.Item2 != -1)
.ToList(); .ToList();
var existingEpisodes = series.RecursiveChildren
.OfType<Episode>()
.Where(i => i.IndexNumber.HasValue && i.ParentIndexNumber.HasValue)
.ToList();
var hasChanges = false; var hasChanges = false;
if (_config.Configuration.CreateVirtualMissingEpisodes || _config.Configuration.CreateVirtualFutureEpisodes) if (_config.Configuration.CreateVirtualMissingEpisodes || _config.Configuration.CreateVirtualFutureEpisodes)
{ {
if (_config.Configuration.EnableInternetProviders) if (_config.Configuration.EnableInternetProviders)
{ {
hasChanges = await AddMissingEpisodes(series, seriesDataPath, existingEpisodes, episodeLookup, cancellationToken).ConfigureAwait(false); hasChanges = await AddMissingEpisodes(series, seriesDataPath, episodeLookup, cancellationToken).ConfigureAwait(false);
} }
} }
var anyRemoved = await RemoveObsoleteMissingEpisodes(series, existingEpisodes, cancellationToken).ConfigureAwait(false); var anyRemoved = await RemoveObsoleteMissingEpisodes(series, cancellationToken).ConfigureAwait(false);
if (hasChanges || anyRemoved) if (hasChanges || anyRemoved)
{ {
@ -174,12 +165,15 @@ namespace MediaBrowser.Providers.TV
/// </summary> /// </summary>
/// <param name="series">The series.</param> /// <param name="series">The series.</param>
/// <param name="seriesDataPath">The series data path.</param> /// <param name="seriesDataPath">The series data path.</param>
/// <param name="existingEpisodes">The existing episodes.</param>
/// <param name="episodeLookup">The episode lookup.</param> /// <param name="episodeLookup">The episode lookup.</param>
/// <param name="cancellationToken">The cancellation token.</param> /// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns> /// <returns>Task.</returns>
private async Task<bool> AddMissingEpisodes(Series series, string seriesDataPath, List<Episode> existingEpisodes, IEnumerable<Tuple<int, int>> episodeLookup, CancellationToken cancellationToken) private async Task<bool> AddMissingEpisodes(Series series, string seriesDataPath, IEnumerable<Tuple<int, int>> episodeLookup, CancellationToken cancellationToken)
{ {
var existingEpisodes = series.RecursiveChildren
.OfType<Episode>()
.ToList();
var hasChanges = false; var hasChanges = false;
foreach (var tuple in episodeLookup) foreach (var tuple in episodeLookup)
@ -209,8 +203,9 @@ namespace MediaBrowser.Providers.TV
{ {
continue; continue;
} }
var now = DateTime.UtcNow;
if (airDate.Value < DateTime.UtcNow && _config.Configuration.CreateVirtualMissingEpisodes) if (airDate.Value < now && _config.Configuration.CreateVirtualMissingEpisodes)
{ {
// tvdb has a lot of nearly blank episodes // tvdb has a lot of nearly blank episodes
_logger.Info("Creating virtual missing episode {0} {1}x{2}", series.Name, tuple.Item1, tuple.Item2); _logger.Info("Creating virtual missing episode {0} {1}x{2}", series.Name, tuple.Item1, tuple.Item2);
@ -219,7 +214,7 @@ namespace MediaBrowser.Providers.TV
hasChanges = true; hasChanges = true;
} }
else if (airDate.Value > DateTime.UtcNow && _config.Configuration.CreateVirtualFutureEpisodes) else if (airDate.Value > now && _config.Configuration.CreateVirtualFutureEpisodes)
{ {
// tvdb has a lot of nearly blank episodes // tvdb has a lot of nearly blank episodes
_logger.Info("Creating virtual future episode {0} {1}x{2}", series.Name, tuple.Item1, tuple.Item2); _logger.Info("Creating virtual future episode {0} {1}x{2}", series.Name, tuple.Item1, tuple.Item2);
@ -236,14 +231,21 @@ namespace MediaBrowser.Providers.TV
/// <summary> /// <summary>
/// Removes the virtual entry after a corresponding physical version has been added /// Removes the virtual entry after a corresponding physical version has been added
/// </summary> /// </summary>
private async Task<bool> RemoveObsoleteMissingEpisodes(Series series, List<Episode> existingEpisodes, CancellationToken cancellationToken) private async Task<bool> RemoveObsoleteMissingEpisodes(Series series, CancellationToken cancellationToken)
{ {
var existingEpisodes = series.RecursiveChildren
.OfType<Episode>()
.ToList();
var physicalEpisodes = existingEpisodes var physicalEpisodes = existingEpisodes
.Where(i => i.LocationType != LocationType.Virtual) .Where(i => i.LocationType != LocationType.Virtual)
.ToList(); .ToList();
var episodesToRemove = existingEpisodes var virtualEpisodes = existingEpisodes
.Where(i => i.LocationType == LocationType.Virtual) .Where(i => i.LocationType == LocationType.Virtual)
.ToList();
var episodesToRemove = virtualEpisodes
.Where(i => .Where(i =>
{ {
if (i.IndexNumber.HasValue && i.ParentIndexNumber.HasValue) if (i.IndexNumber.HasValue && i.ParentIndexNumber.HasValue)
@ -279,7 +281,8 @@ namespace MediaBrowser.Providers.TV
/// <returns>Task.</returns> /// <returns>Task.</returns>
private async Task AddEpisode(Series series, int seasonNumber, int episodeNumber, CancellationToken cancellationToken) private async Task AddEpisode(Series series, int seasonNumber, int episodeNumber, CancellationToken cancellationToken)
{ {
var season = series.Children.OfType<Season>().FirstOrDefault(i => i.IndexNumber.HasValue && i.IndexNumber.Value == seasonNumber); var season = series.Children.OfType<Season>()
.FirstOrDefault(i => i.IndexNumber.HasValue && i.IndexNumber.Value == seasonNumber);
if (season == null) if (season == null)
{ {
@ -290,7 +293,7 @@ namespace MediaBrowser.Providers.TV
var episode = new Episode var episode = new Episode
{ {
Name = string.Format("Episode {0}", episodeNumber.ToString(UsCulture)), Name = name,
IndexNumber = episodeNumber, IndexNumber = episodeNumber,
ParentIndexNumber = seasonNumber, ParentIndexNumber = seasonNumber,
Parent = season, Parent = season,

View File

@ -37,37 +37,37 @@ namespace MediaBrowser.Server.Implementations.Library
/// Saves the user data. /// Saves the user data.
/// </summary> /// </summary>
/// <param name="userId">The user id.</param> /// <param name="userId">The user id.</param>
/// <param name="key">The key.</param> /// <param name="item">The item.</param>
/// <param name="userData">The user data.</param> /// <param name="userData">The user data.</param>
/// <param name="reason">The reason.</param> /// <param name="reason">The reason.</param>
/// <param name="cancellationToken">The cancellation token.</param> /// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns> /// <returns>Task.</returns>
/// <exception cref="System.ArgumentNullException"> /// <exception cref="System.ArgumentNullException">userData
/// userData
/// or /// or
/// cancellationToken /// cancellationToken
/// or /// or
/// userId /// userId
/// or /// or
/// key /// key</exception>
/// </exception> public async Task SaveUserData(Guid userId, BaseItem item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken)
public async Task SaveUserData(Guid userId, string key, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken)
{ {
if (userData == null) if (userData == null)
{ {
throw new ArgumentNullException("userData"); throw new ArgumentNullException("userData");
} }
if (item == null)
{
throw new ArgumentNullException("item");
}
if (userId == Guid.Empty) if (userId == Guid.Empty)
{ {
throw new ArgumentNullException("userId"); throw new ArgumentNullException("userId");
} }
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key");
}
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
var key = item.GetUserDataKey();
try try
{ {
await Repository.SaveUserData(userId, key, userData, cancellationToken).ConfigureAwait(false); await Repository.SaveUserData(userId, key, userData, cancellationToken).ConfigureAwait(false);
@ -89,7 +89,8 @@ namespace MediaBrowser.Server.Implementations.Library
Key = key, Key = key,
UserData = userData, UserData = userData,
SaveReason = reason, SaveReason = reason,
UserId = userId UserId = userId,
Item = item
}, _logger); }, _logger);
} }

View File

@ -254,7 +254,7 @@ namespace MediaBrowser.Server.Implementations.Session
data.Played = true; data.Played = true;
} }
await _userDataRepository.SaveUserData(user.Id, key, data, UserDataSaveReason.PlaybackStart, CancellationToken.None).ConfigureAwait(false); await _userDataRepository.SaveUserData(user.Id, info.Item, data, UserDataSaveReason.PlaybackStart, CancellationToken.None).ConfigureAwait(false);
// Nothing to save here // Nothing to save here
// Fire events to inform plugins // Fire events to inform plugins
@ -298,7 +298,7 @@ namespace MediaBrowser.Server.Implementations.Session
UpdatePlayState(info.Item, data, info.PositionTicks.Value); UpdatePlayState(info.Item, data, info.PositionTicks.Value);
await _userDataRepository.SaveUserData(user.Id, key, data, UserDataSaveReason.PlaybackProgress, CancellationToken.None).ConfigureAwait(false); await _userDataRepository.SaveUserData(user.Id, info.Item, data, UserDataSaveReason.PlaybackProgress, CancellationToken.None).ConfigureAwait(false);
} }
EventHelper.QueueEventIfNotNull(PlaybackProgress, this, new PlaybackProgressEventArgs EventHelper.QueueEventIfNotNull(PlaybackProgress, this, new PlaybackProgressEventArgs
@ -361,7 +361,7 @@ namespace MediaBrowser.Server.Implementations.Session
data.PlaybackPositionTicks = 0; data.PlaybackPositionTicks = 0;
} }
await _userDataRepository.SaveUserData(user.Id, key, data, UserDataSaveReason.PlaybackFinished, CancellationToken.None).ConfigureAwait(false); await _userDataRepository.SaveUserData(user.Id, info.Item, data, UserDataSaveReason.PlaybackFinished, CancellationToken.None).ConfigureAwait(false);
EventHelper.QueueEventIfNotNull(PlaybackStopped, this, new PlaybackProgressEventArgs EventHelper.QueueEventIfNotNull(PlaybackStopped, this, new PlaybackProgressEventArgs
{ {