mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
565d3249e5
Introduces OnlinePingingController and related integration/unit tests for user online status updates. Adds PingHandlerService with memory cache throttling, IPingHandlerService interface, and service registration. Implements VerifyFriendship service and interface for friendship validation, with exception handling. Refactors CurrentUserService for improved user ID extraction and testability. Updates ChatsHub to verify friendship before messaging. Cleans up MediaController and optimizes UsersRepository queries by removing unnecessary includes.
34 lines
1.0 KiB
C#
34 lines
1.0 KiB
C#
using Govor.Application.Interfaces;
|
|
using Govor.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
namespace Govor.Application.Services;
|
|
|
|
public class PingHandlerService : IPingHandlerService
|
|
{
|
|
private readonly GovorDbContext _context;
|
|
private readonly IMemoryCache _cache;
|
|
|
|
public PingHandlerService(GovorDbContext context, IMemoryCache cache)
|
|
{
|
|
_context = context;
|
|
_cache = cache;
|
|
}
|
|
|
|
public async Task Ping(Guid userId)
|
|
{
|
|
var cacheKey = $"LastPing_{userId}";
|
|
if (_cache.TryGetValue(cacheKey, out DateTime lastPing) &&
|
|
DateTime.UtcNow.Subtract(lastPing).TotalSeconds < 30)
|
|
{
|
|
return; // Пропускаем слишком частые пинги
|
|
}
|
|
|
|
await _context.Users
|
|
.Where(u => u.Id == userId)
|
|
.ExecuteUpdateAsync(u => u.SetProperty(x => x.WasOnline, DateTime.UtcNow));
|
|
|
|
_cache.Set(cacheKey, DateTime.UtcNow, TimeSpan.FromSeconds(30));
|
|
}
|
|
} |