| | 1 | | #pragma warning disable CA1307 |
| | 2 | | #pragma warning disable CA1309 |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Jellyfin.Data.Entities; |
| | 9 | | using MediaBrowser.Controller; |
| | 10 | | using Microsoft.EntityFrameworkCore; |
| | 11 | |
|
| | 12 | | namespace Jellyfin.Server.Implementations.Users |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Manages the storage and retrieval of display preferences through Entity Framework. |
| | 16 | | /// </summary> |
| | 17 | | public sealed class DisplayPreferencesManager : IDisplayPreferencesManager, IAsyncDisposable |
| | 18 | | { |
| | 19 | | private readonly JellyfinDbContext _dbContext; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Initializes a new instance of the <see cref="DisplayPreferencesManager"/> class. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="dbContextFactory">The database context factory.</param> |
| | 25 | | public DisplayPreferencesManager(IDbContextFactory<JellyfinDbContext> dbContextFactory) |
| | 26 | | { |
| 0 | 27 | | _dbContext = dbContextFactory.CreateDbContext(); |
| 0 | 28 | | } |
| | 29 | |
|
| | 30 | | /// <inheritdoc /> |
| | 31 | | public DisplayPreferences GetDisplayPreferences(Guid userId, Guid itemId, string client) |
| | 32 | | { |
| 0 | 33 | | var prefs = _dbContext.DisplayPreferences |
| 0 | 34 | | .Include(pref => pref.HomeSections) |
| 0 | 35 | | .FirstOrDefault(pref => |
| 0 | 36 | | pref.UserId.Equals(userId) && string.Equals(pref.Client, client) && pref.ItemId.Equals(itemId)); |
| | 37 | |
|
| 0 | 38 | | if (prefs is null) |
| | 39 | | { |
| 0 | 40 | | prefs = new DisplayPreferences(userId, itemId, client); |
| 0 | 41 | | _dbContext.DisplayPreferences.Add(prefs); |
| | 42 | | } |
| | 43 | |
|
| 0 | 44 | | return prefs; |
| | 45 | | } |
| | 46 | |
|
| | 47 | | /// <inheritdoc /> |
| | 48 | | public ItemDisplayPreferences GetItemDisplayPreferences(Guid userId, Guid itemId, string client) |
| | 49 | | { |
| 0 | 50 | | var prefs = _dbContext.ItemDisplayPreferences |
| 0 | 51 | | .FirstOrDefault(pref => pref.UserId.Equals(userId) && pref.ItemId.Equals(itemId) && string.Equals(pref.C |
| | 52 | |
|
| 0 | 53 | | if (prefs is null) |
| | 54 | | { |
| 0 | 55 | | prefs = new ItemDisplayPreferences(userId, Guid.Empty, client); |
| 0 | 56 | | _dbContext.ItemDisplayPreferences.Add(prefs); |
| | 57 | | } |
| | 58 | |
|
| 0 | 59 | | return prefs; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | /// <inheritdoc /> |
| | 63 | | public IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client) |
| | 64 | | { |
| 0 | 65 | | return _dbContext.ItemDisplayPreferences |
| 0 | 66 | | .Where(prefs => prefs.UserId.Equals(userId) && !prefs.ItemId.Equals(default) && string.Equals(prefs.Clie |
| 0 | 67 | | .ToList(); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | /// <inheritdoc /> |
| | 71 | | public Dictionary<string, string?> ListCustomItemDisplayPreferences(Guid userId, Guid itemId, string client) |
| | 72 | | { |
| 0 | 73 | | return _dbContext.CustomItemDisplayPreferences |
| 0 | 74 | | .Where(prefs => prefs.UserId.Equals(userId) |
| 0 | 75 | | && prefs.ItemId.Equals(itemId) |
| 0 | 76 | | && string.Equals(prefs.Client, client)) |
| 0 | 77 | | .ToDictionary(prefs => prefs.Key, prefs => prefs.Value); |
| | 78 | | } |
| | 79 | |
|
| | 80 | | /// <inheritdoc /> |
| | 81 | | public void SetCustomItemDisplayPreferences(Guid userId, Guid itemId, string client, Dictionary<string, string?> |
| | 82 | | { |
| 0 | 83 | | var existingPrefs = _dbContext.CustomItemDisplayPreferences |
| 0 | 84 | | .Where(prefs => prefs.UserId.Equals(userId) |
| 0 | 85 | | && prefs.ItemId.Equals(itemId) |
| 0 | 86 | | && string.Equals(prefs.Client, client)); |
| 0 | 87 | | _dbContext.CustomItemDisplayPreferences.RemoveRange(existingPrefs); |
| | 88 | |
|
| 0 | 89 | | foreach (var (key, value) in customPreferences) |
| | 90 | | { |
| 0 | 91 | | _dbContext.CustomItemDisplayPreferences |
| 0 | 92 | | .Add(new CustomItemDisplayPreferences(userId, itemId, client, key, value)); |
| | 93 | | } |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | /// <inheritdoc /> |
| | 97 | | public void SaveChanges() |
| | 98 | | { |
| 0 | 99 | | _dbContext.SaveChanges(); |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | /// <inheritdoc /> |
| | 103 | | public async ValueTask DisposeAsync() |
| | 104 | | { |
| | 105 | | await _dbContext.DisposeAsync().ConfigureAwait(false); |
| | 106 | | } |
| | 107 | | } |
| | 108 | | } |