Files
Govor/Govor.Application/Users/UserSessions/Crypto/SessionKeysReader.cs
Artemy 2d99c57a3b Expand server tests and enforce coverage
Consolidate server tests under tests/, upgrade them to .NET 10, and add CI coverage thresholds and reports. Expand API and application boundary coverage while fixing related issues in media handling, friend blocking, session crypto, presence, username validation, and local storage.
2026-10-24 15:30:32 +07:00

62 lines
2.1 KiB
C#

using Govor.Domain.Models.Users.Crypto;
using Govor.Domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace Govor.Application.Users.UserSessions.Crypto;
public class SessionKeysReader : ISessionKeysReader
{
private readonly ILogger<SessionKeyAttacher> _logger;
private readonly GovorDbContext _context;
public SessionKeysReader(ILogger<SessionKeyAttacher> logger, GovorDbContext context)
{
_logger = logger;
_context = context;
}
public async Task<bool> HasKeysAttachedAsync(Guid sessionId)
{
return await _context.UserCryptoSessions.AnyAsync(c => c.UserSessionId == sessionId);
}
public async Task<IReadOnlyList<UserCryptoSession>> GetAllActiveKeysAsync(Guid userId)
{
_logger.LogInformation("Getting all active keys for user {UserId}.", userId);
var now = DateTime.UtcNow;
return await _context.UserCryptoSessions
.AsNoTracking()
.Include(c => c.OneTimePreKeys)
.Include(c => c.SignedPreKey)
.Where(c => c.UserSession.UserId == userId
&& !c.UserSession.IsRevoked
&& c.UserSession.ExpiresAt > now)
.ToListAsync();
}
public async Task<int> GetRemainingOneTimePreKeysCountAsync(Guid sessionId)
{
_logger.LogInformation("Getting count of one time pre keys for session {UserId}.", sessionId);
return await _context.OneTimePreKeys
.AsNoTracking()
.Include(f => f.UserCryptoSession)
.CountAsync(f => f.UserCryptoSession.UserSessionId == sessionId && !f.IsUsed);
}
public async Task<UserCryptoSession?> GetKeysBySessionIdAsync(Guid sessionId)
{
_logger.LogInformation("Getting keys for session {session}.", sessionId);
return await _context.UserCryptoSessions
.AsNoTracking()
.Include(c => c.OneTimePreKeys)
.Include(c => c.UserSession)
.Include(c => c.SignedPreKey)
.FirstOrDefaultAsync(c => c.UserSessionId == sessionId);
}
}