using Knot.Contracts.Profiles.Application.DTOs; using Knot.Contracts.Profiles.Domain; using Knot.Shared.Kernel; using MediatR; namespace Knot.Modules.Profiles.Application.Statuses; public sealed record SetCustomUserStatusCommand( Guid UserId, string? Emoji, string? Text, DateTime? ExpiresAt, string? PresetKey) : IRequest>; public sealed class SetCustomUserStatusCommandHandler : IRequestHandler> { private readonly IProfileStatusWriter _writer; public SetCustomUserStatusCommandHandler(IProfileStatusWriter writer) { _writer = writer; } public async Task> Handle(SetCustomUserStatusCommand request, CancellationToken cancellationToken) { var key = request.PresetKey?.Trim(); if (!string.IsNullOrEmpty(key) && key.Equals("online", StringComparison.OrdinalIgnoreCase)) return await _writer.ClearAsync(request.UserId, cancellationToken); var emoji = request.Emoji?.Trim() ?? string.Empty; var text = request.Text?.Trim() ?? string.Empty; if (!string.IsNullOrEmpty(key)) { var preset = StatusPresetCatalog.Find(key); if (preset is null) return Result.Failure(ProfilesErrors.InvalidPreset); if (string.IsNullOrEmpty(emoji)) emoji = preset.Emoji; if (string.IsNullOrEmpty(text)) text = preset.TextRu; } if (string.IsNullOrEmpty(emoji) && string.IsNullOrEmpty(text)) return Result.Failure(ProfilesErrors.StatusEmpty); if (text.Length > 50) return Result.Failure(ProfilesErrors.StatusTextTooLong); var presetForWriter = string.IsNullOrWhiteSpace(key) ? null : key; return await _writer.SetCustomAsync(request.UserId, emoji, text, request.ExpiresAt, presetForWriter, cancellationToken); } }