//
// 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/.
//
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;
///
/// Provides information and operations for network peers.
///
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));
BindClientEvents();
Configure(OptionsMonitor.CurrentValue);
}
///
/// Gets the list of tracked users.
///
public IReadOnlyList Users => UserDictionary.Values.ToList().AsReadOnly();
///
/// Gets the list of watched usernames.
///
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; }
private IStateMutator StateMutator { get; }
private ConcurrentDictionary UserDictionary { get; set; } = new ConcurrentDictionary();
private ConcurrentDictionary WatchedUsernamesDictionary { get; set; } = new ConcurrentDictionary();
///
/// Gets the name of the group for the specified .
///
/// The username of the peer.
/// The group for the specified username.
public string GetGroup(string username)
{
// note: this is an extremely hot path; keep the work done to an absolute minimum.
if (UserDictionary.TryGetValue(username ?? string.Empty, out var user))
{
if (user.Status?.IsPrivileged ?? false)
{
return Application.PrivilegedGroup;
}
return user.Group ?? Application.DefaultGroup;
}
return Application.DefaultGroup;
}
///
/// 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)
{
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;
}
///
/// Retrieves a peer's IP endpoint, including their IP address and listen port.
///
/// The username of the peer.
/// The retrieved endpoint.
public Task GetIPEndPointAsync(string username)
{
return Client.GetUserEndPointAsync(username);
}
///
/// Retrieves the current of a peer.
///
/// The username of the peer.
/// The retrieved statistics.
public async Task GetStatisticsAsync(string username)
{
var soulseekStatistics = await Client.GetUserStatisticsAsync(username);
return soulseekStatistics.AsStatistics();
}
///
/// Retrieves the current of a peer.
///
/// The username of the peer.
/// The retrieved status.
public async Task GetStatusAsync(string username)
{
var soulseekStatus = await Client.GetUserStatusAsync(username);
return soulseekStatus.AsStatus();
}
///
/// Grants the specified peer the specified number of privilege days.
///
/// The username of the peer.
/// The number of days to grant.
/// The operation context.
public Task GrantPrivilegesAsync(string username, int days)
=> Client.GrantUserPrivilegesAsync(username, days);
///
/// Retrieves a value indicating whether the specified peer is privileged.
///
/// The username of the peer.
/// A value indicating whether the specified peer is privileged.
public async Task IsPrivilegedAsync(string username)
{
var status = await GetStatusAsync(username);
return status.IsPrivileged;
}
///
/// Gets a value indicating whether the specified is watched.
///
/// The username of the peer.
/// A value indicating whether the username is watched.
public bool IsWatched(string username) => WatchedUsernamesDictionary.ContainsKey(username);
///
/// Adds the specified username to the server-side user list.
///
/// The username of the peer.
/// The operation context.
public async Task WatchAsync(string username)
{
// note that this isn't strictly synchronized, and this may be executed more than once per user. this is acceptable,
// and has no side effects other than increased round trips to the server.
if (!WatchedUsernamesDictionary.ContainsKey(username))
{
await Client.AddUserAsync(username);
WatchedUsernamesDictionary.TryAdd(username, true);
Log.Information("Added user {Username} to watch list", username);
// the server does not send a status update when a user is added, but does when that user's status changes.
// request the status explicitly to get the current status.
await GetStatusAsync(username);
}
}
private void BindClientEvents()
{
// it isn't completely clear what all causes the server to send this message. we know it is sent following an explicit
// request, but it may be sent in an unsolicited manner as well. to be safe, we update internal stats for this user
// only on this event, which should cover all bases (both solicited and unsolicited).
Client.UserStatisticsChanged += (_, userStatistics) =>
{
var statistics = userStatistics.AsStatistics();
UserDictionary.AddOrUpdate(
key: userStatistics.Username,
addValue: new User() { Statistics = statistics },
updateValueFactory: (key, user) => user with { Username = userStatistics.Username, Statistics = statistics });
StateMutator.SetValue(state => state with { Users = Users.ToArray() });
};
// the server sends this message in both solicited (we request status), and unsolicited (the user's status changes and
// the server informs us without asking). we handle updates in this event handler to cover both cases.
Client.UserStatusChanged += (_, userStatus) =>
{
var status = userStatus.AsStatus();
UserDictionary.AddOrUpdate(
key: userStatus.Username,
addValue: new User() { Status = status },
updateValueFactory: (key, user) => user with { Username = userStatus.Username, Status = status });
StateMutator.SetValue(state => state with { Users = Users.ToArray() });
// the server doesn't send statistics events by itself, so when a user status changes, fetch stats at the same time.
_ = GetStatisticsAsync(userStatus.Username);
};
Client.LoggedIn += (_, _) => Configure(OptionsMonitor.CurrentValue, force: true);
Client.Disconnected += (_, _) =>
{
// when the client is disconnected, the watched user list resets server-side,
// so clear the state to reflect this
WatchedUsernamesDictionary.Clear();
UserDictionary.Clear();
StateMutator.SetValue(state => state with { Users = Users.ToArray() });
};
}
private void Configure(Options options, bool force = false)
{
var optionsHash = Compute.Sha1Hash(options.Groups.UserDefined.ToJson());
if (optionsHash == LastOptionsHash && !force)
{
return;
}
var usernamesBeforeUpdate = UserDictionary.Keys.ToList();
var usernamesAfterUpdate = options.Groups.UserDefined.SelectMany(g => g.Value.Members);
var usernamesRemoved = usernamesBeforeUpdate.Except(usernamesAfterUpdate);
foreach (var username in usernamesRemoved)
{
UserDictionary.AddOrUpdate(
key: username,
addValue: new User() { Username = username },
updateValueFactory: (key, user) => user with { Username = username, Group = null });
}
// sort by priority, descending. this will cause the highest priority group for the user to be persisted when the
// operation is complete.
foreach (var group in options.Groups.UserDefined.OrderByDescending(kvp => kvp.Value.Upload.Priority))
{
foreach (var username in group.Value.Members)
{
UserDictionary.AddOrUpdate(
key: username,
addValue: new User() { Username = username, Group = group.Key },
updateValueFactory: (key, user) => user with { Username = username, Group = group.Key });
}
}
StateMutator.SetValue(state => state with { Users = Users.ToArray() });
WatchAllUsers();
LastOptionsHash = optionsHash;
}
private void WatchAllUsers()
{
if (Client.State.HasFlag(SoulseekClientStates.Connected) && Client.State.HasFlag(SoulseekClientStates.LoggedIn))
{
foreach (var username in UserDictionary.Keys.ToList())
{
_ = WatchAsync(username);
}
}
}
}
}