mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-06-23 15:30:34 -04:00
* Fixed an issue where reading list were not able to update their summary due to a duplicate title check. * Misc code smell cleanup * Updated .net dependencies and removed unneeded ones * Fixed an issue where removing a series from want to read list page wouldn't update the page correctly * Fixed age restriction not applied to Recommended page * Ensure that Genres and Tags are age restricted gated * Persons are now age gated as well * When you choose a cover, the new cover will properly be selected and will focus on it, in the cases there are many other covers available. * Fixed caching profiles * Added in a special hook when deleting a library to clear all series Relations before we delete
114 lines
3.2 KiB
C#
114 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.Data;
|
|
|
|
namespace API.SignalR.Presence;
|
|
|
|
public interface IPresenceTracker
|
|
{
|
|
Task UserConnected(string username, string connectionId);
|
|
Task UserDisconnected(string username, string connectionId);
|
|
Task<string[]> GetOnlineAdmins();
|
|
Task<List<string>> GetConnectionsForUser(string username);
|
|
|
|
}
|
|
|
|
internal class ConnectionDetail
|
|
{
|
|
public List<string> ConnectionIds { get; set; }
|
|
public bool IsAdmin { get; set; }
|
|
}
|
|
|
|
// TODO: This can respond to UserRoleUpdate events to handle online users
|
|
/// <summary>
|
|
/// This is a singleton service for tracking what users have a SignalR connection and their difference connectionIds
|
|
/// </summary>
|
|
public class PresenceTracker : IPresenceTracker
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private static readonly Dictionary<string, ConnectionDetail> OnlineUsers = new Dictionary<string, ConnectionDetail>();
|
|
|
|
public PresenceTracker(IUnitOfWork unitOfWork)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
}
|
|
|
|
public async Task UserConnected(string username, string connectionId)
|
|
{
|
|
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(username);
|
|
if (user == null) return;
|
|
var isAdmin = await _unitOfWork.UserRepository.IsUserAdminAsync(user);
|
|
lock (OnlineUsers)
|
|
{
|
|
if (OnlineUsers.ContainsKey(username))
|
|
{
|
|
OnlineUsers[username].ConnectionIds.Add(connectionId);
|
|
}
|
|
else
|
|
{
|
|
OnlineUsers.Add(username, new ConnectionDetail()
|
|
{
|
|
ConnectionIds = new List<string>() {connectionId},
|
|
IsAdmin = isAdmin
|
|
});
|
|
}
|
|
}
|
|
|
|
// Update the last active for the user
|
|
user.LastActive = DateTime.Now;
|
|
await _unitOfWork.CommitAsync();
|
|
}
|
|
|
|
public Task UserDisconnected(string username, string connectionId)
|
|
{
|
|
lock (OnlineUsers)
|
|
{
|
|
if (!OnlineUsers.ContainsKey(username)) return Task.CompletedTask;
|
|
|
|
OnlineUsers[username].ConnectionIds.Remove(connectionId);
|
|
|
|
if (OnlineUsers[username].ConnectionIds.Count == 0)
|
|
{
|
|
OnlineUsers.Remove(username);
|
|
}
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public static Task<string[]> GetOnlineUsers()
|
|
{
|
|
string[] onlineUsers;
|
|
lock (OnlineUsers)
|
|
{
|
|
onlineUsers = OnlineUsers.OrderBy(k => k.Key).Select(k => k.Key).ToArray();
|
|
}
|
|
|
|
return Task.FromResult(onlineUsers);
|
|
}
|
|
|
|
public Task<string[]> GetOnlineAdmins()
|
|
{
|
|
string[] onlineUsers;
|
|
lock (OnlineUsers)
|
|
{
|
|
onlineUsers = OnlineUsers.Where(pair => pair.Value.IsAdmin).OrderBy(k => k.Key).Select(k => k.Key).ToArray();
|
|
}
|
|
|
|
|
|
return Task.FromResult(onlineUsers);
|
|
}
|
|
|
|
public Task<List<string>> GetConnectionsForUser(string username)
|
|
{
|
|
List<string> connectionIds;
|
|
lock (OnlineUsers)
|
|
{
|
|
connectionIds = OnlineUsers.GetValueOrDefault(username)?.ConnectionIds;
|
|
}
|
|
|
|
return Task.FromResult(connectionIds ?? new List<string>());
|
|
}
|
|
}
|