mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
6d1c53beeb
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.
169 lines
4.9 KiB
C#
169 lines
4.9 KiB
C#
using Govor.Domain;
|
|
using Govor.Domain.Common;
|
|
using Govor.Domain.Models.Users;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Govor.Application.PushNotifications;
|
|
|
|
public class PushTokenService : IPushTokenService
|
|
{
|
|
private readonly GovorDbContext _context;
|
|
private readonly ILogger<PushTokenService> _logger;
|
|
|
|
public PushTokenService(GovorDbContext context, ILogger<PushTokenService> logger)
|
|
{
|
|
_context = context;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<Result> DeactivateTokenBySessionAsync(Guid sessionId)
|
|
{
|
|
try
|
|
{
|
|
await _context.UserPushTokens
|
|
.Where(t => t.UserSessionId == sessionId && t.IsActive)
|
|
.ExecuteUpdateAsync(s => s.SetProperty(t => t.IsActive, false));
|
|
|
|
return Result.Success();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to deactivate push token for session {SessionId}", sessionId);
|
|
return Result.Failure(ex);
|
|
}
|
|
}
|
|
|
|
public async Task<Result> DeactivateAllTokensByUserIdAsync(Guid userId)
|
|
{
|
|
try
|
|
{
|
|
await _context.UserPushTokens
|
|
.Where(t => t.UserId == userId && t.IsActive)
|
|
.ExecuteUpdateAsync(s => s.SetProperty(t => t.IsActive, false));
|
|
|
|
return Result.Success();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to deactivate all push tokens for user {UserId}", userId);
|
|
return Result.Failure(ex);
|
|
}
|
|
}
|
|
|
|
public async Task<Result<List<string>>> GetStringsActiveTokensAsync(Guid userId)
|
|
{
|
|
try
|
|
{
|
|
var tokens = await _context.UserPushTokens
|
|
.AsNoTracking()
|
|
.Where(t => t.UserId == userId && t.IsActive)
|
|
.Select(t => t.Token)
|
|
.ToListAsync();
|
|
|
|
return tokens;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to fetch active push tokens for user {UserId}", userId);
|
|
return Result<List<string>>.Failure(ex);
|
|
}
|
|
}
|
|
|
|
public async Task<Result<List<string>>> GetUsersStringsActiveTokensAsync(IEnumerable<Guid> userIds)
|
|
{
|
|
try
|
|
{
|
|
var tokens = await _context.UserPushTokens
|
|
.AsNoTracking()
|
|
.Where(t => userIds.Contains(t.UserId) && t.IsActive)
|
|
.Select(t => t.Token)
|
|
.ToListAsync();
|
|
|
|
return tokens;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to fetch active push tokens for bulk users");
|
|
return Result<List<string>>.Failure(ex);
|
|
}
|
|
}
|
|
|
|
public async Task<Result<string?>> GetActiveTokenBySessionAsync(Guid sessionId)
|
|
{
|
|
try
|
|
{
|
|
var token = await _context.UserPushTokens
|
|
.AsNoTracking()
|
|
.Where(t => t.UserSessionId == sessionId && t.IsActive)
|
|
.Select(t => t.Token)
|
|
.FirstOrDefaultAsync();
|
|
|
|
return token;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to fetch active push token for session {SessionId}", sessionId);
|
|
return Result<string?>.Failure(ex);
|
|
}
|
|
}
|
|
|
|
public async Task<Result> RemoveTokensAsync(IEnumerable<string> tokens)
|
|
{
|
|
if (tokens is null || !tokens.Any())
|
|
return Result.Success();
|
|
|
|
try
|
|
{
|
|
await _context.UserPushTokens
|
|
.Where(t => tokens.Contains(t.Token))
|
|
.ExecuteDeleteAsync();
|
|
|
|
return Result.Success();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to bulk remove invalid push tokens");
|
|
return Result.Failure(ex);
|
|
}
|
|
}
|
|
|
|
public async Task<Result> AddOrUpdateTokenAsync(Guid userId, Guid sessionId, string token, string platform)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(token))
|
|
{
|
|
return new Error("PushToken.Empty", "Push token cannot be empty.");
|
|
}
|
|
|
|
var existingToken = await _context.UserPushTokens
|
|
.FirstOrDefaultAsync(t => t.Token == token);
|
|
|
|
if (existingToken is null)
|
|
{
|
|
var newToken = new UserPushToken
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
UserId = userId,
|
|
UserSessionId = sessionId,
|
|
Token = token,
|
|
Platform = platform,
|
|
CreatedAt = DateTime.UtcNow
|
|
};
|
|
|
|
await _context.UserPushTokens.AddAsync(newToken);
|
|
}
|
|
else
|
|
{
|
|
|
|
existingToken.UserId = userId;
|
|
existingToken.UserSessionId = sessionId;
|
|
existingToken.Platform = platform;
|
|
existingToken.UpdatedAt = DateTime.UtcNow;
|
|
}
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Result.Success();
|
|
}
|
|
}
|