From 2b64f48bc6acb79bd2844333afea7e7a70d77a22 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 23 Mar 2022 10:14:33 -0500 Subject: move Info factory to extensions --- src/slskd/Users/Domain/Info.cs | 24 +----------------------- src/slskd/Users/Extensions.cs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 23 deletions(-) (limited to 'src') 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, }; + + /// + /// Converts to . + /// + /// The UserInfo instance to convert. + /// The converted instance. + 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, + }; } } -- cgit v1.2.3 From cff862e12a846c6328d5cf77e2b12187cc6d4e30 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 23 Mar 2022 10:16:58 -0500 Subject: don't save user info to a database --- src/slskd/Users/IUserService.cs | 5 ++--- src/slskd/Users/UserService.cs | 39 +++------------------------------------ 2 files changed, 5 insertions(+), 39 deletions(-) (limited to 'src') 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 . /// /// The username of the peer. - /// A value indicating whether the local cache should be bypassed. - /// The cached or retrieved info. - Task GetInfoAsync(string username, bool bypassCache = false); + /// The retrieved info. + Task GetInfoAsync(string username); /// /// Retrieves a peer's IP endpoint, including their IP address and listen port. 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 /// public class UserService : IUserService { - private const int CacheTTLSeconds = 300; - /// /// Initializes a new instance of the class. /// /// /// - /// The database context to use. /// public UserService( ISoulseekClient soulseekClient, IStateMutator stateMutator, - IDbContextFactory contextFactory, IOptionsMonitor optionsMonitor) { Client = soulseekClient; StateMutator = stateMutator; - ContextFactory = contextFactory; - OptionsMonitor = optionsMonitor; OptionsMonitor.OnChange(options => Configure(options)); @@ -87,7 +79,6 @@ namespace slskd.Users public IReadOnlyList WatchedUsernames => WatchedUsernamesDictionary.Keys.ToList().AsReadOnly(); private ISoulseekClient Client { get; } - private IDbContextFactory ContextFactory { get; } private string LastOptionsHash { get; set; } private ILogger Log { get; set; } = Serilog.Log.ForContext(); private IOptionsMonitor OptionsMonitor { get; } @@ -130,35 +121,11 @@ namespace slskd.Users /// Retrieves peer . /// /// The username of the peer. - /// A value indicating whether the local cache should be bypassed. - /// The cached or retrieved info. - public async Task GetInfoAsync(string username, bool bypassCache = false) + /// The retrieved info. + public async Task 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(); } /// -- cgit v1.2.3 From bf27026a4b13d49eb85bb881ea2353b9c417c7bf Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 23 Mar 2022 10:17:09 -0500 Subject: rename Peers to Users --- src/slskd/Users/API/Controllers/UsersController.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'src') 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 /// /// /// - /// - public UsersController(ISoulseekClient soulseekClient, IBrowseTracker browseTracker, IUserService peerService) + /// + 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; } /// /// Retrieves the address of the specified . @@ -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 . /// /// The username of the user. - /// Bypasses the internal cache and requests the latest information from the peer. /// [HttpGet("{username}/info")] [Authorize] [ProducesResponseType(typeof(Info), 200)] [ProducesResponseType(404)] - public async Task Info([FromRoute, Required] string username, [FromQuery]bool bypassCache = false) + public async Task 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) -- cgit v1.2.3 From d0698f7aa7a4c9537c9d1bfbc30a93fec877b58d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 23 Mar 2022 10:17:17 -0500 Subject: remove UserDbContext --- src/slskd/Users/UserDbContext.cs | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 src/slskd/Users/UserDbContext.cs (limited to 'src') 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 (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/. -// - -namespace slskd.Users -{ - using Microsoft.EntityFrameworkCore; - - public class UserDbContext : DbContext - { - public UserDbContext(DbContextOptions options) - : base(options) - { - Database.EnsureCreated(); - } - - public DbSet Info { get; set; } - } -} -- cgit v1.2.3 From 7777cf6c2dcf91051d58d01b4bde6301e3f9387b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 23 Mar 2022 10:17:36 -0500 Subject: remove UserDbContext factory --- src/slskd/Startup.cs | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src') 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(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().CreateDbContext(); - using var peer = GetFactory().CreateDbContext(); } catch (Exception ex) { -- cgit v1.2.3