aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-05-30 23:04:58 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-05-30 23:04:58 -0500
commitedd7a8bcb1b055942c4ceecd1d9db9797a5071c1 (patch)
tree5eadc609fc58187422963dd1f5aa7c29a74bee38 /src
parent8b916c494687349ae94361f8835f6347ba01d227 (diff)
downloadslskd-edd7a8bcb1b055942c4ceecd1d9db9797a5071c1.tar.gz
rough in PeerService
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Core/Peer/Services/IPeerService.cs27
-rw-r--r--src/slskd/Core/Peer/Services/PeerService.cs96
-rw-r--r--src/slskd/Startup.cs6
3 files changed, 128 insertions, 1 deletions
diff --git a/src/slskd/Core/Peer/Services/IPeerService.cs b/src/slskd/Core/Peer/Services/IPeerService.cs
new file mode 100644
index 00000000..0c0738a3
--- /dev/null
+++ b/src/slskd/Core/Peer/Services/IPeerService.cs
@@ -0,0 +1,27 @@
+// <copyright file="IPeerService.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.Peer
+{
+ using System.Threading;
+ using System.Threading.Tasks;
+
+ public interface IPeerService
+ {
+ Task<Peer> GetAsync(string username);
+ }
+}
diff --git a/src/slskd/Core/Peer/Services/PeerService.cs b/src/slskd/Core/Peer/Services/PeerService.cs
new file mode 100644
index 00000000..782a4702
--- /dev/null
+++ b/src/slskd/Core/Peer/Services/PeerService.cs
@@ -0,0 +1,96 @@
+// <copyright file="PeerService.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.Peer
+{
+ using System;
+ using System.Threading.Tasks;
+ using Microsoft.EntityFrameworkCore;
+ using Microsoft.Extensions.Logging;
+ using ISoulseekClient = Soulseek.ISoulseekClient;
+
+ public class PeerService : IPeerService
+ {
+ private const int CacheTTLSeconds = 300;
+
+ public PeerService(
+ ISoulseekClient client,
+ IDbContextFactory<PeerDbContext> contextFactory,
+ ILogger<PeerService> log)
+ {
+ Client = client;
+ ContextFactory = contextFactory;
+ Log = log;
+ }
+
+ private ISoulseekClient Client { get; }
+ private IDbContextFactory<PeerDbContext> ContextFactory { get; }
+ private ILogger<PeerService> Log { get; set; }
+
+ public async Task<Peer> GetAsync(string username)
+ {
+ using var context = ContextFactory.CreateDbContext();
+
+ var peer = await context.Peers.FirstOrDefaultAsync(peer => peer.Username == username);
+ var peerExists = peer != default;
+
+ if (peerExists && peer.UpdatedAt < DateTime.UtcNow.AddSeconds(CacheTTLSeconds))
+ {
+ return peer;
+ }
+
+ var infoTask = Client.GetUserInfoAsync(username);
+ var statusTask = Client.GetUserStatusAsync(username);
+ var endpointTask = Client.GetUserEndPointAsync(username);
+
+ await Task.WhenAll(infoTask, statusTask, endpointTask);
+
+ var info = await infoTask;
+ var status = await statusTask;
+ var endpoint = await endpointTask;
+
+ peer = new Peer()
+ {
+ Username = username,
+ Description = info.Description,
+ HasFreeUploadSlot = info.HasFreeUploadSlot,
+ HasPicture = info.HasPicture,
+ Picture = info.Picture,
+ QueueLength = info.QueueLength,
+ UploadSlots = info.UploadSlots,
+ IsPrivileged = status.IsPrivileged,
+ Presence = status.Presence,
+ IPAddress = endpoint.Address,
+ Port = endpoint.Port,
+ UpdatedAt = DateTime.UtcNow,
+ };
+
+ if (peerExists)
+ {
+ context.Update(peer);
+ }
+ else
+ {
+ context.Add(peer);
+ }
+
+ await context.SaveChangesAsync();
+
+ return peer;
+ }
+ }
+}
diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs
index 4d11ad1a..0c8804f0 100644
--- a/src/slskd/Startup.cs
+++ b/src/slskd/Startup.cs
@@ -171,7 +171,10 @@ namespace slskd
options.UseSqlite($"Data Source={Path.Combine(Options.Directories.App, "search.db")}");
});
- services.AddTransient(p => p.GetRequiredService<IDbContextFactory<SearchDbContext>>().CreateDbContext());
+ services.AddDbContextFactory<PeerDbContext>(options =>
+ {
+ options.UseSqlite($"Data Source={Path.Combine(Options.Directories.App, "peer.db")}");
+ });
services.AddSingleton<ITransferTracker, TransferTracker>();
services.AddSingleton<IBrowseTracker, BrowseTracker>();
@@ -181,6 +184,7 @@ namespace slskd
new SharedFileCache(Options.Directories.Shared, 3600000));
services.AddSingleton<ISearchService, SearchService>();
+ services.AddSingleton<IPeerService, PeerService>();
services.AddHostedService<Service>();
services.AddSingleton(_ => Service.SoulseekClient);