Files
Artemy 6d1c53beeb Refactor: migrate Core -> Domain and reorganize projects
Large refactor that renames/moves core types into a new Govor.Domain surface and reorganizes the Application layer. Models, configurations, migrations and many files moved from Govor.Core/Govor.Data to Govor.Domain; numerous Application services, interfaces and implementations were relocated or added (authentication, friends, messages, medias, push notifications, user sessions, storage, synching, private chats, etc.). Tests updated to use Govor.Domain namespaces and adjusted project references (removed Govor.Data reference from API tests). Also updated API, Hub and mapping code and project files to reflect the new structure and naming. This is primarily a codebase-wide namespace and module reorganization to establish a Domain project and restructure application services.
2026-07-16 19:27:45 +07:00

60 lines
2.0 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.UserSession)
.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);
}
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)
.FirstOrDefaultAsync(c => c.UserSessionId == sessionId);
}
}