| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using Jellyfin.Database.Implementations; |
| | | 5 | | using Jellyfin.Database.Implementations.Entities; |
| | | 6 | | using MediaBrowser.Controller; |
| | | 7 | | using Microsoft.EntityFrameworkCore; |
| | | 8 | | |
| | | 9 | | namespace Jellyfin.Server.Implementations.Users; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Manages the storage and retrieval of display preferences through Entity Framework. |
| | | 13 | | /// </summary> |
| | | 14 | | public sealed class DisplayPreferencesManager : IDisplayPreferencesManager |
| | | 15 | | { |
| | | 16 | | private const int MaxSaveAttempts = 3; |
| | | 17 | | |
| | | 18 | | private readonly IDbContextFactory<JellyfinDbContext> _dbContextFactory; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="DisplayPreferencesManager"/> class. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="dbContextFactory">The database context factory.</param> |
| | | 24 | | public DisplayPreferencesManager(IDbContextFactory<JellyfinDbContext> dbContextFactory) |
| | | 25 | | { |
| | 5 | 26 | | _dbContextFactory = dbContextFactory; |
| | 5 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public DisplayPreferences GetDisplayPreferences(Guid userId, Guid itemId, string client) |
| | | 31 | | { |
| | 1 | 32 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| | 1 | 33 | | var prefs = FindDisplayPreferences(dbContext, userId, itemId, client); |
| | 1 | 34 | | if (prefs is not null) |
| | | 35 | | { |
| | 0 | 36 | | return prefs; |
| | | 37 | | } |
| | | 38 | | |
| | 1 | 39 | | prefs = new DisplayPreferences(userId, itemId, client); |
| | 1 | 40 | | dbContext.DisplayPreferences.Add(prefs); |
| | | 41 | | try |
| | | 42 | | { |
| | 1 | 43 | | dbContext.SaveChanges(); |
| | 0 | 44 | | } |
| | 1 | 45 | | catch (DbUpdateException) |
| | | 46 | | { |
| | | 47 | | // Another request may have stored the preferences between the lookup and the insert, and the unique index |
| | | 48 | | // rejected this one. Return the stored preferences; if there are none, the insert failed for another reason |
| | 1 | 49 | | using var retryContext = _dbContextFactory.CreateDbContext(); |
| | 1 | 50 | | var stored = FindDisplayPreferences(retryContext, userId, itemId, client); |
| | 1 | 51 | | if (stored is null) |
| | | 52 | | { |
| | 0 | 53 | | throw; |
| | | 54 | | } |
| | | 55 | | |
| | 1 | 56 | | return stored; |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | return prefs; |
| | 1 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | public ItemDisplayPreferences GetItemDisplayPreferences(Guid userId, Guid itemId, string client) |
| | | 64 | | { |
| | 3 | 65 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| | 3 | 66 | | var prefs = dbContext.ItemDisplayPreferences |
| | 3 | 67 | | .FirstOrDefault(pref => pref.UserId.Equals(userId) && pref.ItemId.Equals(itemId) && pref.Client == client); |
| | | 68 | | |
| | 3 | 69 | | if (prefs is null) |
| | | 70 | | { |
| | 2 | 71 | | prefs = new ItemDisplayPreferences(userId, itemId, client); |
| | 2 | 72 | | dbContext.ItemDisplayPreferences.Add(prefs); |
| | 2 | 73 | | dbContext.SaveChanges(); |
| | | 74 | | } |
| | | 75 | | |
| | 3 | 76 | | return prefs; |
| | 3 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <inheritdoc /> |
| | | 80 | | public IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client) |
| | | 81 | | { |
| | 0 | 82 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| | 0 | 83 | | return dbContext.ItemDisplayPreferences |
| | 0 | 84 | | .Where(prefs => prefs.UserId.Equals(userId) && !prefs.ItemId.Equals(default) && prefs.Client == client) |
| | 0 | 85 | | .ToList(); |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <inheritdoc /> |
| | | 89 | | public Dictionary<string, string?> ListCustomItemDisplayPreferences(Guid userId, Guid itemId, string client) |
| | | 90 | | { |
| | 2 | 91 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| | 2 | 92 | | return dbContext.CustomItemDisplayPreferences |
| | 2 | 93 | | .Where(prefs => prefs.UserId.Equals(userId) |
| | 2 | 94 | | && prefs.ItemId.Equals(itemId) |
| | 2 | 95 | | && prefs.Client == client) |
| | 2 | 96 | | .ToDictionary(prefs => prefs.Key, prefs => prefs.Value); |
| | 2 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <inheritdoc /> |
| | | 100 | | public void SetCustomItemDisplayPreferences(Guid userId, Guid itemId, string client, Dictionary<string, string?> cus |
| | | 101 | | { |
| | | 102 | | // Another request can store one of these keys after this one's delete, and the unique index then rejects the |
| | | 103 | | // insert. Replacing the set again gives the same result, so the replace is repeated. |
| | 7 | 104 | | for (var attempt = 1; ; attempt++) |
| | | 105 | | { |
| | 7 | 106 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| | 7 | 107 | | using var transaction = dbContext.Database.BeginTransaction(); |
| | 7 | 108 | | dbContext.CustomItemDisplayPreferences.Where(prefs => prefs.UserId.Equals(userId) |
| | 7 | 109 | | && prefs.ItemId.Equals(itemId) |
| | 7 | 110 | | && prefs.Client == client) |
| | 7 | 111 | | .ExecuteDelete(); |
| | | 112 | | |
| | 28 | 113 | | foreach (var (key, value) in customPreferences) |
| | | 114 | | { |
| | 7 | 115 | | dbContext.CustomItemDisplayPreferences |
| | 7 | 116 | | .Add(new CustomItemDisplayPreferences(userId, itemId, client, key, value)); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | try |
| | | 120 | | { |
| | 7 | 121 | | dbContext.SaveChanges(); |
| | 3 | 122 | | } |
| | 4 | 123 | | catch (DbUpdateException) when (attempt < MaxSaveAttempts) |
| | | 124 | | { |
| | 3 | 125 | | continue; |
| | | 126 | | } |
| | | 127 | | |
| | 3 | 128 | | transaction.Commit(); |
| | 3 | 129 | | return; |
| | | 130 | | } |
| | 3 | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <inheritdoc/> |
| | | 134 | | public void UpdateDisplayPreferences(DisplayPreferences displayPreferences) |
| | | 135 | | { |
| | 0 | 136 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| | 0 | 137 | | dbContext.DisplayPreferences.Attach(displayPreferences).State = EntityState.Modified; |
| | 0 | 138 | | dbContext.SaveChanges(); |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <inheritdoc/> |
| | | 142 | | public void UpdateItemDisplayPreferences(ItemDisplayPreferences itemDisplayPreferences) |
| | | 143 | | { |
| | 0 | 144 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| | 0 | 145 | | dbContext.ItemDisplayPreferences.Attach(itemDisplayPreferences).State = EntityState.Modified; |
| | 0 | 146 | | dbContext.SaveChanges(); |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | private static DisplayPreferences? FindDisplayPreferences(JellyfinDbContext dbContext, Guid userId, Guid itemId, str |
| | 2 | 150 | | => dbContext.DisplayPreferences |
| | 2 | 151 | | .Include(pref => pref.HomeSections) |
| | 2 | 152 | | .FirstOrDefault(pref => pref.UserId.Equals(userId) && pref.Client == client && pref.ItemId.Equals(itemId)); |
| | | 153 | | } |