Serialize/deserialize new entities properly.

This commit is contained in:
Patrick Barron 2020-07-27 20:40:21 -04:00
parent 592d2480ca
commit 68a185fd02
7 changed files with 139 additions and 43 deletions

View File

@ -5,15 +5,15 @@ using Jellyfin.Data.Enums;
namespace Jellyfin.Data.Entities
{
public class LibraryDisplayPreferences
public class ItemDisplayPreferences
{
/// <summary>
/// Initializes a new instance of the <see cref="LibraryDisplayPreferences"/> class.
/// Initializes a new instance of the <see cref="ItemDisplayPreferences"/> class.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="itemId">The item id.</param>
/// <param name="client">The client.</param>
public LibraryDisplayPreferences(Guid userId, Guid itemId, string client)
public ItemDisplayPreferences(Guid userId, Guid itemId, string client)
{
UserId = userId;
ItemId = itemId;
@ -27,9 +27,9 @@ namespace Jellyfin.Data.Entities
}
/// <summary>
/// Initializes a new instance of the <see cref="LibraryDisplayPreferences"/> class.
/// Initializes a new instance of the <see cref="ItemDisplayPreferences"/> class.
/// </summary>
protected LibraryDisplayPreferences()
protected ItemDisplayPreferences()
{
}

View File

@ -48,7 +48,7 @@ namespace Jellyfin.Data.Entities
PasswordResetProviderId = passwordResetProviderId;
AccessSchedules = new HashSet<AccessSchedule>();
LibraryDisplayPreferences = new HashSet<LibraryDisplayPreferences>();
ItemDisplayPreferences = new HashSet<ItemDisplayPreferences>();
// Groups = new HashSet<Group>();
Permissions = new HashSet<Permission>();
Preferences = new HashSet<Preference>();
@ -362,7 +362,7 @@ namespace Jellyfin.Data.Entities
/// <summary>
/// Gets or sets the list of item display preferences.
/// </summary>
public virtual ICollection<LibraryDisplayPreferences> LibraryDisplayPreferences { get; protected set; }
public virtual ICollection<ItemDisplayPreferences> ItemDisplayPreferences { get; protected set; }
/*
/// <summary>

View File

@ -32,6 +32,8 @@ namespace Jellyfin.Server.Implementations
public virtual DbSet<ImageInfo> ImageInfos { get; set; }
public virtual DbSet<ItemDisplayPreferences> ItemDisplayPreferences { get; set; }
public virtual DbSet<Permission> Permissions { get; set; }
public virtual DbSet<Preference> Preferences { get; set; }

View File

@ -1,6 +1,7 @@
#pragma warning disable CA1307
using System;
using System.Collections.Generic;
using System.Linq;
using Jellyfin.Data.Entities;
using MediaBrowser.Controller;
@ -31,17 +32,43 @@ namespace Jellyfin.Server.Implementations.Users
var prefs = dbContext.DisplayPreferences
.Include(pref => pref.HomeSections)
.FirstOrDefault(pref =>
pref.UserId == userId && pref.ItemId == null && string.Equals(pref.Client, client));
pref.UserId == userId && string.Equals(pref.Client, client));
if (prefs == null)
{
prefs = new DisplayPreferences(client, userId);
prefs = new DisplayPreferences(userId, client);
dbContext.DisplayPreferences.Add(prefs);
}
return prefs;
}
/// <inheritdoc />
public ItemDisplayPreferences GetItemDisplayPreferences(Guid userId, Guid itemId, string client)
{
using var dbContext = _dbProvider.CreateContext();
var prefs = dbContext.ItemDisplayPreferences
.FirstOrDefault(pref => pref.UserId == userId && pref.ItemId == itemId && string.Equals(pref.Client, client));
if (prefs == null)
{
prefs = new ItemDisplayPreferences(userId, Guid.Empty, client);
dbContext.ItemDisplayPreferences.Add(prefs);
}
return prefs;
}
/// <inheritdoc />
public IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client)
{
using var dbContext = _dbProvider.CreateContext();
return dbContext.ItemDisplayPreferences
.Where(prefs => prefs.UserId == userId && prefs.ItemId != Guid.Empty && string.Equals(prefs.Client, client))
.ToList();
}
/// <inheritdoc />
public void SaveChanges(DisplayPreferences preferences)
{
@ -49,5 +76,13 @@ namespace Jellyfin.Server.Implementations.Users
dbContext.Update(preferences);
dbContext.SaveChanges();
}
/// <inheritdoc />
public void SaveChanges(ItemDisplayPreferences preferences)
{
using var dbContext = _dbProvider.CreateContext();
dbContext.Update(preferences);
dbContext.SaveChanges();
}
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Jellyfin.Data.Entities;
@ -78,16 +79,11 @@ namespace Jellyfin.Server.Migrations.Routines
: ChromecastVersion.Stable
: ChromecastVersion.Stable;
var displayPreferences = new DisplayPreferences(result[2].ToString(), new Guid(result[1].ToBlob()))
var displayPreferences = new DisplayPreferences(new Guid(result[1].ToBlob()), result[2].ToString())
{
ViewType = Enum.TryParse<ViewType>(dto.ViewType, true, out var viewType) ? viewType : (ViewType?)null,
IndexBy = Enum.TryParse<IndexingKind>(dto.IndexBy, true, out var indexBy) ? indexBy : (IndexingKind?)null,
ShowBackdrop = dto.ShowBackdrop,
ShowSidebar = dto.ShowSidebar,
SortBy = dto.SortBy,
SortOrder = dto.SortOrder,
RememberIndexing = dto.RememberIndexing,
RememberSorting = dto.RememberSorting,
ScrollDirection = dto.ScrollDirection,
ChromecastVersion = chromecastVersion,
SkipForwardLength = dto.CustomPrefs.TryGetValue("skipForwardLength", out var length)
@ -95,7 +91,7 @@ namespace Jellyfin.Server.Migrations.Routines
: 30000,
SkipBackwardLength = dto.CustomPrefs.TryGetValue("skipBackLength", out length)
? int.Parse(length, CultureInfo.InvariantCulture)
: 30000,
: 10000,
EnableNextVideoInfoOverlay = dto.CustomPrefs.TryGetValue("enableNextVideoInfoOverlay", out var enabled)
? bool.Parse(enabled)
: true
@ -112,6 +108,29 @@ namespace Jellyfin.Server.Migrations.Routines
});
}
foreach (var key in dto.CustomPrefs.Keys.Where(key => key.StartsWith("landing-", StringComparison.Ordinal)))
{
if (!Guid.TryParse(key.AsSpan().Slice("landing-".Length), out var itemId))
{
continue;
}
var libraryDisplayPreferences = new ItemDisplayPreferences(displayPreferences.UserId, itemId, displayPreferences.Client)
{
SortBy = dto.SortBy,
SortOrder = dto.SortOrder,
RememberIndexing = dto.RememberIndexing,
RememberSorting = dto.RememberSorting,
};
if (Enum.TryParse<ViewType>(dto.ViewType, true, out var viewType))
{
libraryDisplayPreferences.ViewType = viewType;
}
dbContext.ItemDisplayPreferences.Add(libraryDisplayPreferences);
}
dbContext.Add(displayPreferences);
}

View File

@ -76,37 +76,38 @@ namespace MediaBrowser.Api
/// <param name="request">The request.</param>
public object Get(GetDisplayPreferences request)
{
var result = _displayPreferencesManager.GetDisplayPreferences(Guid.Parse(request.UserId), request.Client);
if (result == null)
{
return null;
}
var displayPreferences = _displayPreferencesManager.GetDisplayPreferences(Guid.Parse(request.UserId), request.Client);
var itemPreferences = _displayPreferencesManager.GetItemDisplayPreferences(displayPreferences.UserId, Guid.Empty, displayPreferences.Client);
var dto = new DisplayPreferencesDto
{
Client = result.Client,
Id = result.UserId.ToString(),
ViewType = result.ViewType?.ToString(),
SortBy = result.SortBy,
SortOrder = result.SortOrder,
IndexBy = result.IndexBy?.ToString(),
RememberIndexing = result.RememberIndexing,
RememberSorting = result.RememberSorting,
ScrollDirection = result.ScrollDirection,
ShowBackdrop = result.ShowBackdrop,
ShowSidebar = result.ShowSidebar
Client = displayPreferences.Client,
Id = displayPreferences.UserId.ToString(),
ViewType = itemPreferences.ViewType.ToString(),
SortBy = itemPreferences.SortBy,
SortOrder = itemPreferences.SortOrder,
IndexBy = displayPreferences.IndexBy?.ToString(),
RememberIndexing = itemPreferences.RememberIndexing,
RememberSorting = itemPreferences.RememberSorting,
ScrollDirection = displayPreferences.ScrollDirection,
ShowBackdrop = displayPreferences.ShowBackdrop,
ShowSidebar = displayPreferences.ShowSidebar
};
foreach (var homeSection in result.HomeSections)
foreach (var homeSection in displayPreferences.HomeSections)
{
dto.CustomPrefs["homesection" + homeSection.Order] = homeSection.Type.ToString().ToLowerInvariant();
}
dto.CustomPrefs["chromecastVersion"] = result.ChromecastVersion.ToString().ToLowerInvariant();
dto.CustomPrefs["skipForwardLength"] = result.SkipForwardLength.ToString();
dto.CustomPrefs["skipBackLength"] = result.SkipBackwardLength.ToString();
dto.CustomPrefs["enableNextVideoInfoOverlay"] = result.EnableNextVideoInfoOverlay.ToString();
foreach (var itemDisplayPreferences in _displayPreferencesManager.ListItemDisplayPreferences(displayPreferences.UserId, displayPreferences.Client))
{
dto.CustomPrefs["landing-" + itemDisplayPreferences.ItemId] = itemDisplayPreferences.ViewType.ToString().ToLowerInvariant();
}
dto.CustomPrefs["chromecastVersion"] = displayPreferences.ChromecastVersion.ToString().ToLowerInvariant();
dto.CustomPrefs["skipForwardLength"] = displayPreferences.SkipForwardLength.ToString();
dto.CustomPrefs["skipBackLength"] = displayPreferences.SkipBackwardLength.ToString();
dto.CustomPrefs["enableNextVideoInfoOverlay"] = displayPreferences.EnableNextVideoInfoOverlay.ToString();
return ToOptimizedResult(dto);
}
@ -130,14 +131,10 @@ namespace MediaBrowser.Api
var prefs = _displayPreferencesManager.GetDisplayPreferences(Guid.Parse(request.UserId), request.Client);
prefs.ViewType = Enum.TryParse<ViewType>(request.ViewType, true, out var viewType) ? viewType : (ViewType?)null;
prefs.IndexBy = Enum.TryParse<IndexingKind>(request.IndexBy, true, out var indexBy) ? indexBy : (IndexingKind?)null;
prefs.ShowBackdrop = request.ShowBackdrop;
prefs.ShowSidebar = request.ShowSidebar;
prefs.SortBy = request.SortBy;
prefs.SortOrder = request.SortOrder;
prefs.RememberIndexing = request.RememberIndexing;
prefs.RememberSorting = request.RememberSorting;
prefs.ScrollDirection = request.ScrollDirection;
prefs.ChromecastVersion = request.CustomPrefs.TryGetValue("chromecastVersion", out var chromecastVersion)
? Enum.Parse<ChromecastVersion>(chromecastVersion, true)
@ -164,7 +161,26 @@ namespace MediaBrowser.Api
});
}
foreach (var key in request.CustomPrefs.Keys.Where(key => key.StartsWith("landing-")))
{
var itemPreferences = _displayPreferencesManager.GetItemDisplayPreferences(prefs.UserId, Guid.Parse(key.Substring("landing-".Length)), prefs.Client);
itemPreferences.ViewType = Enum.Parse<ViewType>(request.ViewType);
_displayPreferencesManager.SaveChanges(itemPreferences);
}
var itemPrefs = _displayPreferencesManager.GetItemDisplayPreferences(prefs.UserId, Guid.Empty, prefs.Client);
itemPrefs.SortBy = request.SortBy;
itemPrefs.SortOrder = request.SortOrder;
itemPrefs.RememberIndexing = request.RememberIndexing;
itemPrefs.RememberSorting = request.RememberSorting;
if (Enum.TryParse<ViewType>(request.ViewType, true, out var viewType))
{
itemPrefs.ViewType = viewType;
}
_displayPreferencesManager.SaveChanges(prefs);
_displayPreferencesManager.SaveChanges(itemPrefs);
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Jellyfin.Data.Entities;
namespace MediaBrowser.Controller
@ -16,10 +17,33 @@ namespace MediaBrowser.Controller
/// <returns>The associated display preferences.</returns>
DisplayPreferences GetDisplayPreferences(Guid userId, string client);
/// <summary>
/// Gets the default item display preferences for the user and client.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="itemId">The item id.</param>
/// <param name="client">The client string.</param>
/// <returns>The item display preferences.</returns>
ItemDisplayPreferences GetItemDisplayPreferences(Guid userId, Guid itemId, string client);
/// <summary>
/// Gets all of the item display preferences for the user and client.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="client">The client string.</param>
/// <returns>A list of item display preferences.</returns>
IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client);
/// <summary>
/// Saves changes to the provided display preferences.
/// </summary>
/// <param name="preferences">The display preferences to save.</param>
void SaveChanges(DisplayPreferences preferences);
/// <summary>
/// Saves changes to the provided item display preferences.
/// </summary>
/// <param name="preferences">The item display preferences to save.</param>
void SaveChanges(ItemDisplayPreferences preferences);
}
}