diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-03-25 07:54:44 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-25 07:54:44 -0500 |
| commit | 511748128942afa64231fbaa1ca971a354bcc1d1 (patch) | |
| tree | 74358c0126e0c0226fbae088b38830342ce017e0 /src | |
| parent | 78b7cb219b155e9be951b9c6b7ca9eb91ba794b2 (diff) | |
| parent | c31ea043beed4d4cc15a8ab5fe28388e8a4155ff (diff) | |
| download | slskd-511748128942afa64231fbaa1ca971a354bcc1d1.tar.gz | |
Merge pull request #396 from slskd/info
Remove persistence of user info
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Startup.cs | 6 | ||||
| -rw-r--r-- | src/slskd/Users/API/Controllers/UsersController.cs | 17 | ||||
| -rw-r--r-- | src/slskd/Users/Domain/Info.cs | 24 | ||||
| -rw-r--r-- | src/slskd/Users/Extensions.cs | 15 | ||||
| -rw-r--r-- | src/slskd/Users/IUserService.cs | 5 | ||||
| -rw-r--r-- | src/slskd/Users/UserDbContext.cs | 32 | ||||
| -rw-r--r-- | src/slskd/Users/UserService.cs | 39 |
7 files changed, 29 insertions, 109 deletions
diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index b1af2482..29f9717f 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -224,11 +224,6 @@ namespace slskd options.UseSqlite($"Data Source={Path.Combine(Program.AppDirectory, "data", "search.db")}"); }); - services.AddDbContextFactory<UserDbContext>(options => - { - options.UseSqlite($"Data Source={Path.Combine(Program.AppDirectory, "data", "users.db")}"); - }); - services.AddHttpClient(); // add a partially configured instance of SoulseekClient. the Application instance will @@ -403,7 +398,6 @@ namespace slskd try { using var search = GetFactory<SearchDbContext>().CreateDbContext(); - using var peer = GetFactory<UserDbContext>().CreateDbContext(); } catch (Exception ex) { diff --git a/src/slskd/Users/API/Controllers/UsersController.cs b/src/slskd/Users/API/Controllers/UsersController.cs index e7eb5256..2439785e 100644 --- a/src/slskd/Users/API/Controllers/UsersController.cs +++ b/src/slskd/Users/API/Controllers/UsersController.cs @@ -40,17 +40,17 @@ namespace slskd.Users.API /// </summary> /// <param name="soulseekClient"></param> /// <param name="browseTracker"></param> - /// <param name="peerService"></param> - public UsersController(ISoulseekClient soulseekClient, IBrowseTracker browseTracker, IUserService peerService) + /// <param name="userService"></param> + public UsersController(ISoulseekClient soulseekClient, IBrowseTracker browseTracker, IUserService userService) { Client = soulseekClient; BrowseTracker = browseTracker; - Peers = peerService; + Users = userService; } private IBrowseTracker BrowseTracker { get; } private ISoulseekClient Client { get; } - private IUserService Peers { get; } + private IUserService Users { get; } /// <summary> /// Retrieves the address of the specified <paramref name="username"/>. @@ -66,7 +66,7 @@ namespace slskd.Users.API { try { - var endpoint = await Peers.GetIPEndPointAsync(username); + var endpoint = await Users.GetIPEndPointAsync(username); return Ok(endpoint); } catch (UserOfflineException ex) @@ -127,17 +127,16 @@ namespace slskd.Users.API /// Retrieves information about the specified <paramref name="username"/>. /// </summary> /// <param name="username">The username of the user.</param> - /// <param name="bypassCache">Bypasses the internal cache and requests the latest information from the peer.</param> /// <returns></returns> [HttpGet("{username}/info")] [Authorize] [ProducesResponseType(typeof(Info), 200)] [ProducesResponseType(404)] - public async Task<IActionResult> Info([FromRoute, Required] string username, [FromQuery]bool bypassCache = false) + public async Task<IActionResult> Info([FromRoute, Required] string username) { try { - var response = await Peers.GetInfoAsync(username, bypassCache); + var response = await Users.GetInfoAsync(username); return Ok(response); } catch (UserOfflineException ex) @@ -159,7 +158,7 @@ namespace slskd.Users.API { try { - var response = await Peers.GetStatusAsync(username); + var response = await Users.GetStatusAsync(username); return Ok(response); } catch (UserOfflineException ex) diff --git a/src/slskd/Users/Domain/Info.cs b/src/slskd/Users/Domain/Info.cs index aaf19942..e3969f17 100644 --- a/src/slskd/Users/Domain/Info.cs +++ b/src/slskd/Users/Domain/Info.cs @@ -17,35 +17,13 @@ namespace slskd.Users { - using System; - using System.ComponentModel.DataAnnotations; - using Soulseek; - - public class Info + public record Info { - [Key] - public string Username { get; init; } public string Description { get; init; } public bool HasFreeUploadSlot { get; init; } public bool HasPicture { get; init; } public byte[] Picture { get; init; } public int QueueLength { get; init; } public int UploadSlots { get; init; } - public DateTime UpdatedAt { get; init; } - - public static Info FromSoulseekUserInfo(string username, UserInfo info) - { - return new Info() - { - Username = username, - Description = info.Description, - HasFreeUploadSlot = info.HasFreeUploadSlot, - HasPicture = info.HasPicture, - Picture = info.Picture, - QueueLength = info.QueueLength, - UploadSlots = info.UploadSlots, - UpdatedAt = DateTime.UtcNow, - }; - } } } diff --git a/src/slskd/Users/Extensions.cs b/src/slskd/Users/Extensions.cs index 846fd125..d2189a20 100644 --- a/src/slskd/Users/Extensions.cs +++ b/src/slskd/Users/Extensions.cs @@ -47,5 +47,20 @@ namespace slskd.Users IsPrivileged = s.IsPrivileged, Presence = s.Presence, }; + + /// <summary> + /// Converts <see cref="Soulseek.UserInfo"/> to <see cref="Info"/>. + /// </summary> + /// <param name="i">The UserInfo instance to convert.</param> + /// <returns>The converted instance.</returns> + public static Info ToInfo(this UserInfo i) => new() + { + Description = i.Description, + HasFreeUploadSlot = i.HasFreeUploadSlot, + HasPicture = i.HasPicture, + Picture = i.Picture, + QueueLength = i.QueueLength, + UploadSlots = i.UploadSlots, + }; } } diff --git a/src/slskd/Users/IUserService.cs b/src/slskd/Users/IUserService.cs index e78b1b1c..88090f9f 100644 --- a/src/slskd/Users/IUserService.cs +++ b/src/slskd/Users/IUserService.cs @@ -47,9 +47,8 @@ namespace slskd.Users /// Retrieves peer <see cref="Info"/>. /// </summary> /// <param name="username">The username of the peer.</param> - /// <param name="bypassCache">A value indicating whether the local cache should be bypassed.</param> - /// <returns>The cached or retrieved info.</returns> - Task<Info> GetInfoAsync(string username, bool bypassCache = false); + /// <returns>The retrieved info.</returns> + Task<Info> GetInfoAsync(string username); /// <summary> /// Retrieves a peer's IP endpoint, including their IP address and listen port. diff --git a/src/slskd/Users/UserDbContext.cs b/src/slskd/Users/UserDbContext.cs deleted file mode 100644 index 4d0fd08c..00000000 --- a/src/slskd/Users/UserDbContext.cs +++ /dev/null @@ -1,32 +0,0 @@ -// <copyright file="UserDbContext.cs" company="slskd Team"> -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// </copyright> - -namespace slskd.Users -{ - using Microsoft.EntityFrameworkCore; - - public class UserDbContext : DbContext - { - public UserDbContext(DbContextOptions<UserDbContext> options) - : base(options) - { - Database.EnsureCreated(); - } - - public DbSet<Info> Info { get; set; } - } -} diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index f081d8b1..aa2ae234 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -19,13 +19,11 @@ using Microsoft.Extensions.Options; namespace slskd.Users { - using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading.Tasks; - using Microsoft.EntityFrameworkCore; using Serilog; using Soulseek; @@ -34,27 +32,21 @@ namespace slskd.Users /// </summary> public class UserService : IUserService { - private const int CacheTTLSeconds = 300; - /// <summary> /// Initializes a new instance of the <see cref="UserService"/> class. /// </summary> /// <param name="soulseekClient"></param> /// <param name="stateMutator"></param> - /// <param name="contextFactory">The database context to use.</param> /// <param name="optionsMonitor"></param> public UserService( ISoulseekClient soulseekClient, IStateMutator<State> stateMutator, - IDbContextFactory<UserDbContext> contextFactory, IOptionsMonitor<Options> optionsMonitor) { Client = soulseekClient; StateMutator = stateMutator; - ContextFactory = contextFactory; - OptionsMonitor = optionsMonitor; OptionsMonitor.OnChange(options => Configure(options)); @@ -87,7 +79,6 @@ namespace slskd.Users public IReadOnlyList<string> WatchedUsernames => WatchedUsernamesDictionary.Keys.ToList().AsReadOnly(); private ISoulseekClient Client { get; } - private IDbContextFactory<UserDbContext> ContextFactory { get; } private string LastOptionsHash { get; set; } private ILogger Log { get; set; } = Serilog.Log.ForContext<UserService>(); private IOptionsMonitor<Options> OptionsMonitor { get; } @@ -130,35 +121,11 @@ namespace slskd.Users /// Retrieves peer <see cref="Info"/>. /// </summary> /// <param name="username">The username of the peer.</param> - /// <param name="bypassCache">A value indicating whether the local cache should be bypassed.</param> - /// <returns>The cached or retrieved info.</returns> - public async Task<Info> GetInfoAsync(string username, bool bypassCache = false) + /// <returns>The retrieved info.</returns> + public async Task<Info> GetInfoAsync(string username) { - using var context = ContextFactory.CreateDbContext(); - - var info = await context.Info.AsNoTracking().FirstOrDefaultAsync(peer => peer.Username == username); - var peerExists = info != default; - - if (!bypassCache && peerExists && info.UpdatedAt < DateTime.UtcNow.AddSeconds(CacheTTLSeconds)) - { - return info; - } - var soulseekUserInfo = await Client.GetUserInfoAsync(username); - info = Info.FromSoulseekUserInfo(username, soulseekUserInfo); - - if (peerExists) - { - context.Update(info); - } - else - { - context.Add(info); - } - - await context.SaveChangesAsync(); - - return info; + return soulseekUserInfo.ToInfo(); } /// <summary> |
