| | 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 readonly IDbContextFactory<JellyfinDbContext> _dbContextFactory; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Initializes a new instance of the <see cref="DisplayPreferencesManager"/> class. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="dbContextFactory">The database context factory.</param> |
| | 22 | | public DisplayPreferencesManager(IDbContextFactory<JellyfinDbContext> dbContextFactory) |
| | 23 | | { |
| 0 | 24 | | _dbContextFactory = dbContextFactory; |
| 0 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <inheritdoc /> |
| | 28 | | public DisplayPreferences GetDisplayPreferences(Guid userId, Guid itemId, string client) |
| | 29 | | { |
| 0 | 30 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| 0 | 31 | | var prefs = dbContext.DisplayPreferences |
| 0 | 32 | | .Include(pref => pref.HomeSections) |
| 0 | 33 | | .FirstOrDefault(pref => |
| 0 | 34 | | pref.UserId.Equals(userId) && pref.Client == client && pref.ItemId.Equals(itemId)); |
| | 35 | |
|
| 0 | 36 | | if (prefs is null) |
| | 37 | | { |
| 0 | 38 | | prefs = new DisplayPreferences(userId, itemId, client); |
| 0 | 39 | | dbContext.DisplayPreferences.Add(prefs); |
| 0 | 40 | | dbContext.SaveChanges(); |
| | 41 | | } |
| | 42 | |
|
| 0 | 43 | | return prefs; |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <inheritdoc /> |
| | 47 | | public ItemDisplayPreferences GetItemDisplayPreferences(Guid userId, Guid itemId, string client) |
| | 48 | | { |
| 0 | 49 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| 0 | 50 | | var prefs = dbContext.ItemDisplayPreferences |
| 0 | 51 | | .FirstOrDefault(pref => pref.UserId.Equals(userId) && pref.ItemId.Equals(itemId) && pref.Client == client); |
| | 52 | |
|
| 0 | 53 | | if (prefs is null) |
| | 54 | | { |
| 0 | 55 | | prefs = new ItemDisplayPreferences(userId, Guid.Empty, client); |
| 0 | 56 | | dbContext.ItemDisplayPreferences.Add(prefs); |
| 0 | 57 | | dbContext.SaveChanges(); |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | return prefs; |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | /// <inheritdoc /> |
| | 64 | | public IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client) |
| | 65 | | { |
| 0 | 66 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| 0 | 67 | | return dbContext.ItemDisplayPreferences |
| 0 | 68 | | .Where(prefs => prefs.UserId.Equals(userId) && !prefs.ItemId.Equals(default) && prefs.Client == client) |
| 0 | 69 | | .ToList(); |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | /// <inheritdoc /> |
| | 73 | | public Dictionary<string, string?> ListCustomItemDisplayPreferences(Guid userId, Guid itemId, string client) |
| | 74 | | { |
| 0 | 75 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| 0 | 76 | | return dbContext.CustomItemDisplayPreferences |
| 0 | 77 | | .Where(prefs => prefs.UserId.Equals(userId) |
| 0 | 78 | | && prefs.ItemId.Equals(itemId) |
| 0 | 79 | | && prefs.Client == client) |
| 0 | 80 | | .ToDictionary(prefs => prefs.Key, prefs => prefs.Value); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | /// <inheritdoc /> |
| | 84 | | public void SetCustomItemDisplayPreferences(Guid userId, Guid itemId, string client, Dictionary<string, string?> cus |
| | 85 | | { |
| 0 | 86 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| 0 | 87 | | dbContext.CustomItemDisplayPreferences.Where(prefs => prefs.UserId.Equals(userId) |
| 0 | 88 | | && prefs.ItemId.Equals(itemId) |
| 0 | 89 | | && prefs.Client == client) |
| 0 | 90 | | .ExecuteDelete(); |
| | 91 | |
|
| 0 | 92 | | foreach (var (key, value) in customPreferences) |
| | 93 | | { |
| 0 | 94 | | dbContext.CustomItemDisplayPreferences |
| 0 | 95 | | .Add(new CustomItemDisplayPreferences(userId, itemId, client, key, value)); |
| | 96 | | } |
| | 97 | |
|
| 0 | 98 | | dbContext.SaveChanges(); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | /// <inheritdoc/> |
| | 102 | | public void UpdateDisplayPreferences(DisplayPreferences displayPreferences) |
| | 103 | | { |
| 0 | 104 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| 0 | 105 | | dbContext.DisplayPreferences.Attach(displayPreferences).State = EntityState.Modified; |
| 0 | 106 | | dbContext.SaveChanges(); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <inheritdoc/> |
| | 110 | | public void UpdateItemDisplayPreferences(ItemDisplayPreferences itemDisplayPreferences) |
| | 111 | | { |
| 0 | 112 | | using var dbContext = _dbContextFactory.CreateDbContext(); |
| 0 | 113 | | dbContext.ItemDisplayPreferences.Attach(itemDisplayPreferences).State = EntityState.Modified; |
| 0 | 114 | | dbContext.SaveChanges(); |
| 0 | 115 | | } |
| | 116 | | } |