Files
Govor/Govor.Application/Profiles/ProfileService.cs
Artemy 29cc51ee0c Add reactions, reliable push & session checks
Implements message reactions and reaction packs (domain models, services, admin HTTP API, media processing via ImageSharp and limits) and adds reliable push delivery (ChatPushNotification, ChatPushDispatcher, ChatPushWorker, improved ChatNotificationService). Enhances message reading (ReadChat API, unread counts, concurrency-safe receipts) and tightens session security (access-token session validation in JWT, hub filter, SessionAuthorization). Improves SignalR behavior (session groups, reaction/channel events, hub/error mappings). Adds EF migrations, many controller fixes (Forbid handling, password API), tests (Govor.Server.Tests), docs, CI and tooling updates (Dockerfile → .NET 10, GH Actions matrix for sqlite/postgres, Govor.Server.slnx, global.json), and small infra changes (AutoMapper bump, NullPushProvider, storage/token handling).
2026-10-03 17:42:24 +07:00

85 lines
2.6 KiB
C#

using Govor.Domain;
using Govor.Domain.Common;
using Govor.Domain.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using SmartRes;
namespace Govor.Application.Profiles;
public class ProfileService : IProfileService
{
private readonly ILogger<ProfileService> _logger;
private readonly GovorDbContext _context;
public ProfileService(GovorDbContext context, ILogger<ProfileService> logger)
{
_context = context;
_logger = logger;
}
public async Task<Result<UserProfile, Error>> GetUserProfileAsync(Guid userId)
{
_logger.LogInformation("Getting user {UserId} profile", userId);
var profile = await _context.Users
.AsNoTracking()
.Where(u => u.Id == userId)
.Select(user => new UserProfile
{
Id = user.Id,
Username = user.Username,
Description = user.Description,
IconId = user.IconId
})
.FirstOrDefaultAsync();
if (profile is null)
{
return Result.Failure<UserProfile>(CreateNotFoundError(userId));
}
return profile;
}
public async Task<Result<Unit, Error>> SetDescription(string description, Guid userId)
{
_logger.LogInformation("Updating description for user {UserId}", userId);
var updatedRows = await _context.Users
.Where(u => u.Id == userId)
.ExecuteUpdateAsync(setters => setters.SetProperty(u => u.Description, description));
if (updatedRows == 0)
{
return Result.Failure(CreateNotFoundError(userId));
}
return Result.Success();
}
public async Task<Result<Unit, Error>> SetNewIcon(Guid userId, Guid iconId)
{
_logger.LogInformation("Updating icon for user {UserId}", userId);
if (!await _context.MediaFiles.AnyAsync(f => f.Id == iconId && f.UploaderId == userId &&
f.OwnerType == MediaOwnerType.Avatar))
return Result<Unit, Error>.Failure(Error.Forbidden("Profile.IconAccess", "Only your uploaded avatar can be used."));
var updatedRows = await _context.Users
.Where(u => u.Id == userId)
.ExecuteUpdateAsync(setters => setters.SetProperty(u => u.IconId, iconId));
if (updatedRows == 0)
{
return Result.Failure(CreateNotFoundError(userId));
}
return Result.Success();
}
private static Error CreateNotFoundError(Guid userId) =>
Error.NotFound("Profile.UserNotFound", $"User with ID {userId} was not found.");
}