limit use of GetUserDataKey

This commit is contained in:
Luke Pulverenti 2016-04-30 18:05:13 -04:00
parent d75cc21d37
commit 1f9d32afc5
19 changed files with 70 additions and 65 deletions

View File

@ -247,7 +247,7 @@ namespace MediaBrowser.Api.Movies
var recentlyPlayedMovies = allMoviesForCategories
.Select(i =>
{
var userdata = _userDataRepository.GetUserData(user.Id, i.GetUserDataKey());
var userdata = _userDataRepository.GetUserData(user, i);
return new Tuple<BaseItem, bool, DateTime>(i, userdata.Played, userdata.LastPlayedDate ?? DateTime.MinValue);
})
.Where(i => i.Item2)
@ -260,7 +260,7 @@ namespace MediaBrowser.Api.Movies
.Select(i =>
{
var score = 0;
var userData = _userDataRepository.GetUserData(user.Id, i.GetUserDataKey());
var userData = _userDataRepository.GetUserData(user, i);
if (userData.IsFavorite)
{

View File

@ -274,7 +274,7 @@ namespace MediaBrowser.Api.UserLibrary
{
items = items.Where(i =>
{
var userdata = UserDataRepository.GetUserData(user.Id, i.GetUserDataKey());
var userdata = UserDataRepository.GetUserData(user, i);
return userdata != null && userdata.Likes.HasValue && !userdata.Likes.Value;
});
@ -284,7 +284,7 @@ namespace MediaBrowser.Api.UserLibrary
{
items = items.Where(i =>
{
var userdata = UserDataRepository.GetUserData(user.Id, i.GetUserDataKey());
var userdata = UserDataRepository.GetUserData(user, i);
return userdata != null && userdata.Likes.HasValue && userdata.Likes.Value;
});
@ -294,7 +294,7 @@ namespace MediaBrowser.Api.UserLibrary
{
items = items.Where(i =>
{
var userdata = UserDataRepository.GetUserData(user.Id, i.GetUserDataKey());
var userdata = UserDataRepository.GetUserData(user, i);
var likes = userdata.Likes ?? false;
var favorite = userdata.IsFavorite;
@ -307,7 +307,7 @@ namespace MediaBrowser.Api.UserLibrary
{
items = items.Where(i =>
{
var userdata = UserDataRepository.GetUserData(user.Id, i.GetUserDataKey());
var userdata = UserDataRepository.GetUserData(user, i);
return userdata != null && userdata.IsFavorite;
});

View File

@ -519,10 +519,8 @@ namespace MediaBrowser.Api.UserLibrary
var item = string.IsNullOrEmpty(itemId) ? user.RootFolder : _libraryManager.GetItemById(itemId);
var key = item.GetUserDataKey();
// Get the user data for this item
var data = _userDataRepository.GetUserData(user.Id, key);
var data = _userDataRepository.GetUserData(user, item);
// Set favorite status
data.IsFavorite = isFavorite;
@ -567,10 +565,8 @@ namespace MediaBrowser.Api.UserLibrary
var item = string.IsNullOrEmpty(itemId) ? user.RootFolder : _libraryManager.GetItemById(itemId);
var key = item.GetUserDataKey();
// Get the user data for this item
var data = _userDataRepository.GetUserData(user.Id, key);
var data = _userDataRepository.GetUserData(user, item);
data.Likes = likes;

View File

@ -1617,9 +1617,7 @@ namespace MediaBrowser.Controller.Entities
throw new ArgumentNullException();
}
var key = GetUserDataKey();
var data = UserDataManager.GetUserData(user.Id, key);
var data = UserDataManager.GetUserData(user, this);
if (datePlayed.HasValue)
{
@ -1654,9 +1652,7 @@ namespace MediaBrowser.Controller.Entities
throw new ArgumentNullException();
}
var key = GetUserDataKey();
var data = UserDataManager.GetUserData(user.Id, key);
var data = UserDataManager.GetUserData(user, this);
//I think it is okay to do this here.
// if this is only called when a user is manually forcing something to un-played
@ -1987,14 +1983,14 @@ namespace MediaBrowser.Controller.Entities
public virtual bool IsPlayed(User user)
{
var userdata = UserDataManager.GetUserData(user.Id, GetUserDataKey());
var userdata = UserDataManager.GetUserData(user, this);
return userdata != null && userdata.Played;
}
public bool IsFavoriteOrLiked(User user)
{
var userdata = UserDataManager.GetUserData(user.Id, GetUserDataKey());
var userdata = UserDataManager.GetUserData(user, this);
return userdata != null && (userdata.IsFavorite || (userdata.Likes ?? false));
}
@ -2006,7 +2002,7 @@ namespace MediaBrowser.Controller.Entities
throw new ArgumentNullException("user");
}
var userdata = UserDataManager.GetUserData(user.Id, GetUserDataKey());
var userdata = UserDataManager.GetUserData(user, this);
return userdata == null || !userdata.Played;
}

View File

@ -1635,7 +1635,7 @@ namespace MediaBrowser.Controller.Entities
var isUnplayed = true;
var itemUserData = UserDataManager.GetUserData(user.Id, child.GetUserDataKey());
var itemUserData = UserDataManager.GetUserData(user, child);
// Incrememt totalPercentPlayed
if (itemUserData != null)

View File

@ -348,7 +348,7 @@ namespace MediaBrowser.Controller.Entities
.Where(i => !i.IsFolder)
.OfType<IHasAlbumArtist>();
var artists = _libraryManager.GetAlbumArtists(items).Where(i => _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).IsFavorite);
var artists = _libraryManager.GetAlbumArtists(items).Where(i => _userDataManager.GetUserData(user, i).IsFavorite);
return GetResult(artists, parent, query);
}
@ -1218,7 +1218,7 @@ namespace MediaBrowser.Controller.Entities
if (query.IsLiked.HasValue)
{
userData = userData ?? userDataManager.GetUserData(user.Id, item.GetUserDataKey());
userData = userData ?? userDataManager.GetUserData(user, item);
if (!userData.Likes.HasValue || userData.Likes != query.IsLiked.Value)
{
@ -1228,7 +1228,7 @@ namespace MediaBrowser.Controller.Entities
if (query.IsFavoriteOrLiked.HasValue)
{
userData = userData ?? userDataManager.GetUserData(user.Id, item.GetUserDataKey());
userData = userData ?? userDataManager.GetUserData(user, item);
var isFavoriteOrLiked = userData.IsFavorite || (userData.Likes ?? false);
if (isFavoriteOrLiked != query.IsFavoriteOrLiked.Value)
@ -1239,7 +1239,7 @@ namespace MediaBrowser.Controller.Entities
if (query.IsFavorite.HasValue)
{
userData = userData ?? userDataManager.GetUserData(user.Id, item.GetUserDataKey());
userData = userData ?? userDataManager.GetUserData(user, item);
if (userData.IsFavorite != query.IsFavorite.Value)
{
@ -1249,7 +1249,7 @@ namespace MediaBrowser.Controller.Entities
if (query.IsResumable.HasValue)
{
userData = userData ?? userDataManager.GetUserData(user.Id, item.GetUserDataKey());
userData = userData ?? userDataManager.GetUserData(user, item);
var isResumable = userData.PlaybackPositionTicks > 0;
if (isResumable != query.IsResumable.Value)

View File

@ -45,6 +45,11 @@ namespace MediaBrowser.Controller.Library
/// <returns>Task{UserItemData}.</returns>
UserItemData GetUserData(Guid userId, string key);
UserItemData GetUserData(IHasUserData user, IHasUserData item);
UserItemData GetUserData(string userId, IHasUserData item);
UserItemData GetUserData(Guid userId, IHasUserData item);
/// <summary>
/// Gets the user data dto.
/// </summary>

View File

@ -111,7 +111,7 @@ namespace MediaBrowser.Dlna.ContentDirectory
var newbookmark = int.Parse(sparams["PosSecond"], _usCulture);
var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
var userdata = _userDataManager.GetUserData(user, item);
userdata.PlaybackPositionTicks = TimeSpan.FromSeconds(newbookmark).Ticks;

View File

@ -133,7 +133,7 @@ namespace MediaBrowser.Server.Implementations.Channels
if (query.IsFavorite.HasValue)
{
var val = query.IsFavorite.Value;
channels = channels.Where(i => _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).IsFavorite == val)
channels = channels.Where(i => _userDataManager.GetUserData(user, i).IsFavorite == val)
.ToList();
}
@ -1437,7 +1437,7 @@ namespace MediaBrowser.Server.Implementations.Channels
case ItemFilter.IsFavoriteOrLikes:
return items.Where(item =>
{
var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
var userdata = _userDataManager.GetUserData(user, item);
if (userdata == null)
{
@ -1453,7 +1453,7 @@ namespace MediaBrowser.Server.Implementations.Channels
case ItemFilter.Likes:
return items.Where(item =>
{
var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
var userdata = _userDataManager.GetUserData(user, item);
return userdata != null && userdata.Likes.HasValue && userdata.Likes.Value;
});
@ -1461,7 +1461,7 @@ namespace MediaBrowser.Server.Implementations.Channels
case ItemFilter.Dislikes:
return items.Where(item =>
{
var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
var userdata = _userDataManager.GetUserData(user, item);
return userdata != null && userdata.Likes.HasValue && !userdata.Likes.Value;
});
@ -1469,7 +1469,7 @@ namespace MediaBrowser.Server.Implementations.Channels
case ItemFilter.IsFavorite:
return items.Where(item =>
{
var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
var userdata = _userDataManager.GetUserData(user, item);
return userdata != null && userdata.IsFavorite;
});
@ -1477,7 +1477,7 @@ namespace MediaBrowser.Server.Implementations.Channels
case ItemFilter.IsResumable:
return items.Where(item =>
{
var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
var userdata = _userDataManager.GetUserData(user, item);
return userdata != null && userdata.PlaybackPositionTicks > 0;
});

View File

@ -487,7 +487,7 @@ namespace MediaBrowser.Server.Implementations.Dto
{
if (item.IsFolder)
{
var userData = _userDataRepository.GetUserData(user.Id, item.GetUserDataKey());
var userData = _userDataRepository.GetUserData(user, item);
// Skip the user data manager because we've already looped through the recursive tree and don't want to do it twice
// TODO: Improve in future
@ -1686,7 +1686,7 @@ namespace MediaBrowser.Server.Implementations.Dto
dateLastMediaAdded = new[] { dateLastMediaAdded.Value, child.DateCreated }.Max();
}
var userdata = _userDataRepository.GetUserData(user.Id, child.GetUserDataKey());
var userdata = _userDataRepository.GetUserData(user, child);
recursiveItemCount++;

View File

@ -267,7 +267,7 @@ namespace MediaBrowser.Server.Implementations.Library
private void SetUserProperties(IHasUserData item, MediaSourceInfo source, User user)
{
var userData = item == null ? new UserItemData() : _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
var userData = item == null ? new UserItemData() : _userDataManager.GetUserData(user, item);
var allowRememberingSelection = item == null || item.EnableRememberingTrackSelections;

View File

@ -172,6 +172,21 @@ namespace MediaBrowser.Server.Implementations.Library
return userId + key;
}
public UserItemData GetUserData(IHasUserData user, IHasUserData item)
{
return GetUserData(user.Id, item.GetUserDataKey());
}
public UserItemData GetUserData(string userId, IHasUserData item)
{
return GetUserData(userId, item.GetUserDataKey());
}
public UserItemData GetUserData(Guid userId, IHasUserData item)
{
return GetUserData(userId, item.GetUserDataKey());
}
public UserItemDataDto GetUserDataDto(IHasUserData item, User user)
{
var userData = GetUserData(user.Id, item.GetUserDataKey());

View File

@ -164,7 +164,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv
var val = query.IsFavorite.Value;
channels = channels
.Where(i => _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).IsFavorite == val);
.Where(i => _userDataManager.GetUserData(user, i).IsFavorite == val);
}
if (query.IsLiked.HasValue)
@ -174,7 +174,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv
channels = channels
.Where(i =>
{
var likes = _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).Likes;
var likes = _userDataManager.GetUserData(user, i).Likes;
return likes.HasValue && likes.Value == val;
});
@ -187,7 +187,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv
channels = channels
.Where(i =>
{
var likes = _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).Likes;
var likes = _userDataManager.GetUserData(user, i).Likes;
return likes.HasValue && likes.Value != val;
});
@ -200,7 +200,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv
{
if (enableFavoriteSorting)
{
var userData = _userDataManager.GetUserData(user.Id, i.GetUserDataKey());
var userData = _userDataManager.GetUserData(user, i);
if (userData.IsFavorite)
{
@ -1005,7 +1005,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv
var channel = GetInternalChannel(program.ChannelId);
var channelUserdata = _userDataManager.GetUserData(userId, channel.GetUserDataKey());
var channelUserdata = _userDataManager.GetUserData(userId, channel);
if (channelUserdata.Likes ?? false)
{
@ -1036,7 +1036,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv
if (genres.TryGetValue(i, out genre))
{
var genreUserdata = _userDataManager.GetUserData(userId, genre.GetUserDataKey());
var genreUserdata = _userDataManager.GetUserData(userId, genre);
if (genreUserdata.Likes ?? false)
{

View File

@ -601,11 +601,9 @@ namespace MediaBrowser.Server.Implementations.Session
if (libraryItem != null)
{
var key = libraryItem.GetUserDataKey();
foreach (var user in users)
{
await OnPlaybackStart(user.Id, key, libraryItem).ConfigureAwait(false);
await OnPlaybackStart(user.Id, libraryItem).ConfigureAwait(false);
}
}
@ -632,12 +630,11 @@ namespace MediaBrowser.Server.Implementations.Session
/// Called when [playback start].
/// </summary>
/// <param name="userId">The user identifier.</param>
/// <param name="userDataKey">The user data key.</param>
/// <param name="item">The item.</param>
/// <returns>Task.</returns>
private async Task OnPlaybackStart(Guid userId, string userDataKey, IHasUserData item)
private async Task OnPlaybackStart(Guid userId, IHasUserData item)
{
var data = _userDataRepository.GetUserData(userId, userDataKey);
var data = _userDataRepository.GetUserData(userId, item);
data.PlayCount++;
data.LastPlayedDate = DateTime.UtcNow;
@ -676,11 +673,9 @@ namespace MediaBrowser.Server.Implementations.Session
if (libraryItem != null)
{
var key = libraryItem.GetUserDataKey();
foreach (var user in users)
{
await OnPlaybackProgress(user, key, libraryItem, info).ConfigureAwait(false);
await OnPlaybackProgress(user, libraryItem, info).ConfigureAwait(false);
}
}
@ -714,9 +709,9 @@ namespace MediaBrowser.Server.Implementations.Session
StartIdleCheckTimer();
}
private async Task OnPlaybackProgress(User user, string userDataKey, BaseItem item, PlaybackProgressInfo info)
private async Task OnPlaybackProgress(User user, BaseItem item, PlaybackProgressInfo info)
{
var data = _userDataRepository.GetUserData(user.Id, userDataKey);
var data = _userDataRepository.GetUserData(user.Id, item);
var positionTicks = info.PositionTicks;
@ -811,11 +806,9 @@ namespace MediaBrowser.Server.Implementations.Session
if (libraryItem != null)
{
var key = libraryItem.GetUserDataKey();
foreach (var user in users)
{
playedToCompletion = await OnPlaybackStopped(user.Id, key, libraryItem, info.PositionTicks, info.Failed).ConfigureAwait(false);
playedToCompletion = await OnPlaybackStopped(user.Id, libraryItem, info.PositionTicks, info.Failed).ConfigureAwait(false);
}
}
@ -848,13 +841,13 @@ namespace MediaBrowser.Server.Implementations.Session
await SendPlaybackStoppedNotification(session, CancellationToken.None).ConfigureAwait(false);
}
private async Task<bool> OnPlaybackStopped(Guid userId, string userDataKey, BaseItem item, long? positionTicks, bool playbackFailed)
private async Task<bool> OnPlaybackStopped(Guid userId, BaseItem item, long? positionTicks, bool playbackFailed)
{
bool playedToCompletion = false;
if (!playbackFailed)
{
var data = _userDataRepository.GetUserData(userId, userDataKey);
var data = _userDataRepository.GetUserData(userId, item);
if (positionTicks.HasValue)
{

View File

@ -47,7 +47,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
/// <returns>DateTime.</returns>
private DateTime GetDate(BaseItem x)
{
var userdata = UserDataRepository.GetUserData(User.Id, x.GetUserDataKey());
var userdata = UserDataRepository.GetUserData(User, x);
if (userdata != null && userdata.LastPlayedDate.HasValue)
{

View File

@ -34,7 +34,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
/// <returns>DateTime.</returns>
private int GetValue(BaseItem x)
{
var userdata = UserDataRepository.GetUserData(User.Id, x.GetUserDataKey());
var userdata = UserDataRepository.GetUserData(User, x);
return userdata == null ? 0 : userdata.PlayCount;
}

View File

@ -687,7 +687,7 @@ namespace MediaBrowser.Server.Implementations.Sync
private Task ReportOfflinePlayedItem(UserAction action)
{
var item = _libraryManager.GetItemById(action.ItemId);
var userData = _userDataManager.GetUserData(new Guid(action.UserId), item.GetUserDataKey());
var userData = _userDataManager.GetUserData(action.UserId, item);
userData.LastPlayedDate = action.Date;
_userDataManager.UpdatePlayState(item, userData, action.PositionTicks);

View File

@ -85,7 +85,7 @@ namespace MediaBrowser.Server.Implementations.TV
{
var episode = i.Item1;
var seriesUserData = _userDataManager.GetUserData(user.Id, episode.Series.GetUserDataKey());
var seriesUserData = _userDataManager.GetUserData(user, episode.Series);
if (seriesUserData.IsFavorite)
{
@ -128,7 +128,7 @@ namespace MediaBrowser.Server.Implementations.TV
// Go back starting with the most recent episodes
foreach (var episode in allEpisodes)
{
var userData = _userDataManager.GetUserData(user.Id, episode.GetUserDataKey());
var userData = _userDataManager.GetUserData(user, episode);
if (userData.Played)
{

View File

@ -936,7 +936,7 @@ namespace MediaBrowser.XbmcMetadata.Savers
return;
}
var userdata = userDataRepo.GetUserData(user.Id, item.GetUserDataKey());
var userdata = userDataRepo.GetUserData(user, item);
writer.WriteElementString("isuserfavorite", userdata.IsFavorite.ToString().ToLower());